$_POST checkbox array only returns single value - php

The foreach loop at the end with $_POST['clients'] only returns one value. Whats wrong?
<?php $clients_to_display = Client::find_all(); ?>
<p><?php foreach ($clients_to_display as $key) {
echo $key->name; ?>:<input type='checkbox' name='clients[]' value=<?php $key->name; ?></><br/>
<?php } ?></p>
if(isset($_POST['submit'])){
$job->name = $_POST["job_name"];
$job->description = $_POST["job_description"];
$job->type = $_POST["job_type"];
$job->age = $_POST["job_age"];
foreach ($_POST['clients'] as $key) {
echo $key;
}
}

This code does nothing:
<?php $key->name; ?>
I think you want:
<?php echo($key->name); ?>
Also, it has to be enclosed in quotes:
value="<?php echo($key->name); ?>"
Also the markup is invalid. So, the full line should be:
echo($key->name); ?>:<input type="checkbox" name="clients[]" value="<?php echo($key->name); ?>" /><br/>

Related

Problems when using nested foreach on checkboxes loaded dynamically

I need to use nested foreach for dependent checkboxes.
<input type="checkbox" name="all[]" value="<?php echo $row_bus_details['busid'];?>" >
<?php
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
?>
<input type="checkbox" name="bookingside[]" value="<?php echo $book_side_row['advt_side_id']; ?>" id="<?php echo $book_side_row['advt_side']; ?><?php echo $row_bus_details['busid'];?>" > <?php echo $book_side_row['advt_side']; ?><br/>
<?php } ?>
I need to loop the selected values of second checkbox if the first checkbox is selected.
I wrote the code like
$i = 0;
$busid = isset($_POST['all']) ? $_POST['all'] : array();
foreach ((array)$busid as $item) {
if(!empty($_POST['bookingside'])) {
foreach($_POST['bookingside'] as $side) {
$sql_book_side=mysqli_query($db,"INSERT INTO `advt_book_side`(bus_id,sides_id) VALUES ('$item','$side')");
$i++;
}
}
}
The result I need is just like the image below
You need to save data in serialize array from in data base like:
$sql_book_side=mysqli_query($db,"INSERT INTO advt_book_side(bus_id,sides_id) VALUES ('$item',serialize(array('left'=>1,'right'=>1,'back'=>0)))");
Print check box with check uncheck using below code
$book_side_result = mysqli_query($db,"select * from advt_sides");
while($book_side_row=mysqli_fetch_array($book_side_result))
{
$array = unserialize($book_side_row['sides_id']);
foreach($array[0] as $side){
?>
<input type="checkbox" name="bookingside[]" value="<?php echo ($side)? $side:0; ?>">
<?php }
} ?>

Comparing GET Array Values to Recordset Loop Values

