Reputation: 394
I have a dynamic page that gets a certain number of results for rooms from Mysql and echos them out in a form as a checkbox. Since I do not have a certain number in each result I am not sure how many checkboxes to check. Anyway here is what a sample code from the form looks like:
$rr = 1;
while($ro = mysql_fetch_array($getRooms)){
$roomName = $ro['HotelRoom'];
?>
<input type="checkbox" name="room<?php echo $rr ?>" value="<?php echo $roomName ?>" /><?php echo $roomName ?>
<?php
$rr++;
}
Now when I submit the form, I tried to use a function to get the results but I get nothing back. Here is my function to receive the data:
//Get all rooms
$nn = 1;
$retn = '';
function roomGroup(){
foreach ($_POST['room'.$nn] as $key => $value) {
$retn .= $value.",";
}
$nn++;
return $retn;
}
$rooms = roomGroup();
I get nothing from the $rooms variable. I also tried this as a function:
$nn = 1;
$retn = '';
function roomGroup(){
while(isset($_POST['room'.$nn]))
{
$roomsn = $_POST['room'.$nn];
$retn .= $roomsn.",";
$nn++;
}
return $retn;
}
Any help on what I am doing wrong would be GREATLY appreciated!!!
Upvotes: 0
Views: 2115
Reputation: 3468
If you're intent on doing it the (sloppy, hard) way you're doing it:
function roomGroup() {
$retn = '';
foreach($_POST as $key => $value) {
if (strstr($key,"room") !== false) {
$retn .= $value.",";
}
}
return $retn;
}
Upvotes: 0
Reputation: 1056
You could loop on $_POST array :
$rooms = "";
$first = true;
foreach($_POST as $key => $value) {
if(strlen($key) >= 4 && substr($key,0,4) == "room") {
if($first) {
$rooms .= $value;
$first = false;
}
else
$rooms .= ",".$value;
}
}
Upvotes: 3
Reputation: 5555
You seem to be having issues with variables' scopes. $retn is defined both outside and inside your function but if you want to access the outside one from inside the function you have to use the $GLOBALS
array:
$GLOBALS['retn'] .= $roomsn.",";
instead of:
$retn .= $roomsn.",";
Or, actually better. Use local variables only (globals aren't very good practice in this case).
Upvotes: 0
Reputation: 1792
You are doing this the hard way. =)
You can use the HTML to make an array for you by adding brackets to the end of the input element name.
while($ro = mysql_fetch_array($getRooms)){
$roomName = $ro['HotelRoom'];
echo '<input type="checkbox" name="room[]" value="'.$roomName.'" />'.$roomName.'<br />';
}
Now, on your logic side, you access the array. The rooms checkbox will be passed as an array within the POST variable. You need to check to make sure that you passed at least some rooms. If you passed rooms, just do:
implode( ", ", $_POST['rooms'] );
Also if it would be more convenient for you, you could also make the name="room[]" into name="room['.$ro['HotelRoom'].']" if all your HotelRoom's are unique. Then you could set the value to be the id of the Hotel Room. Then, instead of doing implode on the logic side, you could do a foreach loop like you were doing to still have the key value pairs.
foreach( $_POST['room'] as $hotelRoom => $roomId ){
Upvotes: 1