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
https://forums.phpfreaks.com/topic/150701-dynamic-checkbox-help/
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

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
}

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

try

print_r($_POST) 

at the top of your page to make sure it's being posted.

if it's not then make sure that your form is set to post and not get

<form action='whatever.php' method='post'>

double check to make sure your form opening and closing tags are in the right place

 

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.