Form with variable input values PHP - php

I have a form which has variable input values .For example one customer has bought 5 items so all of them show another customer has bought 3 items so only three items show up.All these values are based on database
<form id="customer" class="customer" method="post" action="">
<input type="text" name="customer1" value="" >
<input type="text" name="customer2" value="" >
<input type="text" name="customer3" value="" >
</form>
Now my question how do i process a form like this where every time the number of post variables is different.

<input type="text" name="customer[]" value="" >

Something like:
$products = array();
foreach ($_POST as $key => $value) {
array_push($products, $key, $value);
}

Related

Laravel Blade some values in 2 dimensional input array missing

i have a form that has editable answers, which are built like the following:
#foreach ($question->answers as $answer)
<input type="text" name="answers[{{$loop->index}}][text]">
<input type="hidden" name="answers[{{$loop->index}}][id]" value="{{$answer->id}}">
#endforeach
Which generates the html
<input type="text" name="answers[0][text]" value="Somevalue">
<input type="hidden" name="answers[0][id]" value="1">
<input type="text" name="answers[1][text]" value="Somevalue">
<input type="hidden" name="answers[1][id]" value="2">
In my Controller, I try to get the values from the request like this:
$requestAnswers = $request->input('answers');
foreach($requestAnswers as $key => $value) {
$id = $value['id'];
$text = $value['text'];
// Some answer handling
}
Somehow, when I try to access my answers, even when I dd($request->input('answers')) only some values show up, sometimes not having a text, sometimes not appearing at all.
Is there an error in my code or is the problem related to something else?
Thanks in advance!

Multiple file upload with optional items

I have a form with multiple inputs like this:
<div id="users">
<input type="text" name="title[]" required>
<input type="file" name="images[]">
<input type="text" name="title[]" required>
<input type="file" name="images[]">
<input type="text" name="title[]" required>
<input type="file" name="images[]">
</div>
<input type="button" value="add new user">
<input type="submit" value="submit">
the problem is that selecting image is optional and if the user doesn't select any image for any of titles the posted data would be like this:
title = ["title1","title2","title3"]
images = [image1.jpg,image3.jpg]
the count of users is not fixed and user can add any desired number of titles and images by clicking on the add new user button. so "add new user" button will add a pair of these inputs to users div:
<input type="text" name="title[]" required>
<input type="file" name="images[]">
is there any way to detect if the file is selected or not? or to send a default value for not selected file inputs? I want to set null value for image if no file is selected, like this:
user1:{title="title1",image=image1.jpg}
user2:{title="title2",image=null}
user3:{title="title3",image=image3.jpg}
.
.
.
You can check if particular index is set for file:
var_dump($_POST['title'][0]);
if (isset($_FILES['images']['tmp_name'][0])) {
var_dump($_FILES['images']['tmp_name'][0]);
}
All you have to do is process the 2 arrays and check there is a matching image with each title. If there is not a image for each title, put anything that you like in place of the image name
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$output = [];
foreach( $_POST['title'] as $idx => $title){
$u = $idx+1;
$output['user'.$u] = ['title'=>$title,
'images'=> $_POST['images'][$idx] == '' ? 'NULL' : $_POST['images'][$idx]];
}
$j = json_encode($output);
echo $j;
}
RESULT
{
"user1":{"title":"asss","images":"NULL"},
"user2":{"title":"dd","images":"hammer.jpg"}
}
Thank you everybody, Pavol Velky was right partially, Although the array size is less than the number of inputs, but the browser will set an index to the array, in example if you have two inputs and just select the second one, the array will be like : item:[1:{}] (if you select first one: item:[{}]) and you can check the order by the index number.

Putting multiple elements in array through loop

