Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to submit checkbox values with PHP post method

Tags:

php

<input type="checkbox" class='form' name="checkbox_1" />

<input type="checkbox" class='form' name="checkbox_2" />

<input type="checkbox" class='form' name="checkbox_3" />

.........

<input type="checkbox" class='form' name="checkbox_10" />

The from has been submitted using the "POST" method. identify, which of the check-boxes and write their numbers in increasing order. Separated all numbers by spaces(not new lines) and do not use any HTML formatting.

For Eg:

If check-boxes 3, 5 and 10 are checked.

Ouput would be:

3 5 10

like image 872
Egglabs Avatar asked Feb 22 '10 09:02

Egglabs


People also ask

Can we give value to checkbox?

A checkbox allows you to select single values for submission in a form (or not).

How checkbox is handle value in PHP?

In the PHP script (checkbox-form. php), we can get the submitted option from the $_POST array. If $_POST['formWheelchair'] is “Yes”, then the box was checked. If the check box was not checked, $_POST['formWheelchair'] won't be set.


2 Answers

Change the markup to something like

<input type="checkbox" class='form' value="1" name="checkbox[]" />
<input type="checkbox" class='form' value="2"  name="checkbox[]" />
<input type="checkbox" class='form' value="3"  name="checkbox[]" />

and to get the values submitted, use a simple loop

foreach($_POST['checkbox'] as $checkbox){
    echo $checkbox . ' ';
}
like image 182
vsr Avatar answered Oct 07 '22 04:10

vsr


HTML Code ::

  <input type="checkbox" name="arrayValue[]"  value="value1" > value1 <br />
  <input type="checkbox" name="arrayValue[]"  value="value2" > value2 <br />
  <input type="checkbox" name="arrayValue[]"  value="value3" > value3 <br />
  <input type="checkbox" name="arrayValue[]"  value="value4" > value4 <br />

php code::

 $checkBoxValue = join(", ", $_POST['arrayValue']);  // here first (,) is user-define
                                                   // means, you can change it whatever
                                                // you want, even if it can be (space) or others

now you get the whole value of 'checkbox' in one variable

like image 20
user2013 Avatar answered Oct 07 '22 04:10

user2013