Jump to content

PHP and database retrieval with drop down box


redsent

Recommended Posts

I have a php page where i would like a drop down box populated with items from the database. When a certain item from a data base is selected in the drop down box the corresponding values from that line of the database appear in a table below it. The code i have so far is as follows: The drop down box works fine, but when i add in the rest of the code after the "echo '</select>';" line it doesn't work. Like wise if i remove the drop down box and use a select all and remove "AND program_name = $va" the page works fine. Just can't get it to work together. Any thoughs would be appreciated, Cheers.

<?php$dbselect = ("dbname");$con = mysql_connect("localhost","username","password");mysql_set_charset('utf8',$con);if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db($dbselect, $con);$QuerySelects = "SELECT program_name FROM program_names";$Query = mysql_query($QuerySelects) or die (mysql_errno() . ": " . mysql_error(). "\n");echo '<label>Select Store:</label>';echo '<select id="program_name"  name="program_name">';echo '<option value="">Select Store</option>';while ($row = mysql_fetch_assoc($Query)){$va = $row['program_name'];echo "<option value='$va'>$va</option>";}echo '</select>';$QuerySelects1 = "SELECT * FROM offers1 WHERE end_date>CURDATE() AND program_name = $va";$Query1 = mysql_query($QuerySelects1) or die (mysql_errno() . ": " . mysql_error(). "\n");include ('variables.php');while($result=mysql_fetch_assoc($Query1)) {echo"<div class='spacerbox'><div class='outerbox eviecodes'><div><div class='topbox'>  <div class='leftbox'>   <div class='offerimage'>    <div class='progdiv'><a class='progname' href=".$url." target='_blank'>".$program_name."</a></div></div>  </div>  <div class='rightbox'><div class='descbox boxwidth'><h1>   <a href=".$url." target='_blank'>".$description." at ".$program_name."</a></h1></div>  <div class='voubox boxwidth'><h2 class='vvv'>Voucher Code:<span class='vcode'>".$code."</span ></h2></div> </div></div><div class='linkbox boxwidthl'>   <a href=".$url." target='_blank'>To Take Advantage of this offer at <span class='prodname'>".$program_name."</span>, click here!</a>  </div><div class='expires'><span class='end'>Expires:</span> <span>".$dateformat."</span ></div><div class='socialbox'>{module Tell A Friend Module}</div></div><div class='spacer'></div></div></div>";}?>

Link to comment
Share on other sites

what is not working as you intended? is there any error?

Link to comment
Share on other sites

I can't get it to use the item that is selceted in the drop down box as the variable $va in the bit where it then queries the data base to retrieve only the line in the able that match the program_name with vaiable $va. variable $va would be the result of the drop down box

echo '<label>Select Store:</label>';echo '<select id="program_name"  name="program_name">';echo '<option value="va">Select Store</option>'; while ($row = mysql_fetch_assoc($Query)){$va = $row['program_name'];echo "<option value=''>$va</option>";}echo '</select>'; $QuerySelects1 = "SELECT * FROM offers1 WHERE end_date>CURDATE() AND program_name = '$va'";$Query1 = mysql_query($QuerySelects1) or die (mysql_errno() . ": " . mysql_error(). "\n");

Link to comment
Share on other sites

you need to set its value too

echo "<option value='$val'>$va</option>";

Link to comment
Share on other sites

yes it will evaluate its value in option so thus in drop down and you can use that when you will submit the form. it will be avaliable in $_GET or $_POST superglobal array depending on your form's method attribute.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...