Posting label text along with the array checkbox values - php

I have following checkbox arrays in html form
<label><input name="columns[]" type="checkbox" value="pname" />Property Name</label>
<label><input name="columns[]" type="checkbox" value="2000" />Price</label>
<label><input name="columns[]" type="checkbox" value="New Road" />Location</label>
<label><input name="columns[]" type="checkbox" value="Joe Smith" />Owner</label>
there are about 30 such checkboxes.
All i want to do is sending the text along with its value of the checked box while form is posted. Lets say if i checked first checkbox, I want to send pname along with text Property Name. There can be several solutions for this, but don't know which one will be easier one.
The one I thought is. In submit page iterating through the loop of checkbox array, create another array and Push the text manually to this new array.
Or other option I have thought is, in value attribute I will send text with value like this pname^Property Name and in submit page i iterate through loop, explode the value and put them to respective arrays.
Is there any alternative solution for this?
Thanks

HTML
<label><input name="columns['Property Name']" type="checkbox" value="pname" />Property Name</label>
<label><input name="columns['Price']" type="checkbox" value="2000" />Price</label>
<label><input name="columns['Location']" type="checkbox" value="New Road" />Location</label>
<label><input name="columns['Owner']" type="checkbox" value="Joe Smith" />Owner</label>
PHP
$columns = $_GET["columns"]; // or $_POST
echo $columns["Property Name"] ; // pname
echo $columns["Price"] ; // 2000
echo $columns["Location"] ; // New Orad
echo $columns["Owner"] ; // Joe Smith

You can use like this
$posted_columns = implode(',',$_POST['columns']);
print_r($posted_columns);
but your code should be like this
<label><input name="columns['Property Name']" type="checkbox" value="pname" />Property Name</label>

Related

Use database to check a checkbox

I have a page to view assets with an Edit link. When I click the link it goes to edit_case.php which has a form to edit what elements of the row are in the database as checkboxes. However the boxes do not show them as checked. I have the following code...
// get already checked box values
$repop = "SELECT * FROM case_audit WHERE case_id = $case_id";
$popresults = mysqli_query($dbc, $repop);
$row = mysqli_fetch_array($popresults, MYSQLI_ASSOC);
print_r ($row);
The print_r does show the whole record row from DB. which is either a 1 or 0, checked || not checked.
The form...
<div id="facepics">
<label><input type="checkbox" name="Facial1" value="<?php echo $row['frontrest']; ?>" >Front at Rest </label><br>
<label><input type="checkbox" name="Facial2" value="<?php echo $row['frontbigsmile']; ?>" >Front Big Smille</label><br>
<label><input type="checkbox" name="Facial3" value="<?php echo $row['profile']; ?>" >Profile</label><br>
<label><input type="checkbox" name="Facial4" value="<?php echo $row['subvertex']; ?>" >SubMento Vertex</label><br>
</div>
I know I need to turn the 1's to "checked" just not sure how best to do that.
so basically checked="true" attribute in input creates a checked checkbox.
HTML Code looks like
<input type="checkbox" checked="true">
In your case you can do it like:
<input type="checkbox" name="Facial1" value="frontrest" <?= (intval($row['frontrest']) == 1) ? 'checked' : '';>
Also note that I changed value attribute, with frontrest so that you can identify the checkbox uniquely
EDIT: I have modified the code
<input type="checkbox" name="Facial1" <?=$row['frontrest']==1?'checked':''?>>
I often have the same issue where the browser ignores checked="false" and checks all
so I use
<input type="checkbox" checked>

html checkboxes with none selected does not POST

