Jump to content

Selecting data via a dropdown box


uwictech

Recommended Posts

Hi all,

 

I'm having a brain freeze, hope you can help.

 

I have created a simple drop down box that populates from a MySQL database. When I choose one of the options from the drop down box, I would like to print on screen a value associated with it in the database. 

 

So when I choose a specific site, it will show the ip address. my code is below, hope you can help.

 

Jamie

 

 

<?php

	require("confwifi.php");

    
$extract = mysql_query ("SELECT * FROM site");
$numrows = mysql_num_rows ($extract);

echo"Select CAMPUS: <select name='site'>";

while ($row = mysql_fetch_assoc($extract))
{
$id  = $row['id'];
$site = $row['site'];
$ip = $row['ip'];

echo"<option name='$ip'>$site</option> ";


}

echo "</select>";


?>

<?php echo $ip; ?>

Link to comment
https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/
Share on other sites

what you're trying to do can't be done without javascript.    You would need to use javascript to submit the form onChange      then check for $_POST and do whatever you need to do.

 

      require("confwifi.php");

    
$extract = mysql_query ("SELECT * FROM site");
$numrows = mysql_num_rows ($extract);

echo"Select CAMPUS: <select name='site' onChange='this.form.submit();'>";

while ($row = mysql_fetch_assoc($extract))
{
   $id  = $row['id'];
   $site = $row['site'];
   $ip = $row['ip'];
   
echo"<option name='$ip'>$site</option> ";


}

echo "</select>";

echo ($_POST['site'])?$_POST['site']:"";
?>

Thanks taquitosensei.

 

I don't think I explained myself properly.

 

What I would like is to do is, store what the user selects from the dropdown boxes into a variable to use later. Is that possible?

 

Sorry for the confusion.

 

Jamie

 

 

<?php

	require("confwifi.php");

$ip_form = $_POST['ip'];

$extract = mysql_query ("SELECT * FROM site");
$numrows = mysql_num_rows ($extract);

echo"Select CAMPUS: <select name='ip'>";

while ($row = mysql_fetch_assoc($extract))
{
$id  = $row['id'];
$site = $row['site'];
$ip = $row['ip'];

echo"<option name='$ip'>$site</option> ";



}

echo "</select>";


?>

<?php echo "$variable"; ?>

 

Hi Jamie,

 

I got your PM - sorry for the delay ;)

 

OK, a simple bit of javascript should do what you need.  Add the following to the HTML of the page your PHP code runs from:

 

<script type="text/javascript">
function showSelected(val){
	document.getElementById
('selectedResult').innerHTML = "The IP address is - " 
+ val;
}
</script>

 

Now, amend your PHP code to read:

 

<?php

	require("confwifi.php");

$ip_form = $_POST['ip'];

$extract = mysql_query ("SELECT * FROM site");
$numrows = mysql_num_rows ($extract);

echo"Select CAMPUS: <select name='ip' onChange='showSelected(this.value)'>";

while ($row = mysql_fetch_assoc($extract))
{
$id  = $row['id'];
$site = $row['site'];
$ip = $row['ip'];

echo"<option name='$ip'>$site</option> ";



}

echo "</select>";

echo "<div id='selectedResult'></div>";

?>

<?php echo "$variable"; ?>

 

The above code will display the IP address when a user selects an option from the dropdown box.

 

Hope this helps.

Hi Jamie,

 

I'm sure it's possible but beyond my scope of knowledge as it will need to be a relatively complex Javascript/AJAX call - because Javascript runs on the client and PHP runs on the server, and neither have direct access to each other.

 

I would recommend you post your code in the AJAX help forum and hopefully one of the experts there will give you an answer.

 

Thanks

Archived

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

×
×
  • 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.