Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Dropdown Value

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Dropdown Value

Postby iLLuZZiOn » Fri Aug 02, 2002 6:08 pm

i need to get the value of a dropdown menu into a variable, Without submiting the form. I also would like it if it updates instantly

So as soon as someone picks an option from the dropdown menu(lets say 'mymenu') it creates a variable called 'myvar'.

Is it possible, if not is there anyway to convert a javascript or vbscript variable into a php variable?
iLLuZZiOn
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Aug 02, 2002 5:59 pm

Postby Jay » Sat Aug 03, 2002 3:12 am

There is no way to do this as Javascript is a client side language, and PHP is a server side language. PHP generates a page BEFORE it's delivered to the client, and then it's job is done. Once the page is delivered to the client Javascript controls how it works while the page is with the client.

What are you trying to achieve? Perhaps it's possible to achieve the same thing with Javascript?
Jay
 

Re: Dropdown Value

Postby WiZARD » Sat Aug 03, 2002 4:59 am

iLLuZZiOn wrote:i need to get the value of a dropdown menu into a variable, Without submiting the form. I also would like it if it updates instantly

So as soon as someone picks an option from the dropdown menu(lets say 'mymenu') it creates a variable called 'myvar'.

Is it possible, if not is there anyway to convert a javascript or vbscript variable into a php variable?

Hi!
I don't know how about Jay, but I think what if use a form with refresh after change dropdown menu you can di it. :wink:
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Re: Dropdown Value

Postby Jay » Sat Aug 03, 2002 5:47 am

WiZARD wrote:Hi!
I don't know how about Jay, but I think what if use a form with refresh after change dropdown menu you can di it. :wink:

Yes, there is that way around it. You can set the drop down menu with javascript to refresh the page with a 'GET' variable, and using this variable you can generate the rest of the page. The reason I didn't mention this was because technically you're still submitting part of a form, although indirectly and I was of the opnion the solution being sought was entirely client side.

Is there any particular reason you need the variables in PHP? You might be able to accomplish the same thing with Javascript which would be better as it would take strain off the server.
Jay
 

Re: Dropdown Value

Postby WiZARD » Sat Aug 03, 2002 5:59 am

Jay wrote:
WiZARD wrote:Hi!
I don't know how about Jay, but I think what if use a form with refresh after change dropdown menu you can di it. :wink:

Yes, there is that way around it. You can set the drop down menu with javascript to refresh the page with a 'GET' variable, and using this variable you can generate the rest of the page. The reason I didn't mention this was because technically you're still submitting part of a form, although indirectly and I was of the opnion the solution being sought was entirely client side.

Is there any particular reason you need the variables in PHP? You might be able to accomplish the same thing with Javascript which would be better as it would take strain off the server.

see this maby it's be interestin for you:
http://www.devshed.com/Server_Side/PHP/ ... lass/print
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Re: Dropdown Value

Postby Jay » Sat Aug 03, 2002 6:44 am

WiZARD wrote:see this maby it's be interestin for you:
http://www.devshed.com/Server_Side/PHP/ ... lass/print

I have read it before. Was there anything specific you wanted to show me?
Jay
 

Re: Dropdown Value

Postby iLLuZZiOn » Sat Aug 03, 2002 4:31 pm

well..

is there any client side language that can access a mySQl database?

what i am trying to do is make a online quote for a computer, so there is a dropdown menu for each componect in a computer.

i have got the values in the dropdown from the mysql database, the value in the dropdown is the product no.

so when it is submited i can use the value in the dropdown the generate the quote

so to get the price i would use

Code: Select all
SELECT Price FROM cpu WHERE ID=1


i think that code is right anyway(if not correct me)


but i also want to have a text box that despays the price of the item in the dropdown.
iLLuZZiOn
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Aug 02, 2002 5:59 pm

Re: Dropdown Value

Postby Jay » Sat Aug 03, 2002 7:20 pm

iLLuZZiOn wrote:well..

is there any client side language that can access a mySQl database?

No, because the database resides on the server, not the client.
iLLuZZiOn wrote:what i am trying to do is make a online quote for a computer, so there is a dropdown menu for each componect in a computer.

i have got the values in the dropdown from the mysql database, the value in the dropdown is the product no.

so when it is submited i can use the value in the dropdown the generate the quote

so to get the price i would use

Code: Select all
SELECT Price FROM cpu WHERE ID=1


i think that code is right anyway(if not correct me)


but i also want to have a text box that despays the price of the item in the dropdown.

