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
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']:"";
?>

Link to comment
Share on other sites

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"; ?>

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.