dannyone Posted March 17, 2009 Share Posted March 17, 2009 is it possible to select a value from a dynamically created dropdown menu, then once selected submit it and refresh the page and store that value as a session or variable. then be able to select a number of multidimensional checkboxes and store the options selected from the drop down box along with the selected checkboxes? i have a script that allows a user to select a value from the drop down and insert it into the database. eg. only inserts the ID Free Table; ID number day status 1 and i have the multidimensional arrays working and they store in the database correctly. Free Table; ID number day status 3 mon busy but i want to be able to insert all values together ie. table Free; ID number day status 1(dropdown value) 3 mon busy (all 3 arrays) can this be done i have really been struggling with this for ages now and im getting no were any help would be greatly appreciated thanks Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/ Share on other sites More sharing options...
Boo-urns Posted March 17, 2009 Share Posted March 17, 2009 From what I understand. You have the ID value as your dropdown value, and you want to pull the data for the (number, day, status) which might not always correlate to the ID? Best bet would be to look into javascript. Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-786649 Share on other sites More sharing options...
dannyone Posted March 17, 2009 Author Share Posted March 17, 2009 sorry i didnt make myself very clear in my previous post. can some1 help me extend the code i have, currently i am selecting the Room_ID, Name and building from a table called Rooms, and i want to store the selected value as a session so that i can use it with some checkboxes. here is the code i have, <?php $nt=$_POST['Room_ID']; if (!isset($_POST['submit'])) { ?> <html> <head> <title>Student Timetable</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> Select a Student:<br /> <?php //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $query=("SELECT Rooms.Room_ID,Rooms.RoomName,Rooms.Building FROM Rooms"); $result = mysql_query ($query); echo "<select name=Room_ID value=''>Room_ID</option>"; // printing the list box select command echo "<option>- Please Select Room -</option>\n"; while($nt=mysql_fetch_array($result)){ echo "<option value=$nt[Room_ID]>$nt[Room_ID], $nt[RoomName] $nt[building]</option>"; "<br />"; } echo "</select>";// Closing of list box ?> <input type="submit" value="submit" name="submit"> <br /><br /> </form> <? }else{ echo "Your are view the availability or Room ID ".$nt.":<br /><br /><br />"; } ?> all the code works fine but is there a way that i can save $nt as a sessions so that i can use it with some checkboxes once the form has been posted? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-786677 Share on other sites More sharing options...
kickstart Posted March 17, 2009 Share Posted March 17, 2009 Hi Sounds like something that could be done either by storing the ID fields as hidden fields on the forms you are sending back and forwards, or possibly something that would be a fair candidate for using Ajax. Using hidden fields would be easier and more reliable than sessions. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-786680 Share on other sites More sharing options...
dannyone Posted March 17, 2009 Author Share Posted March 17, 2009 thanks keith, sorry if this sounds thick but how would i use hidden fields in the dynamic drop down? its just the examples i have seen all use examples like <input type="hidden" name="hidfield" value="Cart ID "> but i am collecting my information from a database as the Room_ID can change due to adding/removing rooms etc.. thanks Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-786687 Share on other sites More sharing options...
kickstart Posted March 17, 2009 Share Posted March 17, 2009 Hi I presume that when they select a value from the drop down list they submit the form and the php script processes the form, putting out another drop down list / load of check boxes / other input fields to process the next stage. Just put that form out with an extra hidden field like <input type='hidden' name='FirstInputFieldValue' value='59' /> , putting whatever it is they selected into the value field. If I have got the wrong idea then sorry! It looks like $nt is the data for a particular room. I suppose technically there could be hundreds of rooms and so loads of data you want to store. You shouldn't really use sessions variables for that, but if you have stored the ID field for the room you can easily retrieve it the next time you process the form. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-786696 Share on other sites More sharing options...
dannyone Posted March 17, 2009 Author Share Posted March 17, 2009 yeah you got the right idea keith. i need a way for a user to be able to select a room from a drop down, then once the form has been posted, be able to select times that are free for that particular room using checkboxes. you helped me previously with the multidimensional arrays, that i am using again on this page to store the times selected. however if i post the value from the dropdown it stores in the database fine, but if i submit the checkbox arrays its just ignores the $nt['Room_ID'] and just stores the room as 0. can you show me how i would use the hidden field in the code below please, as i haven't got a clue how it would work <?php $nt=$_POST['Room_ID']; if (!isset($_POST['submit'])) { ?> <html> <head> <title>Student Timetable</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> Select a Student:<br /> <?php //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $query=("SELECT Rooms.Room_ID,Rooms.RoomName,Rooms.Building FROM Rooms"); $result = mysql_query ($query); echo "<select name=Room_ID value=''>Room_ID</option>"; // printing the list box select command echo "<option>- Please Select Room -</option>\n"; while($nt=mysql_fetch_array($result)){ echo "<option value=$nt[Room_ID]>$nt[Room_ID], $nt[RoomName] $nt[building]</option>"; "<br />"; } echo "</select>";// Closing of list box ?> <input type="submit" value="submit" name="submit"> <br /><br /> </form> <? }else{ echo "Your are view the availability or Room ID ".$nt.":<br /><br /><br />"; } ?> when i come to use the $nt / Room_ID later on to be stored in the database along with other information, would i use '$nt' or $_POST['Room_ID']? thanks for the help again Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-786706 Share on other sites More sharing options...
kickstart Posted March 17, 2009 Share Posted March 17, 2009 Hi Brief covering of what I think you need:- <?php $nt=$_POST['Room_ID']; if (!isset($_POST['submit'])) { ?> <html> <head> <title>Student Timetable</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> Select a Student:<br /> <?php //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $query=("SELECT Rooms.Room_ID,Rooms.RoomName,Rooms.Building FROM Rooms"); $result = mysql_query ($query); echo "<select name=Room_ID value=''>Room_ID</option>"; // printing the list box select command echo "<option>- Please Select Room -</option>\n"; while($nt=mysql_fetch_array($result)) { echo "<option value=$nt[Room_ID]>$nt[Room_ID], $nt[RoomName] $nt[building]</option><br />"; } echo "</select>";// Closing of list box ?> <input type="submit" value="submit" name="submit"> <br /><br /> </form> <? } else { if (!isset($_POST['submit2'])) { ?> <html> <head> <title>Student Timetable</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> Select a Student:<br /> <?php echo "<input type='hidden' name='Room_ID' value='$nt' />"; echo "Your are view the availability or Room ID ".$nt.":<br /><br /><br />"; echo "9:00-12:00 <input type='checkbox' name='Time1' value='1' /><br />"; echo "12:00-17:00 <input type='checkbox' name='Time2' value='1' /><br />"; ?> <input type="submit" value="submit2" name="submit2"> <br /><br /> </form> <? } else { // Process 2nd submit button } } ?> Basically you put the drop down list out for the user to select the room. When they hit the submit button it puts out the room id into a hidden field (which I have given the same name as the drop down list used earlier, hence the assignment of $nt will work) a list of time periods (I have hard coded them) and another select button (I have just put a 2 on the end of the name for now). When the 2nd submit button is pressed then it can process the checkboxes, having picked up the room id from the hidden field. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-786747 Share on other sites More sharing options...
dannyone Posted March 17, 2009 Author Share Posted March 17, 2009 thanks alot for your help keith. i have been trying to use your code but it will not work for me. it just shows a blank screen with no output. sorry to be a pain but could you please explain the 2nd half of your code again to me please from; } else { if (!isset($_POST['submit2'])) { ?> <html> <head> <title>Student Timetable</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> Select a Student:<br /> <?php echo "<input type='hidden' name='Room_ID' value='$nt' />"; echo "Your are view the availability or Room ID ".$nt.":<br /><br /><br />"; echo "9:00-12:00 <input type='checkbox' name='Time1' value='1' /><br />"; echo "12:00-17:00 <input type='checkbox' name='Time2' value='1' /><br />"; ?> <input type="submit" value="submit2" name="submit2"> <br /><br /> </form> <? } else { // Process 2nd submit button } } ?> hopefully i will understand php properly soon and be able to get things better thanks again Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-786930 Share on other sites More sharing options...
kickstart Posted March 17, 2009 Share Posted March 17, 2009 thanks alot for your help keith. i have been trying to use your code but it will not work for me. Sorry, my fault. Teach me for not thinking enough. This is better (but not test run so not sure there are no errors) <?php $nt=$_POST['Room_ID']; switch (true) { case (isset($_POST['submit']): ?> <html> <head> <title>Student Timetable</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> Select a Student:<br /> <?php echo "<input type='hidden' name='Room_ID' value='$nt' />"; echo "Your are view the availability or Room ID ".$nt.":<br /><br /><br />"; echo "9:00-12:00 <input type='checkbox' name='Time1' value='1' /><br />"; echo "12:00-17:00 <input type='checkbox' name='Time2' value='1' /><br />"; ?> <input type="submit" value="submit2" name="submit2"> <br /><br /> </form> <? break; case (isset($_POST['submit']): // Process 2nd submit button break; default : ?> <html> <head> <title>Student Timetable</title> </head> <body> <form method="post" action="<?php echo $PHP_SELF;?>"> Select a Student:<br /> <?php //Connect to mysql server $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $query=("SELECT Rooms.Room_ID,Rooms.RoomName,Rooms.Building FROM Rooms"); $result = mysql_query ($query); echo "<select name=Room_ID value=''>Room_ID</option>"; // printing the list box select command echo "<option>- Please Select Room -</option>\n"; while($nt=mysql_fetch_array($result)) { echo "<option value=$nt[Room_ID]>$nt[Room_ID], $nt[RoomName] $nt[building]</option><br />"; } echo "</select>";// Closing of list box ?> <input type="submit" value="submit" name="submit"> <br /><br /> </form> <? break; } ?> sorry to be a pain but could you please explain the 2nd half of your code again to me please from; The way the above works is that the first thing it does is se $nt to the passed variable. It then does a switch statement to see whether it is coming in having had a button pressed. First time in it should drop down to the default clause and put out the drop down select list. When a user selects something and presses submit the script starts again. This time it will go into the CASE statement for "submit" being pressed. Idea is that here you put out another form with the check boxes on it (not sure how you want to code these, I just put a pair of fixed ones to give you the idea), but also with a hidden field containing the Room_ID from the first form and another button, this time called "select2". 3rd time in it will go into the case statement for "select2" being pressed. In here you can process it as you want. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/149808-dynamic-dropdown-and-checkboxs/#findComment-787004 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.