If you really want it done without refreshing the page or submitting the forms at various stages, then you'll have to load all the values into javascript and display it from there. PHP and MySQL can only work with variables that are sent back to them once variables are sent back.
Jay
 

Re: Dropdown Value

Postby WiZARD » Sat Aug 03, 2002 11:05 pm

iLLuZZiOn wrote:well..

is there any client side language that can access a mySQl database?

what i am trying to do is make a online quote for a computer, so there is a dropdown menu for each componect in a computer.

i have got the values in the dropdown from the mysql database, the value in the dropdown is the product no.

so when it is submited i can use the value in the dropdown the generate the quote

so to get the price i would use

Code: Select all
SELECT Price FROM cpu WHERE ID=1


i think that code is right anyway(if not correct me)


but i also want to have a text box that despays the price of the item in the dropdown.

Hi!
In PHP (server-side) you do not can create all what you want.
But, if youre can learn Java you can write applets....
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Re: Dropdown Value

Postby iLLuZZiOn » Sat Aug 03, 2002 11:34 pm

so is there anyway to do this in a html page

with asp or .jsp

could it be done in flash?
iLLuZZiOn
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Aug 02, 2002 5:59 pm

Re: Dropdown Value

Postby Jay » Sun Aug 04, 2002 3:36 am

iLLuZZiOn wrote:so is there anyway to do this in a html page

with asp or .jsp

could it be done in flash?

ASP again is a server side language, as is JSP (AFAIK). They won't do it because there is NO link left open to the server once the page is delivered!!

As for Flash, I don't know. It might be possible, but I doubt it since it's mainly a vector graphics program and any solution you could up with in Flash would be far far easier to implement in PHP directly.

In either case, this is a PHP forum and you wouldn't find the answers to that question here. :wink:
Jay
 

Re: Dropdown Value

Postby iLLuZZiOn » Mon Aug 05, 2002 12:33 am

That is true

well i would like to do it php, i might just put a button to update the prices on the quote

ok now another question...

how do you get a value from a cell in the mySQL database

the way i have set it up is i have the ID number of the item as the value in the dropdown

so when i submit the form it sends the IDnumber to the next page.

now i need to use that value to get something out of the database

eg. the IDnumber is '2'

say i need the price of the selected item i would want it to use 'UnitPrice' with row 2

or where IDnumber='2' .: row 2....



Code: Select all
<?php
$item1price = mysql_query("SELECT UnitPrice FROM Item1table Where IDnumber = $dropdown1value", $Connection1) or die(mysql_error());
echo $item1price;
?>


please correct that statement, becuase it keeps returning Resource id #6

i have no idea what to do, or what i am doing wrong, HELP
iLLuZZiOn
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Aug 02, 2002 5:59 pm

Re: Dropdown Value

Postby Jay » Mon Aug 05, 2002 4:57 am

iLLuZZiOn wrote:
Code: Select all
<?php
$item1price = mysql_query("SELECT UnitPrice FROM Item1table Where IDnumber = $dropdown1value", $Connection1) or die(mysql_error());
echo $item1price;
?>


please correct that statement, becuase it keeps returning Resource id #6

i have no idea what to do, or what i am doing wrong, HELP


What version of PHP are you using? If it's 4.1.0 and above, try this
Code: Select all
<?php
$query = "SELECT UnitPrice FROM Item1table Where IDnumber = ".$_REQUEST['dropdown1value'];
$item1price = mysql_query($query,$Connection1) or die(mysql_error());
echo $item1price;
?>

I've made 2 recommendations. One is to have the query as it's own variable, and just use the variable in the mysql_query function. If the function doesn't return the result you expected, you can just print (or echo as you prefer) the variable to see if PHP has made it correctly (which I think in your case is the error).

In PHP 4.1.0 the variables are passed into Super Global Arrays. I've used $_REQUEST, but for more security you can change this to $_GET or $_POST depending on what method your form uses.

So try printing out the $query variable first, and see if it's correct. And then try the script as normal.
Jay
 

Postby iLLuZZiOn » Wed Aug 07, 2002 3:08 am

Code: Select all
<?php
mysql_select_db($database_Connection1, $Connection1);
$querycpu = "SELECT UnitPrice FROM amd_cpu Where ProductID = ".$_REQUEST['amd_cpu'];
$cpuprice = mysql_query($querycpu,$Connection1) or die(mysql_error());
echo $cpuprice;
?>


ok, this returns Resource id #3

$querycpu returns 'SELECT UnitPrice FROM amd_cpu Where ProductID = 1'

i cant get it to return a the value from the mySQL database!

this is sooo fustrating

and i am using the POST as the method for the form, this is the recieving page
iLLuZZiOn
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Aug 02, 2002 5:59 pm

