Page 1 of 1

using dynamic dropdown selected value

Posted: Mon Mar 16, 2009 3:40 pm
by dannyone
i have this code below which is cut down to show the main parts.
i am selecting the room-id from mysql table Rooms, and displaying them using a dynamic drop down menu.
then i want to be able to select when that room is free and store it in a table Rooms-Free using the room-id selected.
i have all the arrays working fine but i can not use the selected value from the drop down. it stores in the database as room-id is 0.
i assume i will need to make rooms.room-id = rooms-free.room-id but im not sure were. can some1 have a quick look and see how i can use the selected value. the value is stored as $nt as u will see from the code.

Code: Select all

 
<?php
$nt = $_POST["Room_ID"];
if (!isset($_POST['submit'])) { 
?>
<form method="post" action="<?php echo $PHP_SELF;?>">
Select a Student:<br />
<?php
//connects to db here
$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 post 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 />"; // this is echoing the room so $nt is working here
}
?>
<?php
$WhatToInsert = array();
$WhatToInsert['Monday910'] = array('\'9-10\',\'mon\',\'Free\'');
$WhatToInsert['Monday1011'] = array('\'10-11\',\'mon\',\'Free\'');
// there are more arrays but these are working
$sql1 = "replace Into Rooms_Free (Room_ID, timeslot, day, status) Values ";
$sql2 = "";
foreach ($WhatToInsert as $key => $value)
{
  if (isset($_REQUEST[$key]))
  {
    foreach ($WhatToInsert[$key] as $value2)
    {
       $sql2 .= (($sql2) ? ',(' : '(')."'$nt',$value2)";  //here is were i am stuck i need to use the selecte value from the dropdown to work with this $nt
        
    }
  }
}
$sql = (($sql2) ? $sql1.$sql2 : "");
if(!!$sql) {
echo "<br/><FONT COLOR = RED>Your Availability has been set Thank You</FONT COLOR>";
}
$conn = mysql_connect("localhost", "danny", "danny") or die ('Error connecting to mysql database!');
mysql_select_db("tutorial");
$query=mysql_query($sql);
?>
 
thanks for any help

Re: using dynamic dropdown selected value

Posted: Mon Mar 16, 2009 4:53 pm
by socket1
You have to specify the name of the value that the option is being set to:

Code: Select all

echo "<option name=\"room-id\">- Please Select Room -</option>\n";
the <option value=""> is the same as like <input type="text" name="email" />

As for the other problem you are having..... I don't know.

Re: using dynamic dropdown selected value

Posted: Mon Mar 16, 2009 5:00 pm
by dannyone
thank you for your reply socket1, but i tried ur method and it didnt work, it just gives the same output.

i think i need to define Rooms.Room_ID = Rooms_Free.Room_ID and Rooms_Free.Room_ID = '$nt'

but i dont know where to do this as i have never used the insert into query before.

can you help me out

Thanks

Re: using dynamic dropdown selected value

Posted: Mon Mar 16, 2009 5:05 pm
by socket1
Nevermind my previous post...... and this one..... I missed a section of your code.


But I do think your issue might be with this code:

Code: Select all

echo "<option post value=$nt[Room_ID]>$nt[Room_ID], $nt[RoomName] $nt[Building]</option>";
I believe it should look like:

Code: Select all

echo "<option value=\"$nt[Room_ID]\">$nt[Room_ID], $nt[RoomName] $nt[Building]</option>";
You were missing quotes around the room ID value which is most likely why it kept coming up as 0.