Please help.
I am trying to dynamically name my checkboxes using php. I am using POST.
The problem I am running in to is $element is not working. The results of $_POST
does not show any checkboxes.
Thanks in advance for the help.
foreach(array_keys($cart_array) as $element)
{
print "<input type = 'checkbox' checked name = '{$element}' />";
}
But something like
foreach(array_keys($cart_array) as $element)
{
print "<input type = 'checkbox' checked name = '$element}' />";
}
works just fine. Notice the missing { near $element}. This code would show which checkboxes are
on!! The printed array would have an extra "}"
Array
(
[Tomato_and_Cheese_small] => on
[Tomato_and_Cheese_small}] => 1
[Tomato_and_Cheese_large] => on
[Tomato_and_Cheese_large}] => 1
)
ps. there are other inputs like text that get posted to $_POST just fine.
The print_r($cart_array) works fine too.
The browser sends the value of radio buttons only when they are checked.
Also, each radio button must have the same name (if you want to user to be able to check only one of them). Only the value changes:
print '<input type=checkbox checked value="'.htmlspecialchars($element).'" name=checked_items />';
POST this and check the value of $_POST['checked_items']
If a checkbox is not checked it will not be present in the post parameters.
What happened was that he had checkboxes with the name $element and text fields with the same name too after that. So the checkbox name $element in $_POST got overrided by the text field $element
Related
I'm creating a control panel and having check boxes like the following
The value of $myboxid is the checkbox id, example: cb1 and the name is just the value eg Name of place: London. It grabs this information from my database
<input id='".$myboxid."' name='cplace[]' checked type='checkbox' value='".$box."'><label for='".$myboxid."'>".$boxname."</label>
What I'm trying to do is check which box is selected out of the multi box selection. I can get which boxes are selected and for that to output the value, my problem is I also need it to tell me which boxes are not selected.
My Form method is POST, my php back end is the following
$lname=$_POST['cplace'];
if(isset($_POST['cplace'])) {
foreach($lname as $place){
echo $place." CHECKED <BR>";
}
}
I'm trying to get this to output the checkbed boxes and the ones which are not selected.
Thanks for the help!
Well you can check the value of the checkbox in the POST array.
Simply print all the checkbox from the db and then check the value
In one line you can do like this
Inside your db loop
$isChecked=(in_array($_POST['cplace'], $box)) ? "checked" : "";
echo "<input id='".$myboxid."' name='cplace[]' ".$isChecked." type='checkbox' value='".$box."'><label for='".$myboxid."'>".$boxname."</label>";
I'm using HTML, PHP, jQuery for my website.
I've a check box on my form as follows:
<input type="checkbox" name="check_status" id="check_status" value="1"> Status
I want the same check box for two different values. In short, after submission of the form if check box is checked I should get the value 1 in $_POST['check_status'] array and if the check box is unchecked at the time of submission of form I should get the value as 0 in $_POST['check_status'] array after form submission.
Now as per the above HTML code if check box is checked I'm getting value 1 and if the check box is unchecked then I'm getting blank value.
How should I resolve this issue to achieve the desired result?
You can add condition in php. Hope this will help.
if(isset($_POST['check_status'])) {
$status = $_POST['check_status'];
} else {
$status = 0;
}
echo $status;
you can use this
$status = 0;
if(isset($_POST['check_status'])) {
$status = 1;
}
echo $status;
In case you really need those values to be send as 1 and 0 to the server ( maybe you can't change the server-side code), you can add a hidden field with the same name as your checkbox, and then use JavaScript/jQuery to fill that hidden field, before you submit the form, with 1 or 0, for checkbox being checked, respectively unchecked. .
$("#hiddenField").val( $('#checkbox').is(':checked') ? 1 : 0 );
I have a form with dynamically added checkboxes - each checkbox together with a hidden field. I need only the checked values displayed in pairs with the hidden field when submitted.
This is what I have:
<input type="checkbox" name="valg[]" value="<?=$hent_data[id]?>" />
<input type="hidden" name="process_id[]" value="<?=$hent_data[process_id]?>" />
<?php
if($_POST[submit] != ""){
$arrlength=count($_POST[valg]);
for($x=0;$x<$arrlength;$x++) {
$dimen1 = $_POST[valg][$x];
$dimen2 = $_POST[process_id][$x];
echo $hest = "INSERT INTO chosen (kat_ref, prod_ref, process_id) VALUES ($dimen1, '', $dimen2)"."<br/>";
}
}
?>
When submitted I get the correct number of rows as I have checked, with the correct checkbox value, BUT, the problem is in the hidden text input. On submit it lists all hidden values.
Let's say in a form with 10 checkboxes (and hidden text input) I have ticked 3 checkboxes I would want the exact 3 hidden text input boxes to be listet together with the ticked checkboxes, but it returns them all, which means that no matter how many checkboxes I check, it'll still parse all the hidden value fields.
Any ideas?
I hope you understand - or else let me know ;-)
From the discussion, to me it seems better to send the checkbox in key=>value
<input type="checkbox" name="valg[<?=$hent_data[id]?>]" value="<?=$hent_data[process_id]?>" />
This will give you a result in the php side similar to
Array
(
[valg] => Array
(
[uniqueKey3] => processID3
[uniqueKey7] => processID7
[uniqueKey8] => processID8
)
)
Therefore in the php you can do this:
foreach($_POST['valg'] as $ID => $processID){
echo $hest = "INSERT INTO chosen (kat_ref, prod_ref, process_id) VALUES ($ID, '', $processID)"."<br/>";
}
ok i have dynamically created form elements with unique names and when validated i want to store all the form data into SESSION.
This is the code to do it:
if(isset($_POST["address_submit"])){
insertSessionVars();
header("Location: http://localhost/project%20gallery/notes.php");
exit;
}
function insertSessionVars(){
foreach($_POST as $fieldname => $fieldvalue){
$_SESSION['formAddresses'][$fieldname] = $fieldvalue;
}
}
This works almost fine but stores also submit button value. I output it like this:
foreach($_SESSION['formAddresses'] as $value){
echo $value;
}
Any help will be greatful :)
Don't assign a name attribute to your submit button. If you assign a name then it is passed as part of the $_POST array.
<input type="submit" value="My Button" />
Since you no longer have the submit button in post, instead of checking if the submit button is set check to see if the post array contains data using count().
if(count($_POST) > 0)
The $_POST values are just array so after the for each why don't you use unset to drop the submit key from the array
That way you can check the form has been submitted and the either choose to not set the submit key or drop it rather then not giving it a name at all
Either way you can in your foreach do something like this (see *) and pass "$except" as parameter.
Where $except has to be the name of your submit button in insertSessionVars();
if(!in_array($key, $except)){...}
And ofc above your foreach something like this :
if(!is_array($except)) $except=array($except);
Hi so have form with a vote and what i want to do is what ever options they tick, when they click submit it the posts all the values of the check boxes ticked to the same page allowed me to echo them
i have tried just doing this
if(isset($_POST['submitted'])) {
$list = $_POST['vote'];
echo $list;
}
but that only echos the last value selected
Thanks,
Ben
ok so i have a fix by changing the name to an array but i got a problem because i used javascript functions like this
checkAll(document.form.vote)
so what do i change it to?
When you name the inputs you can give it a name like name="checkboxes[]" and that will throw it in an array when it posts to the next page. Hope that helps!
In your form set name attribut like this:
<input type="checkbox" name="vote[]" />
Then you can:
foreach($_POST['vote'] as $vote){
echo $vote;
}