Multiple option select form - php

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)

Related

Sort multidimensional array with different keys and values

I have a form where the user can add all the additional fields that they need.
Note: I would like to find a way to organize the code more efficiently, I will explain it in detail later.
As it is structured in HTML, I have simplified it to make it easier to understand:
<form action="" method="post">
<h1>Products</h1>
<p>
<input type="text" name="product_name[]" value="Product #1">
<input type="text" name="product_sku[]" value="pro-001">
<input type="text" name="product_price[]" value="$12.00">
<input type="text" name="product_stock[]" value="10">
</p>
<p>
<input type="text" name="product_name[]" value="Product #2">
<input type="text" name="product_sku[]" value="pro-002">
<input type="text" name="product_price[]" value="$12.00">
<input type="text" name="product_stock[]" value="10">
</p>
<p><button type="submit">Add Product</button></p>
</form>
I need to process these received data, to later work with them more easily, for example adding it to the database. But I get the code this way, a structure that doesn't make things much easier for working with that data.
Array
(
[product_name] => Array
(
[0] => Product #1
[1] => Product #2
)
[product_sku] => Array
(
[0] => pro-001
[1] => pro-002
)
[product_price] => Array
(
[0] => $12.00
[1] => $12.00
)
[product_stock] => Array
(
[0] => 10
[1] => 10
)
)
I wish I could receive the code like this:
Array
(
[0] => Array
(
[product_name] => Product #1
[product_sku] => pro-001
[product_price] => $12.00
[product_stock] => 10
)
[1] => Array
(
[product_name] => Product #2
[product_sku] => pro-002
[product_price] => $12.00
[product_stock] => 10
)
)
I have achieved it in the following way, but I want to do it in a more optimal way.
if(isset($_POST) && !empty($_POST)) {
// Total products to add
$total_products = count($_POST["product_name"]);
// Products ordered
$products_created = [];
for ($i=0; $i <$total_products ; $i++) {
$products_created[$i] = array(
'product_name' => $_POST["product_name"][$i],
'product_sku' => $_POST["product_sku"][$i],
'product_price' => $_POST["product_price"][$i],
'product_stock' => $_POST["product_stock"][$i]
);
}
echo "<pre>"; print_r($_POST);
echo "<pre>"; print_r($products_created);
}
Complete example code:
<?php
if(isset($_POST) && !empty($_POST)) {
// Total products to add
$total_products = count($_POST["product_name"]);
// Products ordered
$products_created = [];
for ($i=0; $i <$total_products ; $i++) {
$products_created[$i] = array(
'product_name' => $_POST["product_name"][$i],
'product_sku' => $_POST["product_sku"][$i],
'product_price' => $_POST["product_price"][$i],
'product_stock' => $_POST["product_stock"][$i]
);
}
echo "<pre>"; print_r($_POST);
echo "<pre>"; print_r($products_created);
}
?>
<form action="" method="post">
<h1>Products</h1>
<p>
<input type="text" name="product_name[]" value="Product #1">
<input type="text" name="product_sku[]" value="pro-001">
<input type="text" name="product_price[]" value="$12.00">
<input type="text" name="product_stock[]" value="10">
</p>
<p>
<input type="text" name="product_name[]" value="Product #2">
<input type="text" name="product_sku[]" value="pro-002">
<input type="text" name="product_price[]" value="$12.00">
<input type="text" name="product_stock[]" value="10">
</p>
<p><button type="submit">Add Product</button></p>
</form>
Solution in PHP
You could use a transpose function, that will turn your $_POST data into the desired data structure:
function transpose($array) {
foreach ($array as $key => $values) {
foreach ($values as $i => $value) $result[$i][$key] = $value;
}
return $result;
}
call as:
$products_created = transpose($_POST);
Solution in HTML
You could get the desired structure directly in PHP $_POST, if you change your HTML to this:
<form action="" method="post">
<h1>Products</h1>
<p>
<input type="text" name="product[0][name]" value="Product #1">
<input type="text" name="product[0][sku]" value="pro-001">
<input type="text" name="product[0][price]" value="$12.00">
<input type="text" name="product[0][stock]" value="10">
</p>
<p>
<input type="text" name="product[1][name]" value="Product #2">
<input type="text" name="product[1][sku]" value="pro-002">
<input type="text" name="product[1][price]" value="$12.00">
<input type="text" name="product[1][stock]" value="10">
</p>
<p><button type="submit">Add Product</button></p>
</form>
You would need some logic to inject that sequential number in the first bracket pairs in the name attributes. Assuming that this HTML is produced by PHP, it would not be hard to do that. If your HTML is dynamic on the client side, where rows can be added without interaction with the server, then you'll need to do this (also) in JavaScript.
I suppose you'll have no problem in setting that up.

