Make array out of all checkboxes that were checked? - php

I have a list of people I want to pull from a database. Next to each one, I have a checkbox, here:
<input type='checkbox' name='check' value='".$rows4['id']."'>
They all have to be the same name for me to be able to check all of them / deselect all of them with javascript:
// HTML
<input type='checkbox' onClick='checkAll(document.confirm_form.check)' name='allChecker'>
// JS
function checkAll(field) {
if (confirm_form.allChecker.checked == true) {
for (i = 0; i < field.length; i++) {
field[i].checked = true ;
}
} else {
for (i = 0; i < field.length; i++) {
field[i].checked = false ;
}
}
}
So how will I come out with an array of all the ones that were checked? I have this, but it only returns the last one checked!
<?php
if ($_POST['send_confirm']) {
$check = $_POST['check'];
echo "the check: $check";
}
?>
What would my best bet be?
Thanks.

You can use the special syntax name="check[]" on all the checkboxes. Then the values appear as an array when you retrieve them using $_POST
Here is a good article I found, passing-input-arrays-in-php

You can try with form elements array.
<input type='checkbox' name='check[]' value='".$rows4['id']."'>

$("#formid").find("input:checked").each(function()
{
(this.id).attr('checked',true);
});
use Jquery method to check and uncheck the checkbox

Related

How to display text when a (multiple)checkbox is checked?

This is the snippet in my index:
<p id="text" style="display:none">hello</p>
echo '<tr>';
echo '<td>'.$s['school_name'].'</td>';
echo '<td>'.$s['location'].'</td>';
echo '<td>'.$s['population'].'</td>';
echo '<td>'.$s['cost'].'</td>';
echo '<td>'.$s['topnotchers'].'</td>';
echo '<td>'.$s['rating'].'</td>';
echo '<td><input type="checkbox" name="userSelection" name="userSelection" onclick="userSelection()" value="'.$s['school_id'].'"</td>';
echo '</tr>';
}
And here is my js:
function userSelection(){
var checkBox = document.getElementsByName("userSelection")[0];
var text = document.getElementById("text");
if (checkBox.checked == true){
text.style.display = "block";
} else {
text.style.display = "none";
}
}
The text only displays when I check on row[0] since I set it to row[0]. But, what should be done if I need the function to be true to all rows when I check it?
The above code only displays text on first loop.
Is "*" going to work?
I'm a beginner, a help would be appreciated.
You have hard coded the index 0. So js function will run only for checkbox with zero index.
First of all change the name of checkboxes to checkbox1, checkbox2,... This will simplify the case.
Then You need to run a for loop for each checkbox. Use
`
for(i=0; i < 6; i++){
currentCheckBox = document.getElementById("checkbox"+i)
var text = document.getElementById("text");
if (currentCheckBox.checked == true){
text.style.display = "block";
} else {
text.style.display = "none";
}
}
`
You have to specify a name as an array like below,
<input type="checkbox" id="userSelection1" name="userSelection[]" onclick="userSelection()" value="">
Then only you can able to loop in javascript to check the status
var checkBoxArr = document.getElementsByName("userSelection[]");

Is checkboxes checked or not?