I am having no luck trying to compare values in an array passed as a GET to values in a database recordset do/while loop. I'm attempting to make a checkbox checked if any value in the GET array matches a recordset id. It works if you use only one interest id in the URL without a comma. Thank you.
My URLs are:
interestSearch.php?interests=3 (this works)
interestSearch.php?interests=3,8 (doesn't work)
<? require('db.php');
mysql_select_db($database_data);
$query_allInterests = "SELECT * FROM interests";
$allInterests = mysql_query($query_allInterests, $data) or die(mysql_error());
$row_allInterests = mysql_fetch_assoc($allInterests);
$totalRows_allInterests = mysql_num_rows($allInterests);
?>
<form method="get">
<? do { ?>
<input <?
if(isset($_GET['interests']) && $_GET['interests'] != "") {
$theCounter = 0;
$theArray = array($_GET['interests']);
foreach ($theArray as $value) {
// if ($value == $row_allInterests['id']) {$theCounter++;}
if (in_array($row_allInterests['id'], $theArray)) {$theCounter++;}
}
if($theCounter > 0){echo "checked";}
}
?> name="<? echo $row_allInterests['id']; ?>" class="doCheck" type="checkbox" id="<? echo $row_allInterests['id']; ?>" value="<? echo $row_allInterests['id']; ?>" /> <? echo $row_allInterests['interest']; ?><br />
<? } while ($row_allInterests = mysql_fetch_assoc($allInterests)); ?>
</form>
Instead of
$theArray = array($_GET['interests']);
Use
$theArray = explode( ',', $_GET['interests']);

why does my code echo my post variables but still give me Invalid argument supplied for foreach() warnings?

I get two warnings Warning: Invalid argument supplied for foreach() but I still retrieve my expected post variables. Where am I going wrong? It says the warning is in regard to line 7, which in this case is the line that begins; foreach($value as $k => $v)
<!------------- quote.php ----------------->
<body>
What services are you interested in? <br/><br/>
<form name="input" action="quote2.php" method="post">
<?php
$services = array('Tree Felling', 'Height Reduction', 'Crown Thinning', 'Deadwooding/Ivy Removal', 'Stump Grinding', 'Other');
foreach ($services as $option) { ?>
<input id="<?= $option ?>" type="checkbox" name="services[]" value="<?= $option ?>" />
<label for="<?= $option ?>"><?= $option ?></label>
<br />
<? }
?>
<br/>
<input name="name" type="text" />NAME</br>
<input name="place" type="text"/>TOWN</br/>
<input type="submit" value="Submit">
</form>
</body>
<!------------ quote2.php -------------->
<?php
echo '<h3>SERVICES REQUIRED</h3>';
foreach ($_POST as $key => $value) {
foreach($value as $k => $v)
{
echo '<p>'.$v.'</p>';
}
}
echo "<hr /><h3>DETAILS</h3>";
echo $name = $_POST['name'];
echo "</br>";
echo $place = $_POST['place'];
echo "<hr/>"
?>
A number of the form controls do not have names that end in [] and so are not arrays.
You can't loop over a string.
You should pull out each value of the submitted data individually and only loop over services.
The problem is that there is a very real possibility that not all of the items in your $_POST array will have values of type array; which you are assuming according to the code in quote2.php
A simple is_array() check will ensure that only arrays get iterated over in the foreach, here is the edited file contents:
<!------------ quote2.php -------------->
<?php
echo '<h3>SERVICES REQUIRED</h3>';
foreach ($_POST as $key => $value) {
if (is_array($value)) {
foreach($value as $k => $v)
{
echo '<p>'.$v.'</p>';
}
}
}
echo "<hr /><h3>DETAILS</h3>";
echo $name = $_POST['name'];
echo "</br>";
echo $place = $_POST['place'];
echo "<hr/>"
?>
That should do the trick.
Try wrapping your foreach loop in an if statement to check the array is an array. For example:
if (is_array($services)) {
foreach ($services as $option) {
// Some code
}
}
Although I wouldn't worry too much. This is just a cleaner way of writing your code.

Trying to create an array condition

I have a text field which the user can enter comma separated list and then php converts it to a drop select list however I want a condition that will show a text input field if only a single value is entered. I tried the code below but it is only returning the select box even with a single entry. How can I achieve this condition?
<?php $listval = explode(",",$vals);
if(is_array($listval)) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
explode will always return an array. So therefor is_array will always be true.
Change your if statement to this
if(sizeof($listval) > 1)
try This
<?php $listval = strpos(',',$vals)?explode(",",$vals):$vals;
if(is_array($listval)) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
Please try to use the follwing code:
<?php $listval = explode(",",$vals);
if(count($listval) > 1) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php } elseif(count($listval) == 1) { ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>
As #Kris indicated, you can check with,
count($array)
or
sizeof($array)
<?php $listval = explode(",",$vals);
$array_count = count($listval);
if($array_count > 1) { ?>
<select name="valuelist">
<?php
foreach($listval as $value) {
echo '<option>'.$value.'</option>';
} ?>
</select>
<?php }else{ ?>
<input type="text" size="10" name="valuelist" value="<?php echo $vals; ?>" />
<?php } ?>

keep assoc array checkbox checked

I'm trying to keep a list of checkboxes checked until post is valid and no errors. Below is the code I'm using. I would be thankful for any help.
<?php foreach ($drinks_checkbox as $option => $options){ ?>
<input type='checkbox' id='drinks[]' name='drinks[]' value='<?php echo $option;?>' <?php if(!empty($_POST['drinks'])){if($_POST['drinks']==$option){ echo "checked='checked'" ; }}?> /><?php echo $options;?><br />
<?php } ?>
I can successfully display the checked checkboxes using implode however I need help for the above..
$_POST['drinks'] is an array. Also, the id value does not need to be 'drinks[]'
Try something like this:
$drinksIndex = 0;
$drinksPost = $_POST['drinks'];
foreach ($drinks_checkbox as $option => $options){ ?>
<input type='checkbox' id='drinks<?php echo $drinksIndex; ?>' name='drinks[<?php echo $drinksIndex; ?>]' value='<?php echo $option;?>'<?php
if( !empty($drinksPost[$drinksIndex]) ) echo " checked='checked'";
$drinksIndex++;
?> /><?php
echo $options;?><br />
<?php
} ?>
Amended the !empty($drinksPost[$drinksIndex]) part and changed to associative.
If this doesn't work, can you include $drinks_checkbox

Categories