How to pair the values of these two arrays in PHP?

I have a table with some items on it and each row has a checkbox and a textarea. When I pres submit I want to take the values of the textareas ONLY for the respective checked checkbox and NOT for the ones that aren't checked even if they have textareas with some content.
<form method="post">
<?php if (!empty($arr_devices)) { ?>
<?php foreach ($arr_devices as $device) : ?>
<tr>
<td>
<input type="checkbox" name="devices[]" value="<?php echo $device["id"].$dev_comment ?>">
<td>
<td>
<div class="input-group">
<textarea name="dev_comment[]" placeholder="comment" rows="1" cols="50"><?php echo $dev_comment; ?></textarea>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php } ?>
<input class="btn" type="submit" name="submit" value="Report">
<form>
Printed arrays after summitted:
Device_id([0] => 790 [1] => 1140 [2] => 1142 )
Comments( [0] => sdf [1] => sdfsdfs [2] => [3] => fsdfsd [4] => )
as it is now, I am able to receive ONLY the device_id for the checked ones but ALL the values(even empty ones) for the comments when I am submitting. Why is that happening and how to fix it?
Only checked checkboxes will be submitted. You can keep the checkbox and textarea values paired by adding the same key to their name arrays. The device ID would probably work well for that.
<td>
<input type="checkbox" name="devices[<?php echo $device["id"] ?>]" value="<?php echo $device["id"].$dev_comment ?>">
<td>
<div class="input-group">
<textarea name="dev_comment[<?php echo $device["id"] ?>]" placeholder="comment" rows="1" cols="50"><?php echo $dev_comment; ?></textarea>
</div>
</td>
If the device id is not unique, you can just use the index from the foreach loop.
Then in the form handler code you can use that shared id from the checkboxes to access the comments:
foreach ($_POST['devices'] as $id => $value) {
// do something with $_POST['dev_comment'][$id]
}

php create array with key by html input

am trying to create array with key by using html input
here's my html input
<form method="post" action="">
<input type="text" name="name[][you]" value="" />
<input type="text" name="name[][he]" value="" />
<input type="text" name="name[][she]" value="" />
<input type="text" name="name[][you]" value="" />
<input type="text" name="name[][he]" value="" />
<input type="text" name="name[][she]" value="" />
<button type="submit">go</button>
</form>
my outbut is
Array ( [0] => Array ( [you] => jhon ) [1] => Array ( [he] => joy ) [2] => Array ( [she] => sarah ) [3] => Array ( [you] => samm ) [4] => Array ( [he] => petter ) [5] => Array ( [she] => susan ) )
but i want the array to be like this
Array( [0]=> array ( [you] => jhon [he] => joy [she] => sarah )[1]=> array ( [you] => pitter [he] => tom [she] => suszan ) )
is there away to do that
try like this ==>
<form method="post" action="">
<input type="text" name="name[0][you]" value="" />
<input type="text" name="name[0][he]" value="" />
<input type="text" name="name[0][she]" value="" />
<input type="text" name="name[1][you]" value="" />
<input type="text" name="name[1][he]" value="" />
<input type="text" name="name[1][she]" value="" />
<button type="submit">go</button>
</form>
OR
<form method="post" action="">
<?php $n = 2; // how many interval you want
for ($i = 0; $i < $n; $i++) {
?>
<input type="text" name="name[<?php echo $i; ?>][you]" value="" />
<input type="text" name="name[<?php echo $i; ?>][he]" value="" />
<input type="text" name="name[<?php echo $i; ?>][she]" value="" />
<?php } ?>
</form>
If you want to output an array with two children then set the keys manually, name[0] for the first three inputs and name[1] for the last three.
Everytime you write name="[][key]" php increment automatic the key.
if you write syntax like [] php increment the index of array.
Small explation
For example:
If you write an array like this
$array[] = "msg1";
$array[] = "msg2";
$array[] = "msg3";
$array length will be 2 (3 elements because it starts from 0) and it is same as
$array[0] = "msg1";
$array[1] = "msg2";
$array[2] = "msg3";
This is different from above
$array[0] = "msg1";
$array[1] = "msg2";
$array[1] = "msg3";
This array will have only 1 length (2 elements only)
Solution of your question is :
<form method="post" action="">
<input type="text" name="name[0][you]" value="" />
<input type="text" name="name[0][he]" value="" />
<input type="text" name="name[0][she]" value="" />
<input type="text" name="name[1][you]" value="" />
<input type="text" name="name[1][he]" value="" />
<input type="text" name="name[1][she]" value="" />
<button type="submit">go</button>
</form>

