I have created a HTML form where a user can enter his availability (which day of week). The form has a button to add new users so at the end I will have multiple DIVs for users, hence I have USER_DOW with two dimensions USER_DOW[][]
.
<div id="user1" class="user" >
<div class="name">
<label>Name</label>
<input type="text" name="USER_Name[]">
</div>
<div>
<label>Day of Week</label>
<select multiple id="USER_DOW" name="USER_DOW[][]" size='7'>
<option value="Mon">Monday</option>
<option value="Tue">Tuesday</option>
<option value="Wed">Wednesday</option>
<option value="Thu">Thursday</option>
<option value="Fri">Friday</option>
<option value="Sat">Saturday</option>
<option value="Sun">Sunday</option>
</select>
</div>
</div>
I'm having issues accessing the elements in the PHP
foreach($USER_Name as $a => $b){
echo $a+1;
echo $USER_Name[$a];
echo "Number of selected days for user " + count($USER_DOW);
foreach($USER_DOW as $c => $b){
echo $USER_DOW[$c][$a];
}
}
At the moment, if I add 2 users, one selecting Wed.& Sun. and the second only Mon., what I get is three days (count) for both users and on the first user all three are printed (Wed, Sun, Mon) while for the second nothing.
Any idea? Have I misunderstood the keys in the arrays?
The names of input elements of a form are passed as written, without any interpretation.
So, in this example:
<!DOCTYPE html>
<html>
<head><title>Test</title></head>
<body>
<?php if( count($_GET) ): ?>
<pre><?php var_dump($_GET); ?></pre>
<?php endif; ?>
/* FORM #1 */
<form action="test.php">
<input type="text" name="txt[0]" value="One">
<input type="text" name="txt[1]" value="Two">
<input type="submit" name="action" value="Test">
</form>
/* FORM #2 */
<form action="test.php">
<input type="text" name="txt[]" value="One">
<input type="text" name="txt[]" value="Two">
<input type="submit" name="action" value="Test">
</form>
/* FORM #3 */
<form action="test.php">
<input type="text" name="txt[][]" value="One">
<input type="text" name="txt[][]" value="Two">
<input type="submit" name="action" value="Test">
</form>
<?php
?>
</body>
</html>
The generated URLs are:
/* FORM #1 */ test.php?txt[0]=One&txt[1]=Two&action=Test
/* FORM #2 */ test.php?txt[]=One&txt[]=Two&action=Test
/* FORM #3 */ test.php?txt[][]=One&txt[][]=Two&action=Test
( rawurldecoded for clarity)
When process $_GET
/ $_POST
variables, PHP try to interpreter it, so in the first and second forms, the result is the same:
Array
(
[txt] => Array
(
[0] => One
[1] => Two
)
[action] => Test
)
But, in form #3, the result is:
Array
(
[txt] => Array
(
[0] => Array
(
[0] => One
)
[1] => Array
(
[0] => Two
)
)
[action] => Test
)
because PHP increment first-level key, but not the deeper key.
If you expectation is to increment the deeper array, you have to specify first-level key in the form. Something like:
<input type="text" name="txt[1][]" value="One">
<input type="text" name="txt[1][]" value="Two">
or, if code-generated, like:
<input type="text" name="txt[<?php echo $SomeVar; ?>][]" value="One">
<input type="text" name="txt[<?php echo $SomeVar; ?>][]" value="Two">
<?php
var_dump($_POST);
function do_form($index) {
?>
<div id="user<?= $index ?>" class="user" >
<div class="name">
<label>Name</label>
<input type="text" name="USER_Name[<?= $index ?>]">
</div>
<div>
<label>Day of Week</label>
<select multiple id="USER_DOW" name="USER_DOW[<?= $index ?>][]" size='7'>
<option value="Mon">Monday</option>
<option value="Tue">Tuesday</option>
<option value="Wed">Wednesday</option>
<option value="Thu">Thursday</option>
<option value="Fri">Friday</option>
<option value="Sat">Saturday</option>
<option value="Sun">Sunday</option>
</select>
</div>
</div>
<?php
}
?>
<form method="POST">
<?php do_form(1); do_form(2); ?>
<input type="submit">
</form>
Sample output after submit:
array (size=2)
'USER_Name' =>
array (size=2)
1 => string 'foo' (length=3)
2 => string 'bar' (length=3)
'USER_DOW' =>
array (size=2)
1 =>
array (size=1)
0 => string 'Mon' (length=3)
2 =>
array (size=2)
0 => string 'Mon' (length=3)
1 => string 'Tue' (length=3)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.