Jump to content

Recommended Posts

Hi Guys, Im relitively new to php but I am currently trying to create a drop menu that will decide what contents are displayed on screen. so i need a drop menu and then when the user selects an option from the list an sql query uses the selection value to get details from the db,

The problem is I cant seem to get the value out of the list to use in the query. Here is my code so far, any suggestions much appreciated

 

<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post"> 
			<select name="siteselect">
				<? $result = mysql_query("SELECT site_name FROM communitypage");

				while($row = mysql_fetch_array($result))
				{
				?>												
				<option value="<? $row['site_name'] ?>"><? echo $row['site_name'] ?></option>
				<? } ?>
			</select>
			<input type="submit" border="0" name="reg" value="GO">

		</form>

 

and then I attempt to get the data out as below


if(isset($_POST["siteselect"]))
	{		
		echo "select :".$_POST["siteselect"];
		$result = mysql_query("SELECT * FROM communitypage WHERE site_name='richmond court'");

		while($row = mysql_fetch_array($result))
		{
			$site_name=$row[0]; // 42
			$uname=$row[1]; // 
			$site_details=$row[3]; // 
			$site_notice=$row[4]; // 
			$site_files=$row[5]; // 
		}

		?> <br /> <?
		echo $site_name;
		?> <br /> <?
		echo $site_details;

 

I was trying to make it so the user could just click on the selection but i think it may be easier to use a submit button.

 

Thanks a million in advance:)

 

Michael

Link to comment
https://forums.phpfreaks.com/topic/54193-solved-php-drop-list-value/
Share on other sites

You never actually use the posted variable within your query. eg;

 

$result = mysql_query("SELECT * FROM communitypage WHERE site_name='{$_POST['siteselect']}'");

 

appologies, I forgot to say I am just trying to print out the value on the previous line to that, cheers for looking:)

Did you print_r($_POST)?  Check the HTML source on the form to make sure the action is pointing to the correct script.  You're not echoing the value in the value="" section.

 

Easier to read:

<select name="siteselect">
<?php
$result = mysql_query("SELECT site_name FROM communitypage");
while($row = mysql_fetch_array($result))
	echo "<option value=\"$row[site_name]\">$row[site_name]</option>";
?>
</select>

Did you print_r($_POST)?  Check the HTML source on the form to make sure the action is pointing to the correct script.  You're not echoing the value in the value="" section.

 

Easier to read:

<select name="siteselect">
<?php
$result = mysql_query("SELECT site_name FROM communitypage");
while($row = mysql_fetch_array($result))
	echo "<option value=\"$row[site_name]\">$row[site_name]</option>";
?>
</select>

 

Cool, I have never used that print_r before but this is what it displays

 

Array ( [siteselect] => [reg] => GO )

 

How would I echo the value?

 

Thanks

example.

<select name="siteselect">
<?php

$row[]="i";
$row[]="love";
$row[]="php";

foreach($row as $r){

echo "<option value='$r'>$r</option>";

}
?>
</select>

 

edited

 

<select name="siteselect">
<?php
$result = mysql_query("SELECT site_name FROM communitypage");
while($row = mysql_fetch_array($result))
	echo "<option value='$row[site_name]'>$row[site_name]</option>";
?>
</select>

The loop is left open, maybe that is the problem ??? In order to simply click the drop down box you will need to use AJAX else you will need a submit button.

 

<select name="siteselect">
<?php
$result = mysql_query("SELECT site_name FROM communitypage");
while($row = mysql_fetch_array($result)){
	echo "<option value=\"$row[site_name]\">$row[site_name]</option>";
            }
?>
</select>

The loop is left open, maybe that is the problem ??? In order to simply click the drop down box you will need to use AJAX else you will need a submit button.

 

<select name="siteselect">
<?php
$result = mysql_query("SELECT site_name FROM communitypage");
while($row = mysql_fetch_array($result)){
	echo "<option value=\"$row[site_name]\">$row[site_name]</option>";
             }
?>
</select>

 

Thank you so much guys, it is working now, cheers for all the advice.

 

monty ;D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.