uwictech Posted November 3, 2009 Share Posted November 3, 2009 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/ Share on other sites More sharing options...
taquitosensei Posted November 3, 2009 Share Posted November 3, 2009 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']:""; ?> Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950344 Share on other sites More sharing options...
uwictech Posted November 4, 2009 Author Share Posted November 4, 2009 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950837 Share on other sites More sharing options...
Bricktop Posted November 4, 2009 Share Posted November 4, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950846 Share on other sites More sharing options...
uwictech Posted November 4, 2009 Author Share Posted November 4, 2009 Hi Bricktop, there is most defiantly no need to apologise! It's kind of doing what I want. When I choose an option from the drop down it echo's that out rather than the ip address associated with it the database. Sorry to be a pain! Jamie Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950852 Share on other sites More sharing options...
Bricktop Posted November 4, 2009 Share Posted November 4, 2009 Hi Jamie, Just noticed you are using name= and not value= on your option boxes. Change the relevant code to read: echo"<option value='$ip'>$site</option> "; Otherwise the code I posted won't work. Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950853 Share on other sites More sharing options...
uwictech Posted November 4, 2009 Author Share Posted November 4, 2009 Bricktop, you are indeed a genius!! I hope someday I can help you in some way. I can't tell you how much I appreciate your help! Jamie Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950858 Share on other sites More sharing options...
Bricktop Posted November 4, 2009 Share Posted November 4, 2009 No problem, if I'm ever in Wales I'll look you up - you can buy me a beer Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950862 Share on other sites More sharing options...
uwictech Posted November 4, 2009 Author Share Posted November 4, 2009 no problem there mate! I think I owe you more than 1!! Can I ask one more question? Is it possible to store that ip address in a variable, so that I can use it in some code I about to write? J. Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950864 Share on other sites More sharing options...
Bricktop Posted November 4, 2009 Share Posted November 4, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950878 Share on other sites More sharing options...
uwictech Posted November 4, 2009 Author Share Posted November 4, 2009 bugga! I have just posted it onto the AJAX forum. And there is me thinking it must be easy! J. Quote Link to comment https://forums.phpfreaks.com/topic/180144-selecting-data-via-a-dropdown-box/#findComment-950906 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.