0

I have an 2D array of form, method=POST, where I want to get the days on which a subject have classes using input type=checkbox.

<input type='text' name='subject[]' />
<label> <input type='checkbox' name='monday[]' value='M'>M</label>
<label> <input type='checkbox' name='tuesday[]' value='T'>T</label>
<label> <input type='checkbox' name='wednesday[]' value='W'>W</label>
<label> <input type='checkbox' name='thursday[]' value='Th'>Th</label>
<label> <input type='checkbox' name='friday[]' value='F'>F</label>
<label> <input type='checkbox' name='saturday[]' value='Sa'>Sa</label>
<input type='text' name='subject[]' />
<label> <input type='checkbox' name='monday[]' value='M'>M</label>
<label> <input type='checkbox' name='tuesday[]' value='T'>T</label>
<label> <input type='checkbox' name='wednesday[]' value='W'>W</label>
<label> <input type='checkbox' name='thursday[]' value='Th'>Th</label>
<label> <input type='checkbox' name='friday[]' value='F'>F</label>
<label> <input type='checkbox' name='saturday[]' value='Sa'>Sa</label>

and with the following inputs (2 rows): {Subject1, M} {Subject2 T, W} I am expecting that PHP will fetch the $_POST variable as:

  'subject' => 
    array (size=2)
      0 => string 'Subject1' (length=8)
      1 => string 'Subject2' (length=8)
  'monday' => 
    array (size=1)
      **1 => string 'M' (length=1)**
  'tuesday' => 
    array (size=1)
      0 => string 'T' (length=1)
  'wednesday' => 
    array (size=1)
      0 => string 'W' (length=1)

^result from var_dump($_POST); but it fetch it as:

'subject' => 
    array (size=2)
      0 => string 'Subject1' (length=8)
      1 => string 'Subject2' (length=8)
  'monday' => 
    array (size=1)
      **0 => string 'M' (length=1)**
  'tuesday' => 
    array (size=1)
      0 => string 'T' (length=1)
  'wednesday' => 
    array (size=1)
      0 => string 'W' (length=1)

what should I do to fetch the values correctly?

3
  • Why did you name all the checkboxes different? Commented May 19, 2014 at 9:57
  • 1
    Y not use one array for all the days like <input type='checkbox' name='days[]' value='M'>M</label> ??? Commented May 19, 2014 at 9:57
  • if i use name='days[]' it results into ` 'days' => array (size=3) 0 => string 'M' (length=1) 1 => string 'T' (length=1) 2 => string 'W' (length=1)` Commented May 19, 2014 at 10:01

1 Answer 1

0

Change all checkbox name as days[] and in your php file use this code $_POST['days']. this will returns all selected values.

Sign up to request clarification or add additional context in comments.

2 Comments

that will return the values as ` 'days' => array (size=3) 0 => string 'M' (length=1) 1 => string 'T' (length=1) 2 => string 'W' (length=1)` but the code snippet is only for one row. but the code is for 2D array of forms
use this print_r($_POST['days']). vardump shows value and its datatype php.net/manual/en/function.var-dump.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.