Getting checked checkbox values in variable PHP - php

I have added two checkboxes on page with submit button. After submitting there is a method having foreach loop to check which checkbox is checked , and that checkbox value should get in variable. If both checkbox's are checked then it should append both values in variable. But in my case the values are repeating. And also if i check both checkboxes it is picking up only second checkbox value.
Below is my code for HTML :
<form name="" action="" method="post">
<input type="checkbox" name="checkbox[]" value="Training"/> Training <br/><br/>
<input type="checkbox" name="checkbox[]" value="Posting"/> Posting<br/><br/>
<input type="submit" value="Submit"/>
</form>
Code for PHP:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$value = "";
$name = $_POST['checkbox'];
// optional
// echo "You chose the following color(s): <br>";
foreach ($name as $value){
//echo $value.", ";
$value .= $value;
}
echo $value;
//print "<script>alert('".$value."')</script>";
}
?>

$value is a local variable to the loop it get overwritten when the loop continues again.
$result = '';
foreach ($name as $value){
$result .= $value;
}
echo $result;

$name = (array)$_POST['checkbox'];
$value = '';
foreach ($name as $val){
$value .= $val;
}

Related

How can I echo out content of if statement

i have 5 of these:
if(isset($_POST['checkbox'])) {
$name = "Alex";
}
How can I echo the name of every selected checkbox outside of the if statement?
From your example, build an array of $name:
if(isset($_POST['checkbox'])) {
$name[] = "Alex";
}
if(isset($_POST['checkbox1'])) {
$name[] = "Bob";
}
Then either:
echo implode(', ', $name);
Or:
foreach($name as $value) {
echo $value;
}
But actually I'm wondering why not just set the values in the form inputs (use an array):
<input type="checkbox" name="checkbox[]" value="Alex">
<input type="checkbox" name="checkbox[]" value="Bob">
Then:
foreach($_POST['checkbox'] as $value) {
echo $value;
}

print value of checked checkbox

if (isset($_POST['approve']))
{
$con=mysql_connect("localhost","root","");
mysql_select_db("shoolin",$con);
$arr=$_POST['check'];
foreach($arr as $selected)
{
echo $selected."</br>";
}
}
i need to print the value of checkbox which is checked,so please suggest some solution to solve this problem of printing value of checkbox
This code shows an error in the foreach loop
Invalid argument supplied for foreach() in C:\xampp\htdocs\xampp\user.php on line 8
Try this way..
<form action="#" method="post">
<input type="checkbox" name="check_list[]" value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]" value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]" value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if (isset($_POST['submit'])) { //to run PHP script on submit
if (!empty($_POST['check_list'])) {
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['check_list'] as $selected) {
echo $selected."</br>";
}
}
}
?>
The error is saying that $arr is not an array
if (is_array($arr))
{
foreach ($arr as $selected)
{
echo $selected."</br>";
}
}
else
{
echo $arr."</br>";
}

insert multiple rows to a table using foreach php