I am posting checkboxes from an HTML form and am having a weird problem. The first is that I have a default checked and disabled box at the top of the form, but it doesn not get included in the POST data. The second is that If I don't check something, the entire array is left out.
How can I get it to 1) include my default box and 2) POST an empty array if none are selected?
Here's the code:
<form action="file.php" method="POST">
<label><input type="checkbox" name="options[]" value="username" checked disabled> Username</label><br>
<label><input type="checkbox" name="options[]" value="title"> Title</label>
<label><input type="checkbox" name="options[]" value="first"> First Name</label>
<label><input type="checkbox" name="options[]" value="last"> Last Name</label><br>
<label><input type="checkbox" name="options[]" value="address"> Address</label>
<label><input type="checkbox" name="options[]" value="city"> City</label>
<label><input type="checkbox" name="options[]" value="state"> State</label>
<label><input type="checkbox" name="options[]" value="zip"> ZIP</label><br>
<label><input type="checkbox" name="options[]" value="email"> Email</label>
<label><input type="checkbox" name="options[]" value="phone"> Phone</label><br>
<input type="submit" value="submit">
</form>
file.php
<?php var_dump($_POST)
This is a part of standard HTML (i.e. not a browser thing). By definition, unchecked boxes are never successful. Consider a different data structure, or adding something like
if(isset($_POST['options'])) {
//work with options here
}
If that won't work, you can always include a hidden element, that will at least get you the value in $_POST
<input type="hidden" name="options[]" value="NA">
You could also do something like this without changing your HTML at all. Simply, create a list of all possible checkbox values and compare with those posted. As far as username is concerned, since it will always be there, you could just add it manually to the $_POST array.
// Auto insert username to $_POST array (because it's always there by default)
$_POST['options'][] = 'username';
// Create array of all possible checkbox values
$boxes = array('username','title','first','last','address','city','state','zip','email','phone');
// Compare $_POST array to list of possible checkboxes
// and create manual post array
$post_array = array();
foreach ($boxes as $box) {
$post_array[$box] = in_array($box, $_POST['options']) ? 'checked' : 'NOT checked';
}
The output will be an array, $post_array, which will contain something like:
Array
(
[username] => checked
[title] => checked
[first] => NOT checked
[last] => NOT checked
[address] => checked
[city] => checked
[state] => NOT checked
[zip] => NOT checked
[email] => NOT checked
[phone] => checked
)

checkbox array only returning one result

I have built a form that has a checkbox input array (saving to an array). However, when I POST it and retrieve the results, it only offers the last selection.
<input type="checkbox" value="Friendly" name="quest[9]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[9]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[9]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[9]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[9]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[9]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
For example, say I chose "Friendly", "Efficient", and "Genuine".
When I POST it over to a PHP document and run
print_r($_POST['quest']);
I only receive
Array ( [9] => Genuine )
back so "Genuine" is the only item I get back. Is there a way to fix this? What have I done wrong?
This is the 9th question on the survey, so changing the name unfortunately is not an option. Is there any way to combine the results to that single array separated by commas? I could always explode on the php side.
All your checkboxes have the same name, make them unique
<input type="checkbox" value="Friendly" name="quest[3]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[4]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[5]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[6]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[7]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[8]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[9]"> Genuine<br>
or use empty square brackets so php will treat the inputs as an array
<input type="checkbox" value="Friendly" name="quest[]"> Friendly<br>
<input type="checkbox" value="Attentive" name="quest[]"> Attentive<br>
<input type="checkbox" value="Enthusiastic" name="quest[]"> Enthusiastic<br>
<input type="checkbox" value="Understanding" name="quest[]"> Understanding<br>
<input type="checkbox" value="Well Mannered" name="quest[]"> Well Mannered<br>
<input type="checkbox" value="Efficient" name="quest[]"> Efficient<br>
<input type="checkbox" value="Genuine" name="quest[]"> Genuine<br>
use quest[] in name instead of quest[9].
also in php part use this to add multiple choices .
<?php
$quest = implode(',',$_post['quest']);
print_r($quest);
?>
Happy coding!!
I'm posting a new answer about your comments on the previous one:
Since you must keep quest[9] as the organization for the checkbox array..
You may want to try and make it a more complex array, where each <input> has name="quest[9][1]", name="quest[9][2]" and so on.
And find the contents by
print_r($_POST['quest']);
again

Get POST data from multiple checkboxes?

