4 Replies - 1434 Views - Last Post: 20 March 2012 - 12:35 PM Rate Topic: -----

#1 redsent  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 19-March 12

PHP and database retrieval with drop down box

Posted 19 March 2012 - 08:11 AM

I can't get it to use the item that is selected 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 variable $va. variable $va would be the result of the drop down box.

Below is the code i have so far, any help would be appreciated.

Thanks Jane x

<?php
include ('dbConn.php');

mysql_select_db($dbselect, $con);

$Query = mysql_query("SELECT program_name FROM program_names") or die (mysql_errno() . ": " . mysql_error(). "\n");

echo '<label>Select Store:</label>';
echo '<select id="program_name"  name="program_name">';
echo '<option value="View All">View All</option>';
 
while ($row = mysql_fetch_assoc($Query))
{
$va = $row['program_name'];
echo "<option value='$va'>$va</option>";
}
echo '</select>';


$Query = mysql_query("SELECT * FROM offers1 WHERE program_name = '$va'") or die(mysql_error());

while($result=mysql_fetch_assoc($Query)) {
include ('variables.php');
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>
";
}
?>


Is This A Good Question/Topic? 1
  • +

Replies To: PHP and database retrieval with drop down box

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,237
  • Joined: 08-June 10

Re: PHP and database retrieval with drop down box

Posted 19 March 2012 - 09:02 AM

the problem is that you confuse server and client side actions. PHP solely runs on the server and HTML/Javascript actions (like selecting from a dropdown) only occur on the client. i.e. when you can select something from the dropdown, PHP has long finished its job.

you would need to make a new Request with the selected value to a (different) PHP script that receives the value and outputs new text code for sending to the browser.
Was This Post Helpful? 0
  • +
  • -

#3 rpgmaker  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 224
  • Joined: 02-October 11

Re: PHP and database retrieval with drop down box

Posted 19 March 2012 - 11:09 AM

so you want to do a mysql select and show the results in a html drop down ?
Was This Post Helpful? 1
  • +
  • -

#4 jonesa01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 125
  • Joined: 18-February 07

Re: PHP and database retrieval with drop down box

Posted 20 March 2012 - 09:34 AM

Ok as Dormilich says the dropdown box is browser side and the php code is server side.

Now as far as I can see there are two options create two files and use jquery to load the second page once a value is selected or have it as a form the user selects the option and then sends post data to the other page.

Either way you would need to split the script into two.

Part 1

<?php
include ('dbConn.php');

mysql_select_db($dbselect, $con);

$Query = mysql_query("SELECT program_name FROM program_names") or die (mysql_errno() . ": " . mysql_error(). "\n");

echo '<label>Select Store:</label>';
echo '<select id="program_name"  name="program_name">';
echo '<option value="View All">View All</option>';
 
while ($row = mysql_fetch_assoc($Query))
{
$va = $row['program_name'];
echo "<option value='$va'>$va</option>";
}
echo '</select>';
?>



Part 2

include ('dbConn.php');

mysql_select_db($dbselect, $con);
$Query = mysql_query("SELECT * FROM offers1 WHERE program_name = '$va'") or die(mysql_error());

while($result=mysql_fetch_assoc($Query)) {
include ('variables.php');
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>
";
}
?>



I hope this points you in the right direction
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2992
  • Posts: 10,337
  • Joined: 08-August 08

Re: PHP and database retrieval with drop down box

Posted 20 March 2012 - 12:35 PM

If the number of options is low enough it might be worthwhile generating all possible menus and passing them to Javascript. Letting the browser deal with user actions could reduce server load.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1