Posting array from form
Give each input a name in array format:
<input type="hidden" name="data[EstPriceInput]" value="" />
Then the in PHP $_POST['data'];
will be an array:
print_r($_POST); // print out the whole post
print_r($_POST['data']); // print out only the data array
POST an array from an HTML form without javascript
check this one out.
<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">
<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">
<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">
<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">
it should end up like this in the $_POST[] array (PHP format for easy visualization)
$_POST[] = array(
'firstname'=>'value',
'lastname'=>'value',
'email'=>'value',
'address'=>'value',
'tree' => array(
'tree1'=>array(
'fruit'=>'value',
'height'=>'value'
),
'tree2'=>array(
'fruit'=>'value',
'height'=>'value'
),
'tree3'=>array(
'fruit'=>'value',
'height'=>'value'
)
)
)
Post an array on submit form
Use []
in the input names:
<input type="text" name="item[]" placeholder="item">
Then $_POST['item']
will be an array of all the inputs.
Put form data into a PHP array
Change the name of your inputs like this:
<form action = "put the data to a single PHP array" method="post">
<input name="a[]" type="text" />
<input name="a[]" type="text" />
<input name="a[]" type="text" />
<input name="a[]" type="text" />
<input type="button"/>
</form>
You can then retrieve the array as follows:
$values = $_POST['a'];
How to store POST values for each form in an array?
You don't need multiple forms or hidden inputs to achieve this. You can just use buttons, and set their values to $i
.
echo "<form id='upload' action='test2.php' method='POST'>";
for ($i = 0; $i < 10; $i++) {
echo "<button type='submit' name='test' value='$i'>Go to album</button>";
}
echo '</form>';
In test2.php
, $_POST['test']
will have the $i
value of the button you clicked.
Passing an array to another page (Form)
i'm glad @ksealey pointed out a more proper method of doing this, but for the sake of answering the question...the reason it's not working is that the serialize alone is not enough to prevent the invalid html. see result of what the serialize leaves in the html:
so you need to be sure the html you produce is valid. you might use encoding like base64 to produce safe html:
echo " <form id=\"my_form\" action=\"\" method=\"post\"";
echo "enctype=\"multipart/form-data\">";
echo "<input type=\"hidden\" name=\"id\" value=\"10\">";
echo "<input type=\"hidden\" name=\"input_name\" ";
echo "value=\"".base64_encode(serialize($my_array))."\" />";
then you can just add the decode to your output:
$passed_array = unserialize(base64_decode($_POST['input_name']));
print_r($passed_array);
How to submit all the values of this form as an array and loop through when posting to SQL db?
Your form will post 2 arrays that contain the values of serial
and imei
from your form. After you've verified that they have the same number of entries you just need to loop through one of them to extract the values from both arrays and insert them into your database.
Since your question did not include anything related to your database connection I can only show you the variables that you will use to insert the data into your DB. Use PDO and prepare the insert before entering the loop (here's a good reference to get you going with PDO and prepare).
$serial_no = $_POST['serial_no'];
$imei = $_POST['imei'];
$entrycount = count($serial_no);
for ($loop = 1;$loop <= $entrycount; $loop++) {
//
// The values from your form will be in the following variables:
//
//
// $serial_no[$entrycount]
// $imei[$entrycount]
//
// The variables shown above are what you will have to insert into your DB.
//
}
How to send array using html form
This is not specific to Symfony2. Its basic HTML. You have to supply multiple input with appropriate name. You cannot sent an array value in a single input element!! Thats basic HTML!!
<form action="{{ path('_przepisy') }}" method="post">
<input type="hidden" name = "produkty[0][iloscuser]" value = "specific-value-from-sniadanie">
<input type="hidden" name = "produkty[1][iloscuser]" value = "specific-value-from-sniadanie">
<input type="hidden" name = "produkty[2][iloscuser]" value = "specific-value-from-sniadanie">
<input type="submit" class="btn btn-success pull-right" value="Przepisy"/>
</form>
But you can send a json string inside a single element.
<form action="{{ path('_przepisy') }}" method="post">
<input type="hidden" name = "produkty" value = "{{ sniadanie | serialize }}">
<input type="submit" class="btn btn-success pull-right" value="Przepisy"/>
</form>
Then in controller
$this->render("your view", [
'produkty' => json_decode($request->get('produkty'))
]);
And in the template
{{produkty[0]['iloscuser']}}
Related Topics
PHP Date Format /Date(1365004652303-0500)/
PHP Recursion to Get All Possibilities of Strings
Passing PHP Variable from Controller to JavaScript Function in the Head of a View
PHP Add Elements to Multidimensional Array with Array_Push
How to Insert Multiple Checkbox Values into a Table
How to Remember Input Data in the Forms Even After Refresh Page
How to Read Png Metadata from PHP
Why Should I Fix E_Notice Errors
Get the Latest File Addition in a Directory
Codeigniter - Best Place to Declare Global Variable
PHP HTML Domdocument Getelementbyid Problems
How to Capture the Result of Var_Dump to a String
Email from PHP Has Broken Subject Header Encoding
PHP Combine Two Associative Arrays into One Array