Postby DoppyNL » Wed Aug 07, 2002 3:27 am

"mysql_query" only executes the query.
you will have to get the results from the database after that for instance with mysql_fetch_array:

Code: Select all
$result = mysql_query($query);
$first_row = mysql_fetch_array($result);


wil give you an array with the values of the first record that the query returns

see also http://www.php.net/mysql_fetch_array

Greetz Daan
DoppyNL
 

Postby iLLuZZiOn » Thu Aug 08, 2002 1:12 am

i can't get this working at all!!!

i need to get a single value from the mySQL database
______________________________________
|IDno|ProductName|ProductDesc|UnitPrice|
| 1 | product1 | Desc1 | $100 |
| 2 | product2 | Desc2 | $200 |
| 3 | product3 | Desc3 | $300 |
| 4 | product4 | Desc4 | $400 |

this is part of my first page

Code: Select all
<form name="ezyquote" method="post" action="print.php">
<select name="amd_cpu" id="amd_cpu">
          <?php
do { 
?>
          <option value="<?php echo $row_amd_cpu['ProductID']?>"><?php echo "($",$row_amd_cpu['UnitPrice'].") ",$row_amd_cpu['Product_Name'] ?></option>
          <?php
} while ($row_amd_cpu = mysql_fetch_assoc($amd_cpu));
  $rows = mysql_num_rows($amd_cpu);
  if($rows > 0) {
      mysql_data_seek($amd_cpu, 0);
     $row_amd_cpu = mysql_fetch_assoc($amd_cpu);
  }
?>
        </select>


this is part of the form and a dropdown menu generated from php.
the values of this dropdown menu are the 'IDno' field

and i have my print.php page which has a table to display the results.

in one cell of the table i need some code to display the price using the 'IDno' that was submited to this page

eg. i choose option 2 from the dropdown, and the 'IDno' value is 2...

now i need to use this to get any value from the mySQl datbase say the price...i need the code to get the price when IDno = 2

please help, btw the previous stuff hasnt returned the actual price
iLLuZZiOn
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Aug 02, 2002 5:59 pm

Postby DoppyNL » Thu Aug 08, 2002 3:45 am

I think both pieces of code below do exactly the same:
Code: Select all
<form name="ezyquote" method="post" action="print.php">
<select name="amd_cpu" id="amd_cpu">
          <?php
do { 
?>
          <option value="<?php echo $row_amd_cpu['ProductID']?>"><?php echo "($",$row_amd_cpu['UnitPrice'].") ",$row_amd_cpu['Product_Name'] ?></option>
          <?php
} while ($row_amd_cpu = mysql_fetch_assoc($amd_cpu));
  $rows = mysql_num_rows($amd_cpu);
  if($rows > 0) {
      mysql_data_seek($amd_cpu, 0);
     $row_amd_cpu = mysql_fetch_assoc($amd_cpu);
  }
?>
        </select>
Code: Select all
<form name="ezyquote" method="post" action="print.php">
<select name="amd_cpu" id="amd_cpu">
<?php
while ($row_amd_cpu = mysql_fetch_assoc($amd_cpu))
{
   print ('<option value="' . $row_amd_cpu['ProductID'] . '">' . '($' . $row_amd_cpu['UnitPrice'] . ')' . $row_amd_cpu['Product_Name'] . '</option>');
}
?>
</select>

although.... I may be wrong, but I think the first one doesn't work at all (or there must be some code before it that I can't see rightnow)


as for retrieving the info on the page to wich this form is posted to, you can use "$_POST['amd_cpu']" wich contains the id ($HTTP_POST_VARS for older versions of PHP)
you can build a simple select query with that one.

or didn't I get you're question?

Greetz Daan
DoppyNL
 

Postby iLLuZZiOn » Thu Aug 08, 2002 4:27 am

yes

"$_POST['amd_cpu']" gives me the IDno

but the query returns resourse id #x

or something like that

and when i try to use the array function, it outputs 'Array'
iLLuZZiOn
New php-forum User
New php-forum User
 
Posts: 9
Joined: Fri Aug 02, 2002 5:59 pm

Postby DoppyNL » Thu Aug 08, 2002 4:51 am

mysql_fetch_array returns an array with the values of the retrieved record like this:

$returned_value_from_mysql_fetch_array['fieldname_in_database'] == $value_in_database

read the manual for more details: (both functions are almost identical)
http://www.php.net/mysql_fetch_assoc
http://www.php.net/mysql_fetch_array

Greetz Daan
DoppyNL
 


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.