Submitting a dynamic checkbox form and get values in pairs - php

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/>";
}

Related

Checkbox Values stored in DB and want to display

Please see this link
http://thedesigningworld.com/bea
Here's a Small form contains 8-9 fields + a group of checkboxes
I want to save all details in DB + want to display in a table in proper manner, but it not works properly
Here's the code which i used
for($i=0;$i<count($_POST[wert1]);$i++)
{
if($_POST[wert1][$i]!= "")
{
$check1[] =$_POST['wert1'][$i]; } }
$new1=implode(',', $check1);
$result = "INSERT into table1(check1) values($new1)";
$result = mysqli_query($con, $result);
So i've one doubt that for each checkbox row, should i need to define same array name or different like here i used array name as wert1[] for first row
Checkbox values are not transmitted if the box is not checked.
If you have influence, you could put a hidden input field of the same name before the checkbox and the value "0", like:
<input type="hidden" name="checkbox_name" value="0" />
<input type="checkbox" name="checkbox_name" value="1">Some Text</input>
In you example site, you're using array notation, which is basically a good thing. However, you have not given an index so you might not recognize missing elements.

How to access two different values for one checkbox?

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 need assistance preventing both a checked and unchecked checkbox from posting at the same time

I have created a table of checkboxes. The rows are divided up by category, and either a checked or unchecked checkbox gets displayed under a department column. I have a lot of code so I will break down what I am supplying. I am creating an array via each column (odd method, yes). I have noticed that if all check boxes are deselected, it will return the hidden value of 0 each time it loops. Thats great, thats what I wanted. However, if the box is selected, it returns both the value of 0 and the value of 3. For instance:
Test = Array()
Test[0] => 0
Test[1] => 3
How can I prevent it from posting the hidden value?
$row_two = mysql_query("SELECT dept_id FROM categories WHERE cat_name = '{$cats['cat_name']}' and bus_id = '{$busUnits['bus_id']}'");
while (($test_two=mysql_fetch_assoc($row_two)))
{
$AnotherTest = implode(',', $test_two);
$WhatTest = explode(",", $AnotherTest);
if(in_array("3",$WhatTest, TRUE))
{
echo '<input type="hidden" name="Cat_CBC_Test_One[]" value="0">';
echo '<td><input type="checkbox" name="Cat_CBC_Test_One[]" value="3" checked></td>';
}
else
{
echo '<input type="hidden" name="Cat_CBC_Test_One[]" value="0">';
echo '<td><input type="checkbox" name="Cat_CBC_Test_One[]" value="3"></td>';
}
To detect unchecked Checkboxes i would add a hidden field with a different name, like _Cat_CBC_Test_One. That way you dont have the issue with the hidden field interfering.
Then, on the server, you scan through for parameters that start with _ and add the missing "false" Parameters for further processing.

Dynamic forms and PHP

Ive started working on a dynamic form script that allows a user to add form elements via Jquery, which is then in turn submitted to a PHP script.
I'm just after some feedback on ways to achieve this. At the moment I have the following:
When a user adds a form element the element is added with the following name array:
<textarea name="element[text][123]">
<input type="text" name="element[input][456]" />
As I need to know the type of form element that was submitted I am using a multidimensional array called 'element[][]' where the first level of the array is the type of element and the second element of the array is a unique ID and the value.
When I var_dump() This after submission PHP outputs:
array
text => array
123 => string 'The textarea value'
input => array
456 => string 'The input field value'
Im working on the PHP side of the script now and just wondering if there is a better way to do this.
Any thoughts?
UPDATE
I have to change the way that Im doing this as the array keynames are not unique.
If the user adds two textareas
<textarea name="element[text][123]">
<textarea name="element[text][456]">
When the user adds a form element, the element can be dragged so the positioning can be changed after the element was created. This allows a user to add an element but then move it to where they want it to appear.
PHP handles this ordering fine and accepts the array in the order that the form is submitted, however as mentioned above if the key names are the same then the order will be broken.
On the PHP side I need to know
the type of form field
the value of the form field
the unique ID, which is just a timestamp, of the form field
I think I might need to do what Cole mentioned, assigning the names as:
element[text_123]
I can then explode the keyname on '_' to determine the type and the identifier.
UPDATE
I took the script Jack posted and slightly modified it
$vars = $_POST['element'];
foreach ($vars as $id => $vals)
{
// $vars[id] outputs the ID number
// $vars[vals] is the array containing the type and value
echo "This fields ID is $id. ";
foreach($vals as $key => $value)
{
echo "Type was: $key and the value was: $value <br />";
}
}
A quick test of this outputted
This fields ID is 1338261825063. Type was: heading and the value was: xzczxczxczxczxczxc
This fields ID is 1338261822312. Type was: heading and the value was: asdasdasdasdad
From this I know the identifier and the array that it belongs to, the type and the value, but I also know the order that the data was submitted.
From that I can wrap my data in markup, perform any additional operations and then insert the data into the database.
Looks okay; you could also consider something like this (it introduces more fields though, so you must really think the benefit is worth it):
<input type="hidden" name="element[123][type]" value="text" />
<input type="hidden" name="element[456][type]" value="input" />
<textarea name="element[123][value]">
<input type="text" name="element[456][value]" />
Then you can do this:
foreach ($_POST['element'] as $name => $info) {
// $info['type'] is 'text' or 'input'
// $info['value'] is the user input
}

to take values of checkbox in table attributes

i have a database patient with 3-4 tables n each table has about 8 attributes....
i have a table medical history which has attribute additional info ... under which i have 5 checkboxes....
all the values entered are taken up except the chekbox values.....
plz help
How are you constructing your fields? Do they all have the same name attribute? Do they have a name attribute at all? Is there a value attribute?
<input type="checkbox" name="testfield" value="somevalue" />
<input type="checkbox" name="testfield" value="othervalue" />
If you're constructing the checkboxes like that, then PHP will by default ignore all but the last value submitted (it overwrites previous values with new ones), like this:
$_POST = array(
'testfield' => 'othervalue'
)
You have to force PHP into 'array mode' for this type of construct, by adding [] to the name attribute:
<input type="checkbox" name="testfield[]" value="somevalue" />
<input type="checkbox" name="testfield[]" value="othervalue" />
This will allow multiple values to be submitted under the same name, and you'll end up with an array in your _GET/_POST arrays:
$_POST = array(
'testfield' => array('somevalue', 'othervalue')
)
Of course, only the checkboxes which are actually checked will be sent with the form data.
Check that your form is properly constructed by putting a var_dump($_POST); (or GET or REQUEST) in the form handling part of the script and see if the checkbox values are actually being sent. Perhaps you're looking for the wrong name attribute, the tags could be malformed in the form, etc...

Categories