I'm building a form with the possibility to add more group of fields, to process them i read out the array in a for loop
the script:
<?php
foreach ($_POST as $key => $value) {
$$key = $value;
}
$count = count($name);
for ($i=0; $i<$count; $i++){
?>
<strong><?php echo $name[$i]; ?></strong> (<?php echo $check[$i]; ?>)<br /><?php echo $select[$i]; ?><br /><br />
<?php
}
?>
<form method="post">
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<div class="group">
<input type="text" name="name[]" /><br />
<input type="checkbox" name="check[]" value="true" /><br />
<select name="select[]"><option>1</option><option>2</option><option>3</option></select>
</div>
<button>Add another group</button>
<input type="submit" />
</form>
If all checkboxes are checked there is no problem but if only the last one is checked it counts only one checkbox in the array, name[0] is then combined with check[0] but check[0] is really check[2]. English is not my native language so i don't know the right words.
Actually, there is a decent workaround for this as proposed by Sam in this answer on Stack Overflow:
Post the checkboxes that are unchecked
It worked for me, and I suspect you and I had a similar problem (mine being that I had/have upwards of 300 input fields in similar(ish) groups and didn't want to write validation rules for every one of those individual fields, just rules targetted at each family of input types e.g. the email addresses, or the postcodes. In brief, the technique is that you place a hidden input field, with the same name, before your checkbox field. Setting the value of the hidden field (type='hidden') to '0' will ensure that at least one key/value appears in your POST array, with the '0' being superceded by a later '1' only if the box is checked. I needed the '0' value to allow people to 'unset' an option they had previously 'set', for example that they were willing to show their contact data. This technique allows me to present the user with much the same form for an update as they would get at at first registration. Thanks to Sam!
That's normal PHP behaviour, when a checkbox is not checked it does not includes it in $_POST variable ...
Yes. That's how it is. There is no workaround for this. Using field[] identifiers is only applicable for unstructured input fields. If you depend on the ordering and relation, then unset fields will prevent this from working.
You have no other option but to set explicit indexes. You should bite into the sour apple and do so for name[0], check[1] and select[2]. Use a PHP loop to simplify it:
foreach (range(0,2) as $i)
echo <<< END
<div class="group">
<input type="text" name="name[$i]" /><br />
<input type="checkbox" name="check[$i]" value="true" /><br />
<select name="select[$i]"><option>1</option><option>2</option><option>3</option></select>
</div>
END
I was having the same exact problem with some parts of a form that can be add on user's wish. I came to this really simple workaround using basic javascript:
Add a hidden type input just after your checkbox, assign its value to the state of the checkbox and it's this hidden input that will be reported in your $_POST, will be true or false.
<input type="checkbox" onchange="this.nextSibling.value = this.checked">
<input type="hidden" name="state[]" value="false">
Just change value to true if your checkbox is checked="checked" by default.
Related
How do we dynamically display the sum of all the values of check boxes that have been checked in php.
Its basically like a checkout in a shopping cart.Each item that is to be checked, has a value and the final amount(in the same page at the bottom) should be the sum of rates of all the items that have been checked(without refreshing).
I may need to use AJAX. Can anyone give a simple sample code please
For the checkboxes, you need to make them into an array, say for instance:
<input type="checkbox" name="items[]" id="items[]" value="25" /><br />
<input type="checkbox" name="items[]" id="items[]" value="40" /><br />
<input type="checkbox" name="items[]" id="items[]" value="12" /><br />
... <!-- as many as you want -->
<input type="checkbox" name="items[]" id="items[]" value="20" /><br />
On the PHP side you could then handle it like so...(NOTE: It will come into PHP as an array)
$items = $_POST["items"];
//it's an array -- feel free to do a var_dump($items) to see its content
//to sum you could even do an $total_amount = array_sum($items);
//but i would advise cleaning up the values first
And yes, you can achieve the same if you submit the form using AJAX (e.g. via jQuery)
Happy coding :)
I'm trying to have an email send on submit, but I can't figure out how to echo the value of a radio box, and the text in the value. Can anyone help me?
Here's my radio box:
<label>
<input type="radio" name="question8" value="0;Not at all" id="question8_1" />
Not at all</label>
<br />
<label>
<input type="radio" name="question8" value="25;Slightly" id="question8_2" />
Slightly</label>
<br />
<label>
Assuming your form was submitted via post,
echo $_POST['question8'];
If for some reason it was submitted via $_GET
echo $_GET['question8'];
To separate the value and text, use explode(";", $value):
$radio = explode(";", $_POST['question8']);
$radioval = $radio[0];
$radiotext = $radio[1];
echo "Total $radioval: Answer: $radiotext";
// Or the same thing tidier, via list() multi-assignment
list($radioval, $radiotext) = explode(";", $_POST['question8']);
And both of these assume your <input> tags actually occur inside a <form> tag as in:
<form action='somepage.php' method='post'>
<label>
<input type="radio" name="question8" value="0;Not at all" id="question8_1" />
Not at all</label>
<br />
<label>
<input type="radio" name="question8" value="25;Slightly" id="question8_2" />
Slightly</label>
<br />
<label>
</form>
You can get the value of the data from the $_GET, $_POST, or $_REQUEST variables ($_REQUEST will do either post or get requests) just use the name that your radio box has:
$value = $_REQUEST['question8'];
echo $value;
Also, of course make sure that the form that the radio boxes are part of is pointed at the correct php script for this to work. (they are part of a form right?)
I have an HTML form with a dynamic number of checkbox fields, all of which are encapsulated within a submit form. When the form is submitted, I want to loop through the values of each checkbox field with a PHP script. At the same time, I have to keep a certain ID associated with the checkbox field so that when I loop through each one in my script, I can use the ID to update the correct row in my database. Currently, I have:
<input checked="checked" name="attended_<?php echo($pid); ?>"
I'm just not sure how to go ahead and access all of the attended[] values from my PHP script (and keep the ID at the same time). Would I use a multidimensional array like the following?
<input checked="checked" name="attended[<?php echo($i); ?>][<?php echo($pid); ?>]; ?>"
I'd appreciate any help on this. Thanks!
lets say this is data from form
<input checked="checked" name="attended[1]; ?>"
<input checked="checked" name="attended[2]; ?>"
<input checked="checked" name="attended[3]; ?>"
<input checked="checked" name="attended[4]; ?>"
<input checked="checked" name="hello[1]; ?>"
<input checked="checked" name="hello[2]; ?>"
this is how it would look as array. not needed its just for show
// $k => $v
$attended[1]='blah blah';
$attended[2]='blah blah';
$attended[3]='blah blah';
$attended[4]='blah blah';
$hello[1]='blah blah';
$hello[2]='blah blah';
science bit
foreach($attended as $k=>$v){
$sql = "UPDATE mytable SET attended = '$v', hello = '{$hello[$k]}' where id = '$k'";
$query = mysql_query($sql) or die("Cannot query the database.<br />" . mysql_error());
}
all associated data should have same pid e.g
<input checked="checked" name="attended[1]; ?>"
<input checked="checked" name="hello[1]; ?>"
Usually when I create checkboxes I put an index in the name. This way you can loop through each checkbox in your submittion code.
<input type="checkbox" name="cbGroup[1]" value="y" />
<input type="checkbox" name="cbGroup[2]" value="y" />
<input type="checkbox" name="cbGroup[3]" value="y" />
and in your PHP
foreach($_POST['cbGroup'] as $index=>$checkbox) {}
You'll want to make sure your $_POST['cbGroup'] is set though because it won't be if the checkbox isn't checked.
Edit: Sorry, I should learn to read the question fully heh :) I use multidimensional arrays in PHP with HTML inputs all the time and I think it's the way I would go.
Maybe you could do it with a hidden field which is sort of 'associated' with the checkbox by the name:
<input checked="checked" name="attended_<?php echo($i); ?>"
<input type="hidden" name="attended_<?php echo($i); ?>_reference" value="<?php echo($pid); ?>" />
So while processing the POST-data after a submit, you could put the references together again with some string-manipulation.
I just want to have php determines whether a checkbox is checked, but I am running into a problem of getting the right return. Help please.
My html code
<label>
<input name="Language" type="checkbox" id="aa" checked="checked" />
One</label>
<label>
<input name="Language" type="checkbox" id="bb" />Two</label>
<label>
<input type="checkbox" name="Language" id="cc" />Three</label>
I pass the values to php by the $_GET method
my php code:
$aa=$_GET["aa"];
$bb=$_GET["bb"];
$cc=$_GET["cc"];
echo $aa."<br>";
echo $bb."<br>";
echo $cc."<br>";
the output is
true
false
false
I next want to just determine if each box is checked and if so, do something.
if ($aa == true) { $abc="checked";}
else { $abc="not checked"; }
if ($bb == true) { $cde="checked";}
else { $cde="not checked"; }
if ($fgh == true) { $fgh="checked";}
else { $fgh="not checked"; }
But the if statements always return true, even if the box is not checked. I tried variations of "===" and "!=", but it does not seem to work.
TIA
if (isset($_GET['checkbox_name'])) { ... }
Form controls (with the exception of file inputs, and with some special rules for image inputs) always submit strings. There is no concept of a boolean in a query string or a form encoded POST body.
The id is irrelevant — only the name and value matter (at least as far as PHP is concerned).
Since you haven't given them values they will, IIRC, default to on so if you are doing a comparison you should look for that. Looking with isset is simpler though. This is somewhat beside the point though, since your example gives them all the same name and value, so you can't differentiate between them.
Additionally, due to an oddity of the PHP form data parser, you have to end the with [] if you want multiple elements with the same name.
You probably want to do something like this:
<label>
<input name="Language[]" type="checkbox" id="aa" checked="checked" value="One" />
One</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value="Two" />Two</label>
<label>
<input type="checkbox" name="Language[]" id="cc" value="Three" />Three</label>
Important: Note the addition of values and the change in name.
Then in PHP $_GET['Language'] will be an Array of the values of the checked checkboxes, which you can loop over.
Try isset()
I think your HTML code should be like
<label>
<input name="Language[]" type="checkbox" id="aa" checked="checked" value ="1" />One
</label>
<label>
<input name="Language[]" type="checkbox" id="bb" value ="2" />Two
</label>
<label>
<input name="Language[]" type="checkbox" id="cc" value ="3" />Three
</label>
and then by using something like
$count = count($_GET["Language"]); you can count the number of checkboxes checked.
Or do
$arr = $_GET["Language"]; //$arr is an array that contains the values of the checked checkboxes
Then you can foreach over the array
foreach( $arr as $item)
{
echo $item . "</br>"; /* Will print 1,2 and 3 (mind newlines)*/
}
In my PHP I got it from $_POST['checkbox_name'], and I found that the variable had the value on when the box was checked (i.e. it existed even if the checkbox was clear).
I have a form that has a series of check boxes and some line items have a text input next to them that defines quantity of the item.
<input type="checkbox" name="measure[][input]" value="<?=$item->id?>">
<input class="item_mult" type="text" name="measure[][input]" />
What is the best way to capture the integer from the input field and have it correspond to the check box so I can use it to calculate the total later on?
<input type="checkbox" name="measure[<?php echo $item->id; ?>][checked]" value="<?php echo $item->id; ?>">
<input class="item_mult" type="text" name="measure[<?php echo $item->id; ?>][input]" />
That should give the values $measure[1]['checked'] (only present if checked) and $measure[1]['input']
I corrected your short tags - they're a bad idea, as they are disabled in php by default so can cause issues if you move servers
You can give your array a name/id to associate them, just add it into the name attribute:
<input type="checkbox" name="measure[1][my_checkbox]" value="<?=$item->id?>">
<input class="item_mult" type="text" name="measure[1][my_text]" />