How to pass this dropdown to another PHP page

Codes here !

Moderators: macek, egami, gesf

jamesflowers
New php-forum User
New php-forum User
Posts: 1
Joined: Sat Sep 29, 2012 10:46 am
Location: London , UK
Contact:

How to pass this dropdown to another PHP page

Postby jamesflowers » Sat Sep 29, 2012 3:00 pm

this creates a dropdown list returning my Customer Names from customers, any help on how I can select a name from the list and post it to another PHP page passing that name to a query would be great.

thanks

James

<?php
$link = mysql_connect('*****') or die('Could not connect: ' . mysql_error());
mysql_select_db('***') or die('Could not select database');
$query = "select customerName from customers order by customerName";
$result = mysql_query($query) or die(mysql_error());
$dropdown = "<select name='users'>";
while($row = mysql_fetch_assoc($result)) {$dropdown .= "\r\n<option value='{$row['customerName']}'>{$row['customerName']}</option>";}
$dropdown .= "\r\n</select>";
echo $dropdown;
?>

rwhite35
New php-forum User
New php-forum User
Posts: 13
Joined: Thu Sep 27, 2012 10:55 am

Re: How to pass this dropdown to another PHP page

Postby rwhite35 » Tue Oct 02, 2012 5:28 pm

I think you would want something like the following. Also, in your table customer, you should have a customer id. The option value would be the customers id and the string to select would be the customer name.

Code: Select all

<php
 /* up here is the query. Should use prepared statements instead of mysql_* functions.*/
// query returns a resource with the user name and id that is then assigned to an array named $actives
// using mysqli and object oriented style here
while ($row = $result->fetch_assoc()) {
 $actives[] = $row;
}
// duck out of PHP and continue with HTML form
?>
 <?php
   //back in to PHP
   if (isset($actives)){
     echo "<select name=\"users\">";
     foreach ($actives as $value){ 
       printf('<option value = %s > %s </option>',$value['customerID'],$value['customerName']);
     }
     echo "</select>";
}
?>
</select>


This will produce one menu with each of your user names as menu options. Then you could either assign the selection to a session variable or pass it to another script as a $_POST['users'] super global array. Say the user selects John Doe with an id of 21. $_POST['users'] would have the value 21.
Good Luck


Return to “mySQL & php coding”

Who is online

Users browsing this forum: No registered users and 2 guests