I have multiple inputs in a file like this:
<form action="card_generate.php">
<input type="text" name="tZero">
<input type="text" name="tOne">
<input type="text" name="tTwo">
<input type="text" name="tThree">
</form>
in the file card_generate.php this have to go in an array i have wrote:
$tabs = array($_POST["tZero"], $_POST["tOne"], $_POST["tTwo"], $_POST["tThree"]);
Is there a way I can put these values in an array through a loop or something instead of putting each value in an array one by one, there can be more values than four values.
Use input name array,
<form action="card_generate.php">
<input type="text" name="t[]">
<input type="text" name="t[]">
<input type="text" name="t[]">
<input type="text" name="t[]">
</form>
And, you will get in post,
print_r($_POST['t']);
You can also use a foreach loop as shown in the example:
HTML
<form method="POST">
<input type="text" name="tZero">
<input type="text" name="tOne">
<input type="text" name="tTwo">
<input type="text" name="tThree">
<input type="submit" name="">
</form>
PHP
<?php
if ( isset($_POST) ) {
foreach ($_POST as $key => $value) {
echo "Name: $key, value: $value";
echo "<br>";
}
}
?>
RESULT
Name: tZero, value: first
Name: tOne, value: hi
Name: tTwo, value: firthfds
Name: tThree, value: fourth value
P.S. Don't forget method="POST" in your <form>.
You can, using array_push: http://php.net/manual/de/function.array-push.php
You can than just loop like this:
for($i = 0; $i < 10; $i++) {
if(isset($_POST["t" . $i])) {
array_push($array, $_POST["t" . $i]);
}
}

Save variable value in session like cart with single form

I have a simple form in sidebar on 5 pages where i have two fields name and price. I want to save its value in session when user will submit the form. when i store its value in session after submit a form then it replace name every time. But i want to store name value like array. If user submit form with name user1 and then submit again with username user2 then i want to show these 2 names in session variable and want to show increment of form submit. Like cart functionality. Is there any way to store these value with a single form. Please help me.
<form id="myform" action="post.php" method="POST">
<div class="row">
<label for="name">Your name:</label><br />
<input id="name" class="input" name="name" type="text" value="" size="30" /><br />
</div>
<div class="row">
<label for="price">Price:</label><br />
<input id="price" class="input" name="price" type="text" value="" size="30" /><br />
</div>
<input id="submit_button" type="submit" value="submit" />
</form>
post.php
session_start();
$_SESSION['cart_items'] = array();
$_SESSION['cart_items'] = $_POST['name'];
foreach($_SESSION["cart_items"] as $cart_item)
{
echo $cart_item;
}
Try to add values to session by
session_start();
foreach ($_SESSION["products"] as $cart_itm) //loop through session array
{
if($cart_item["code"] == $product_code){ //the item exist in array
$product[] = array('name'=>$cart_item["name"], 'code'=>$cart_item["code"], 'qty'=>$product_qty, 'price'=>$cart_item["price"]);
$found = true;
}else{
//item doesn't exist in the list, just retrieve old info and prepare array for session var
$product[] = array('name'=>$cart_item["name"], 'price'=>$cart_item["price"]);
}
}
Add the result by
foreach ($_SESSION["cart"] as $cart_item)
{
echo $cart_item["price"];
$subtotal = ($cart_item["price"]*$cart_item["qty"]);
$total = ($total + $subtotal);
}
echo $total;

Dynamic multidimensional post form

I have a form with a variable number of inputs. The inputs are inside the table and I need to get three values from them: the row, the column and the actual value inside the input.
Some may be populated some may not and I need all their values to update a mysql db (row and column to know what to update, value to know the new value to insert in the database).
This is my form (an example version of it):
<form method="post" action="">
<input name="data[111][222]" value="2" />
<input name="data[112][221]" value="0" />
<input name="data[113][223]" value="4" />
//goes on
<input name="data[324][435]" value="11" />
<input name="data[325][436]" value="" />
</form>
And that's as far as I go. How can I get the data from this form so I can do a simple update in my database that goes like this (for all the affected inputs):
update table set res="value_from_input" where row="row_value" and col="col_value"
<form method="post" action="">
<input name="data[111][222]" value="2" />
<input name="data[112][221]" value="0" />
<input name="data[113][223]" value="4" />
<input name="data[324][435]" value="11" />
<input name="data[325][436]" value="" />
<input type="submit" />
</form>
<?php
foreach ($_POST['data'] as $k1 => $v1)
{
foreach ($v1 as $k2 => $v2)
{
echo "<p>k1:".$k1."; k2: ".$k2."; value: ".$v2."</p>";
$query = "update table set res='$v2' where row='$k1' and col='$k2'";
mysql_query($query);
}
}
?>
foreach($_POST['data'] as $row => $row_array)
{
foreach($row_array as $col => $value)
{
// Sanitize all input data before entry
$mysql = "UPDATE table SET res='$value' WHERE row='$row' AND col='$col'";
}
}

Categories