Jump to content

dynamic checkbox help


dannyone

Recommended Posts

hello everyone i am creating a list of dynamic checkboxes that are been pulled from my mysql db. i can get them to display correctly, but i want to simply echo the value that the user has selected. but atm its not displaying anything. can some1 help please, heres my code

 

<?php
require_once('auth1.php');
require_once('config.php');
?>
<html>

<form method="post" action="<?php echo $PHP_SELF;?>">
<?php
$room = $_POST['room'];
mysql_connect("localhost", "xx", "xx") or die(mysql_error());
mysql_select_db("xx") or die(mysql_error());

//$query=("select * from categories");
$result = mysql_query("SELECT * FROM Rooms") 
or die(mysql_error());  


while($row=mysql_fetch_array($result)){ 

echo "<input type='checkbox' value='' name='room'
/>$row[Room_ID]<br/>     ";


}
// this will display all checkboxes w/  values in db.
echo $_POST['room'];

?>

<input type="submit" value="Select Availability" name="submit">
</form>


</html>

 

on submit i would like the rooms they have selected just to be echo'd on the screen

 

thanks again

Link to comment
Share on other sites

If you change your checkboxes to this

echo "<input type='checkbox' value='' name='room[]'
/>$row[Room_ID]<br/>     ";

then it will be an array so you can do this

foreach($_POST['room'] as $room) 
{ 
        echo $name; 
}

hello everyone i am creating a list of dynamic checkboxes that are been pulled from my mysql db. i can get them to display correctly, but i want to simply echo the value that the user has selected. but atm its not displaying anything. can some1 help please, heres my code

 

<?php
require_once('auth1.php');
require_once('config.php');
?>
<html>

<form method="post" action="<?php echo $PHP_SELF;?>">
<?php
$room = $_POST['room'];
mysql_connect("localhost", "xx", "xx") or die(mysql_error());
mysql_select_db("xx") or die(mysql_error());

//$query=("select * from categories");
$result = mysql_query("SELECT * FROM Rooms") 
or die(mysql_error());  


while($row=mysql_fetch_array($result)){ 

echo "<input type='checkbox' value='' name='room'
/>$row[Room_ID]<br/>     ";


}
// this will display all checkboxes w/  values in db.
echo $_POST['room'];

?>

<input type="submit" value="Select Availability" name="submit">
</form>


</html>

 

on submit i would like the rooms they have selected just to be echo'd on the screen

 

thanks again

Link to comment
Share on other sites

echo '<input type="checkbox" value="'.$row['Room_ID'].'" name="room'.$row['Room_ID'].'"
/>'.$row[Room_ID].'<br/>     ';

 

This will build all your checkboxes with unique names.

 

Then to check something like this:

if ($_POST['room'.$row['Room_ID']]) {
  //checkbox was selected
}

Link to comment
Share on other sites

thanks everyone for the replys

 

i have tried using your answer taquitosensel but it still doesn't echo anything.

 

and i have also tried yesideez solution but that just gives me a blank screen?

 

is there anything else i could try?

 

thanks again for the help guys its must appreciated!

 

thanks

Link to comment
Share on other sites

iv been working on this for a bit now since the last comment and iv got a little further. when i echo the value now it always shows the last value that is been called from the mysql table.

can some1 help and just see were im going wrong please, im really stuck

 

 

<form method="post" action="<?php echo $PHP_SELF;?>">
<body>

<?php
mysql_connect("localhost", "xx", "xx") or die(mysql_error());
echo "Connected to MySQL<br />";
mysql_select_db("xx") or die(mysql_error());
"<br/>";
echo "Connected to Database";
"<br>";
"<br>";
$query=("select Room_ID, RoomName from Rooms ORDER BY Room_ID");

$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); 

while($row=mysql_fetch_array($result)){ 
$category = $row["Room_ID"];

"<br/>";
"<br/>";
echo "<input type=\"checkbox\" name=\"category[]\" value=\"\"> $category:";
echo "<br>";
}
echo "".$category."";
?>

<input type="submit" value="submit" name="submit">
<br /> <br />
</form>
</html>

 

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.