kunal
kunal

Reputation: 3

Dropdown menu not displaying change when I change product

I want to access the value selected by user for further processing.

Hence I am using post method to pass the values of whole form. But GET to access cust_id so that I can reflect change in further parts of my form. Hence I had to post the following line:

<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'>

outside php code. But now, once I select some option from dropdown menu, URL changes accordingly, but dropdown menu does not reflects the change

<?php
  $query = "SELECT Cust_id, Cust_Name, Cust_City FROM Customers";
  $result = mysql_query($query);
?>

<select id='fullname' onChange="window.location='sp_menu.php?product='+this.value" name='fullname'>

<?php
  while($row = mysql_fetch_assoc($result)) { 
      echo '<option value="'.$row['Cust_id'].'">'.$row['Cust_Name'].','.$row['Cust_City'].'</option>';   
  }
  echo '</select>';
?>

How can I, in the same form, access the address of the particular customer id from database when user selects customer name from this dropdown menu?

Upvotes: 0

Views: 190

Answers (1)

AyB
AyB

Reputation: 11665

I think you mean when you change dropdown, the value is not retained, it obviously won't be because your page is being refresh, you need to GET the value from url and put a selected attribute to have that value selected.

Do it this way:

<?php
 $query = "SELECT Cust_id,Cust_Name,Cust_City FROM Customers" ;
 $result = mysql_query($query); 
 //checking if GET variable is set, if yes, get the value
 $selected_option = (isset($_GET['product'])) ? $_GET['product'] : '';
 //we will store all the dropdown html code in a variable and display it later
 $select = "<select id='fullname' onChange=\"window.location='sp_menu.php?product='+this.value\"   name='fullname'>";
 while($row = mysql_fetch_assoc( $result )) {
 //checking if the cust_id matches the GET value,
 //if yes, add a selected attribute
 $selected = ($selected_option==$row['Cust_id'])?'selected':''; 
 echo '<option value="'.$row['Cust_id'].'"'. $selected. '>' . $row['Cust_Name'] .' , '.$row['Cust_City'].    '</option>';   
 }
 $select .= '</select>';
 //display the dropdown
 echo $select;
?>

Upvotes: 1

Related Questions