Im trying to create a form using PHP and I cant seem to find a tutorial on what I need so thought Id ask on here.
I have a multiple checkbox option on my page...
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service">Static guarding<br>
<input type="checkbox" value="Mobile Patrols" name="service">Mobile Patrols<br>
<input type="checkbox" value="Alarm response escorting" name="service">Alarm response escorting<br>
<input type="checkbox" value="Alarm response/ Keyholding" name="service">Alarm response/ Keyholding<br>
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
</li>
I'm not sure however how to collect all checkbox values using POST method?
if i use
$service = $_POST['service'];
I only get 'other' returned
Name the fields like service[] instead of service, then you'll be able to access it as array. After that, you can apply regular functions to arrays:
Check if a certain value was selected:
if (in_array("Other", $_POST['service'])) { /* Other was selected */}
Get a single newline-separated string with all selected options:
echo implode("\n", $_POST['service']);
Loop through all selected checkboxes:
foreach ($_POST['service'] as $service) {
echo "You selected: $service <br>";
}
Currently it's just catching your last hidden input. Why do you have that hidden input there at all? If you want to gather information if the "Other" box is checked, then you have to hide the
<input type="text" name="other" style="display:none;"/>
and you can show it with javascript when the "Other" box is checked. Something like that.
Just make the name attribute service[]
<li>
<label>What service are you enquiring about?</label>
<input type="checkbox" value="Static guarding" name="service[]">Static guarding<br />
<input type="checkbox" value="Mobile Patrols" name="service[]">Mobile Patrols<br />
<input type="checkbox" value="Alarm response escorting" name="service[]">Alarm response escorting<br />
<input type="checkbox" value="Alarm response/ Keyholding" name="service[]">Alarm response/ Keyholding<br />
<input type="checkbox" value="Other" name="service[]">Other</span>
</li>
Then in your PHP you can access it like so
$service = $_POST['service'];
echo $service[0]; // Output will be the value of the first selected checkbox
echo $service[1]; // Output will be the value of the second selected checkbox
print_r($service); //Output will be an array of values of the selected checkboxes
etc...
<input type="checkbox" value="Other" name="service">Other<input type="hidden" value="Other" name="service"></span>
You've got a hidden input field with the same name as the checkbox. "later" fields with the same name as an earlier one will overwrite the previous field's values. This means that your form, as posted above, will ALWAYS submit service=Other.
Given the phrasing of your question in the html, it sounds more like you'd want a radio button, which allows only ONE of a group of same-name fields to be selected. Checkboxes are an "AND" situation, radio buttons correspond to "OR"

combine checkbox values using php mysql

I having around 20 check boxes in my form as
<input type="checkbox" name="c1" value="1" />
<input type="checkbox" name="c2" value="2" />
<input type="checkbox" name="c3" value="3" />
<input type="checkbox" name="c4" value="4" />
i would like to these values to a database. I just thought rather than creating 20 fields in my database grab all the values at store in the db as 1,2,3,4 and then when querying to explode the values and display it accordingly using php.
can someone please tell me how am i supposed to concatenate the values 1,2,3,4 from the check fields when submitted so i can pass to the database.
i would appreciate if anyone can suggest a different effective way to achieve this using php.
You can change the name of the checkboxes to be the same, something like
<input type="checkbox" name="cb[]" value="1" />
<input type="checkbox" name="cb[]" value="2" />
Then access them via $_GET or $_POST via
if (isset($_POST['cb'])) {
$my_values = implode(",", $_POST['cb']);
}
If you want them sorted, then you will want to do something like this:
if (isset($_POST['cb'])) {
$my_values = $_POST['cb'];
sort($my_values);
$my_db_value = implode(',', $my_values);
}
For the record, I agree with #Shef in the case that the database can handle the extra load. Depending on when this information will be needed in a highly scalable solution, however, this is a perfectly acceptable way to handle this.
To answer your initial question, first off you need to name your checkboxes all the same name, so:
<input type="checkbox" name="box[]" value="1" />
....
<input type="checkbox" name="box[]" value="20" />
PHP script:
$boxes = $_POST['box'];
$values = implode(",", $boxes); // $values now has "1,2,3,4...", insert into table
A better way would be to have a separate table (see #Shef 's comment).

Categories