i need to insert multiple rows data into table
Date and inspection_id will remain same
but the input values are repeated.
Here is the script i have
<?php
if (isset($_POST["submit"])) {
$taskdate = date('Y:m:d');
$inspection_id = '1';
$post = $_POST['nfo'];
foreach ($post['point_id'] as $key => $value) {
$point_id.= $value.", ";
}
foreach ($post['point_comment'] as $key => $value) {
$point_comment.= $value.", ";
}
foreach ($post['point_value'] as $key => $value) {
$point_value.= $value.", ";
}
$query = "INSERT INTO `inspections` (`inspection_id`, `point_id`, `value`, `comment`)VALUES('$inspection_id', '$point_id', '$point_value', '$point_comment')";
$result = mysql_query($query);
HTML FORM i am USING
<form action="pentasks.php" method="post">
<select name="nfo[point_value][]">
<option selected>Chose</option>
<option value="1">Qualify</option>
<option value="2">Disqualify</option>
</select>
<input name = "nfo[point_comment][]" value = "" type="text">
<input type="hidden" name="inspection_id" value="<?= $task_id; ?>"> <!-- value From another query -->
<input type="hidden" name="nfo[point_id][]" value="<?= $spot_id3; ?>">
<input type="submit" name="submit" value="Submit">
</form>
Help please how i insert data
this is something in a table questions are defined
retrieved on form with input to answer
Try this
foreach ($post['point_comment'] as $key => $value) {
$point_comment=$post['point_comment'][$value];
$point_value= $post['point_value'][$value];
//run your query here
}
and mysql is depricated Learn mysqli_ function or PDO.
and change your select tag like this
<select name="point_value[]">
<select name="point_comment[]">
Arif thanks for your effort but your answer did not helped
spend few hours by ownself i got it working and here is the working solution for my question
HTML FORM
<input type = "text" name = "point_comment[]" />
<input type = "text" name = "point_value[]" />
Here is Foreach loop PHP
$my_comment = $_POST['point_comment'];
$point_comment = "";
foreach ($my_comment as $key => $value) {
$point_comment = $value;
$my_value = $_POST['point_value'];
$point_value = "";
foreach ($my_value as $key => $value) {
$point_value = $value;
// SQL INSERT or anything query here
} }
Hope this will help for those looking for similar

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.

Compute 1st value on multiple values on a checkbox

I was trying to make a COMPUTERIZED ORDERING SYSTEM. My problem is how can I compute the 1st value on my checkbox. The second value on the checkbox will be posted on the summary of orders.
Once I've check all those three, it will compute the total amount of the menu and to display the selected menu on the summary of orders. But I can't figure out how can I display the total amount.
Can anybody guide me how to compute the total on my 1st value on the checkbox?
<form method="post">
<input name='ts[]' type='checkbox' value='40 |Tosilog'/> Tosilog
<br>
<input name='cs[]' type='checkbox' value='40 |Chiksilog'/>Chiksilog
<br>
<input name='ps[]' type='checkbox' value='45 |Porksilog'/>Porksilog
<br>
<input type="submit" name="go" value= "Total">
</form>
<?php
//tosilog
$ts = $_POST['ts'];
$value = explode("|",$ts[0]);
echo $value[0];
echo $value[1];
//chiksilog
$cs = $_POST['cs'];
$value = explode("|",$cs[0]);
echo $value[0];
echo $value[1];
//porksilog
$ps = $_POST['ps'];
$value = explode("|",$ps[0]);
echo $value[0];
echo $value[1];
?>
<!-- compute for the 1st value on checkbox -->
<?php
$ts=$_POST['ts[0]'];
$cs=$_POST['cs[0]'];
$ps=$_POST['ps[0]'];
?>
<?php $compute = $ts[0] + $cs[0] + $ps[0];?>
<?php echo "$compute " ; ?>
If you're getting an array of checkbox elements, and they are numeric, you can use array_sum(). Not sure I understand your suggested structure, but I'll give you a code sample here based on the existing form structure. Then I'll post another bit to try to make this simpler for you. Executive summary: You do not need all the variables that are created by this form structure.
<?php // RAY_temp_user193.php
error_reporting(E_ALL);
$total = 0;
$inputs = array();
$errors = array();
if (!empty($_POST))
{
if (!empty($_POST['ts']))
{
foreach ($_POST['ts'] as $ts)
{
$inputs[] = current(explode(' |', $ts));
}
}
else
{
$errors[] = 'Tosilog';
}
if (!empty($_POST['cs']))
{
foreach ($_POST['cs'] as $cs)
{
$inputs[] = current(explode(' |', $cs));
}
}
else
{
$errors[] = 'Chiksilog';
}
if (!empty($_POST['ps']))
{
foreach ($_POST['ps'] as $ps)
{
$inputs[] = current(explode(' |', $ps));
}
}
else
{
$errors[] = 'Porksilog';
}
// IF ERRORS
if (!empty($errors))
{
echo 'UNABLE TO PRINT COMPLETE TOTAL. MISSING: ' . implode(',', $errors);
}
$total = array_sum($inputs);
if ($total) echo "<br/>TOTAL: $total <br/>" . PHP_EOL;
// END OF THE ACTION SCRIPT
}
// CREATE THE FORM
$form = <<<ENDFORM
<form method="post">
<input name='ts[]' type='checkbox' value='40 |Tosilog'/> Tosilog
<br>
<input name='cs[]' type='checkbox' value='40 |Chiksilog'/>Chiksilog
<br>
<input name='ps[]' type='checkbox' value='45 |Porksilog'/>Porksilog
<br>
<input type="submit" value= "Total">
</form>
ENDFORM;
echo $form;
See http://www.laprbass.com/RAY_temp_miggy.php
This is probably more along the lines of the way I would do it. The script knows what goes into the HTML and it knows exactly what to expect in the POST request. It is easy to add or remove different inputs. Often these kinds of inputs come from a data base table.
<?php // RAY_temp_miggy.php
error_reporting(E_ALL);
$total = 0;
// EXPECTED INPUTS
$inputs = array
( 'Tosilog' => 40
, 'Chiksilog' => 40
, 'Porksilog' => 45
)
;
if (!empty($_POST))
{
// ACTIVATE THIS TO SEE WHAT WAS SUBMITTED
// var_dump($_POST);
// SUM OF THE ELEMENTS
$total = array_sum($_POST);
echo "TOTAL: $total";
// SUM OF THE EXPECTED INPUTS
$expect = array_sum($inputs);
if ($total != $expect) echo " BUT THERE MAY BE INCOMPLETE INPUTS!";
// END OF THE ACTION SCRIPT
}
// CREATE THE FORM
$checkboxes = NULL;
foreach ($inputs as $name => $value)
{
$checkboxes
.= '<input name="'
. $name
. '" type="checkbox" value="'
. $value
. '" />'
. $name
. '<br/>'
. PHP_EOL
;
}
$form = <<<ENDFORM
<form method="post">
$checkboxes
<input type="submit" value= "Total">
</form>
ENDFORM;
echo $form;

Categories