Need som serious help!
<?php
$php1 = !isset($_GET['php1']);
$php2 = !isset($_GET['php2']);
$php3 = !isset($_GET['php3']);
$php4 = !isset($_GET['php4']);
$php5 = !isset($_GET['php5']);
if ($php1 == 'one' && $php2 == 'two' && $php5 == 'five') {
echo "<h2>R</h2>WRONG";
} else {
echo "<h2>R</h2>CORRECT";
}
?>
HTML
<form action="One.php" method="get">
<input type="checkbox" name="php1" value="one"> q1<br>
<input type="checkbox" name="php2" value="two"> q1<br>
<input type="checkbox" name="php3" value="three"> q1<br>
<input type="checkbox" name="php4" value="four"> q1<br>
<input type="checkbox" name="php5" value="five"> q1<br>
<br>
<input type="submit" value="Send!">
</form>
If all checkboxes are empty, there will be a message saying that. How do I do it?
If I dont check any boxes, it tells me the answers are correct. That's wrong.
<?php
If(isset($_GET['php1'])){
$php1 = 1;
}else{
$php1 = 0;
}
If(isset($_GET['php2'])){
$php2 = 1;
}else{
$php2 = 0;
}
If(isset($_GET['php3'])){
$php3 = 1;
}else{
$php3 = 0;
}
If(isset($_GET['php4'])){
$php4 = 1;
}else{
$php4 = 0;
}
If(isset($_GET['php5'])){
$php5 = 1;
}else{
$php5 = 0;
}
if ($php1 && $php2 && $php5) {
echo "<h2>Resultat</h2>Du svarade rätt på frågan";
} else {
echo "<h2>Resultat</h2>Du svarade fel på frågan";
}
Try this.
Each php variable will have the value of 1 or 0 (true/false). Thus the if only needs to check it if it is or not.
You have to remember that UN-CHECKED Checkboxes are not even sent to the PHP script $_POST/$_GET by the browser.
So there existance is normally all you need to know.
First you must check that they were passed to the script and then check its value, otherwise you will receive undefined index errors for each checkbox that was not checked by the user
if (isset($_GET['php1']) && $_GET['php1'] != '' ) {
Although as the checkbox called php1 can only be set to the value you give it, its value can be assumed and all you need to do is
if (isset($_GET['php1']) ) {
// php1 was checked
Also isset() will test more than one variable exists using an AND. So you could write your code as
if ( isset($_GET['php1'], $_GET['php2'], $_GET['php5']) ) {
echo "<h2>Resultat</h2>Du svarade rätt på frågan";
} else {
echo "<h2>Resultat</h2>Du svarade fel på frågan";
}
$php1 = !isset($_GET['php1']);
This means $php1 is either true or false. If you want the value of php1 get parameter then remove isset cover, just
$_GET['php1'];
isset is used to check whether the variable exist or not after that use $_GET['php1'];

Toggle hiding certain values in a dropdown box using a checkbox and jQuery

I have a drop down box filled with options pulled from a database. Each of these options will have an active indicator value of either 'Y' or 'N'. I have a checkbox which will include all options or just the active ones.
I currently have the options pulling from the database perfectly. The for loop that inputs the options looks like this:
for ($i = 0; $i < count($category_record); $i++) {
if($category_record[$i]->getActiveInd() == "Y")
{
echo '<option value="'.$category_record[$i]->getId().'" '.$checked.' > - '.$category_record[$i]->getTitle().'</option>';
}
else if($category_record[$i]->getActiveInd() == "N")
{
echo '<option id="inactive" value="'.$category_record[$i]->getId().'" '.$checked.' > - '.$category_record[$i]->getTitle().'</option>';
}
So if the value is an inactive value, that option will have an id of '#inactive'.
The checkbox is a simple one:
<input type="checkbox" name="search_active" id="checkbox" />
The jQuery I have at the moment looks like this:
$(document).ready(function() {
$('input[type=checkbox]').click(function(){
var $this = $(this);
if($this.is(':checked'))
{
$('#inactive').show();
}
else
{
$('#inactive').hide();
}
});
});
This code does work, slightly, in that it will hide inactive values when the checkbox is unticked, however it will only hide the first value it comes across and not all of them.
I have inspected the elements and they all still definitely have the #inactive id but they won't hide. I have searched for an answer but haven't been able to find one, what am I doing wrong?
For more than one element you should use class .active, .inactive or create your own attribute such as state.
PHP:
for ($i = 0; $i < count($category_record); $i++) {
if($category_record[$i]->getActiveInd() == "Y")
{
echo '<option value="'.$category_record[$i]->getId().'" '.$checked.' > - '.$category_record[$i]->getTitle().'</option>';
}
else if($category_record[$i]->getActiveInd() == "N")
{
echo '<option class="inactive" value="'.$category_record[$i]->getId().'" '.$checked.' > - '.$category_record[$i]->getTitle().'</option>';
}
JS:
$(document).ready(function() {
$('input[type=checkbox]').click(function(){
var $this = $(this);
if($this.is(':checked'))
{
$('.inactive').show();
}
else
{
$('.inactive').hide();
}
});
});
use class 'inactive' for options in dropdown rather than id. and modify your js to show() and hide() using class.

PHP actions if checkboxes are selected!

I have a form with 3 checkboxes, and my idea is run some commands if they are selected and do something else if they are not selected.
for($i=1; $i<=3; $i++)
{
if ($_POST['option'.$i])
{
echo "123";
}
if (!$_POST['option'.$i])
{
echo "456";
}
}
But if they are not selected the commands are not executed.. The if statement is correct?
No, what you should be doing is checking them like this:
if (isset($_POST['option'.$i]))
Otherwise, you are just trying to evaluate the boolean form of whatever is in that $_POST element. Why is this bad? Suppose the value of that field were 0. Even though the checkbox was checked, your code wouldn't run.
Documentation for isset()
Sure, that will work just fine.
If you want to slightly de-uglify your code, you can do it this way:
<input type="checkbox" name="options[option_name]" value="1" />
<input type="checkbox" name="options[second_option_name]" value="1" />
if(isset($_POST['options']) && is_array($_POST['options']) {
foreach($_POST['options'] as $option_name => $ignore_this) {
switch($option_name) {
case 'option_name':
// do something
break;
case 'second_option_name':
// do something else
break;
}
}
}
You can use an if ... else:
if ($_POST['option'.$i])
{
echo "123";
}
else
{
echo "456";
}
to avoid checking the same condition twice.

Server Side Form Validation PHP with multi-file Upload

HTML:
Owner: input type="text" name="owner[]" />
Category:
<select name="cat[]">
<option value="clothes">Clothes</option>
<option value="shoes">Shoes</option>
<option value="accessories">Accessories</option>
</select>
Upload: <input type="file" name="image[]" />
whith function that clone the same fields when click on "+ button"
I count the POST field with:
$num = count($_FILES['image']['name']);
because i want to know how many times the end user clone the fields.
what i want is Make sure that the user has to fill all fields which he opend with "+ button" i cant check all the hidden fields i want to check just the field he opend.
so what can i do ?
i cant do like this:
$owner = $_POST['owner'][$i];
$cat = $_POST['cat'][$i];
$file = $_FILES['image'][$i];
if ($owner && $cat && $file)
echo "bla bla bla";
else
echo "fill all the fields!";
can anyone help me ?
thank you
There are some points which you need to make sure beforehand. Whenever you are using any input field's name attribute as "owner[]" or "cat[]" or "image[]", you will get an array then. But since, input file's property accessing capability is already 2D array by default, so now you will be able to access those properties as a 3D array.
When you have added a "[]" for the input file field's name attribute, you will now get the name of the 1st file as "$_FILES['image'][0]['name']", because array indices start with 0. As per your question, you can validate using the following way:-
<?php
$numOwners = count($_POST['owner']);
$numCats = count($_POST['cat']);
$numFiles = count($_FILES['image']);
// Check to see if the number of Fields for each (Owners, Categories & Files) are the same
if ($numFiles === $numCats && $numFiles === $numOwners) {
$boolInconsistencyOwners = FALSE;
$boolInconsistencyCats = FALSE;
$boolInconsistencyFiles = FALSE;
for ($i = 0; $i < $numFiles; $i++) {
if (empty($_POST['owner'][$i])) {
$boolInconsistencyOwners = TRUE;
break;
}
if (empty($_POST['cat'][$i])) {
$boolInconsistencyCats = TRUE;
break;
}
if (!is_uploaded_file($_FILES['image'][$i]['tmp_name'])) {
$boolInconsistencyFiles = TRUE;
break;
}
}
if ($boolInconsistencyOwners || $boolInconsistencyCats || $boolInconsistencyFiles) {
echo "I knew that there will be some problems with users' mentality!";
// Redirect with proper Error Messages
}
else {
echo "Wow, Users have improved & have become quite obedient!";
// Proceed with normal requirements
}
}
else {
echo "Something fishy is going on!";
}
?>
Hope it helps.

Categories