inserting status for four inputs from select php

I have four HTML inputs and a select option. i want to insert the status for the three inputs = "0" and for the selected one = "1". kindly help me and tell some thing about mysql query for
<input placeholder="Choice A:" name="a">
<input placeholder="Choice B:" name="b">
<input placeholder="Choice C:" name="c">
<input placeholder="Choice D:" name="d">
<select class="form-control" name="select">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
i want the status for a is 1 and for the remaing 0 in Database :
I don't know what you want to achieve but as per your comments you are looking for this:
HTML:
<form method="post" action="">
<input type="text" placeholder="Choice A:" name="a">
<input type="text" placeholder="Choice B:" name="b">
<input type="text" placeholder="Choice C:" name="c">
<input type="text" placeholder="Choice D:" name="d">
<select class="form-control" name="select">
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
</select>
<input type="submit" name="submit" value="Submit Now">
</form>
PHP:
if(count($_POST) > 0){
$updateZero = array();
$updateOne = array();
$selectArr = array('A','B','C','D'); // default array
foreach ($selectArr as $key => $value) {
if($value == $_POST['select']){
$updateOne[] = $value; // store those selected
}
else{
$updateZero[] = $value; // store those not selected
}
}
echo "<pre>";
print_r($updateOne); // selected array
print_r($updateZero); // remaining array
}
Result:
Array
(
[0] => A
)
Array
(
[0] => B
[1] => C
[2] => D
)
Now you can use these array in MYSQL Statement as you need.

html form inputs array PHP

I have a problem with my cloned form:
The form has 4 inputs and I need to separate the variables and put them together by people.
My inputs:
<input type="text" name="fName[]">
<input type="text" name="lName[]">
<input type="number" name="age[]">
<input type="text" name="city[]">
My PHP:
$fNameArray = $_POST['fName'];
$lNameArray = $_POST['lName'];
$ageArray = $_POST['age'];
$cityArray = $_POST['city'];
I really do not understand how two-dimensional arrays work, to group people to complete the form, as in the following example:
first name: siddharta, last name: naranjo, age: 29, city: mexico
first name: xxxxx, last name: xxxxxx, age: xx, city: xxxx.
Some of these answers are wrong if I understand you.
First of all if you use the naming convention data[]fname then it will not group surely?
Secondly, the blank bracket will have it constantly move to a new array.
You will end up with a load of arrays with one element in them.
You will need to number or ID the arrays to keep them together.
<?php
if(isset($_POST['data'])){
$results = $_POST['data'];
echo "<pre>";
print_r($results);
echo "<pre>";
}
?>
<form action="" method="post">
<!-- User 1 -->
<input type="text" name="data[0][fName]" value="test 1">
<input type="text" name="data[0][lName]" value="test 1">
<input type="number" name="data[0][age]" value="21">
<input type="text" name="data[0][city]" value="test 1">
<br>
<!-- User 2 -->
<input type="text" name="data[1][fName]" value="test 2">
<input type="text" name="data[1][lName]" value="test 2">
<input type="number" name="data[1][age]" value="22">
<input type="text" name="data[1][city]" value="test 2">
<br>
<!-- User 3 -->
<input type="text" name="data[2][fName]" value="test 3">
<input type="text" name="data[2][lName]" value="test 3">
<input type="number" name="data[2][age]" value="23">
<input type="text" name="data[2][city]" value="test 3">
<br>
<input type="submit" value="Go">
</form>
This produces this:
Array
(
[0] => Array
(
[fName] => test 1
[lName] => test 1
[age] => 21
[city] => test 1
)
[1] => Array
(
[fName] => test 2
[lName] => test 2
[age] => 22
[city] => test 2
)
[2] => Array
(
[fName] => test 3
[lName] => test 3
[age] => 23
[city] => test 3
)
)
Which is what it looks like you want?
Your html is ok
If i understand you right take a look at this
<?php
if($_POST && isset($_POST['fName'])) {
$people = count($_POST['fName']);
for($i=0; $i<$people; $i++) {
echo "<p>first name: {$_POST['fName'][$i]}, last name: {$_POST['lName'][$i]} ...</p>";
}
}
Use keys for 2 dimensional array.
<input type="text" name="fName[0]">
<input type="text" name="lName[0]">
<input type="number" name="age[0]">
<input type="text" name="city[0]">
<input type="text" name="fName[1]">
<input type="text" name="lName[1]">
<input type="number" name="age[1]">
<input type="text" name="city[1]">

Categories