Can you please help me in this context.
<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
</form>
I am using this code in a loop and I need to get all the values in another page.
How can I get these value as array in another page.
you should be able to use $_POST array in the sample.php page
PHP's $_POST Array, taken from the docs:
An associative array of variables passed to the current script via the HTTP POST method.
first, add s submit button to your form, it should look like this:
<form action="sample.php" method="post">
<input type="text" name="first[]" />
<input type="text" name="second[]" />
<input type="submit">
</form>
and in sample.php:
<?php
print_r($_POST);
?>
This is the most simple example,
once you get the hang of it, I recommend you read the docs on topics such as:
htmlspecialchars
htmlspecialchars — Convert special characters to HTML entities
You can access it as array
$_POST['first'][0] // 0 will be index for single
to list all just loop
foreach($_POST['first'] as $first) {
...
}
Your values will end up in $_POST, and because you're using the [] syntax, you'll get an array. So, to get your first array, do:
$first = $_POST['first'];
You could then iterate over it like so:
foreach ($first as $item) {
...
}
I think this will help you.
$first=$_POST['first'];
$second=$_POST['second'];
foreach($first as $key=>$first){
echo 'Your first value'.$first.'Your second value'.$second[$key];
}
Related
It was easy passing data from controller to view. Also, to pass data from view to controller you need form tags. But, how do you pass an array from an input form? any idea guys? here's what the array looks like:
$test = array
(
array($employee_id[0],$name[0],$days_worked[0],$overtime_hours[0]),
array($employee_id[1],$name[1],$days_worked[1],$overtime_hours[1]),
array($employee_id[2],$name[2],$days_worked[2],$overtime_hours[2])
);
and from my html view i got here an input form:
<input name="test" type="text" class="form-control" id="test" value="<?php echo $test;?>">
and when i got to my model to test if it gotten the data inside the array:
$this->test = $_POST['test'];
echo $test = $_POST['test'];
all i got was a String "Array". I can't access what's inside the array. I need help.
Your questions is unclear.. you want to pass the array to you view, or to the HTML output into the INPUT element?
These are 2 different things, as one is only passing an array internally in you application (on the server), and the second is passing it into you form data, displaying it in the browser, and then sending the form to the server and getting the data there.
For the first - i see no problem there, as passing variables is no issue
The second - it is not possible by default to pass a multi-dimensional array into a form input. Becasue the input has only 1 dimension.
You could pass the data with some conversion function e.g. as a JSON string
value="<?php echo json_encode($test);?>"
and then load it like this:
$this->test = json_decode($_POST['test']);
But that does not make sense in the frontend I guess, as the user would not understant what data is presented in the input field.
To to it in a logical way, I would divide the data into groups, and then display accordingly in more input fields e.g.
<input name="test[0][employee_id]" type="text" class="form-control" id="test" value="<?php echo $test[0][0];?>">
<input name="test[0][employee_name]" type="text" class="form-control" id="test" value="<?php echo $test[0][1];?>">
<input name="test[0][employee_days_worked]" type="text" class="form-control" id="test" value="<?php echo $test[0][2]?>">
<input name="test[0][employee_overtime]" type="text" class="form-control" id="test" value="<?php echo $test[0][3];?>">
but do it more nicely. This way you can create sth which will be sent as a multi-dim. array to the php script.
as suggested in another answer here, you can see the structure with var_dump($test)
Array values cant be passed in form data directly.
You should use json_encode.
In your view file
$encoded_text = echo json_encode($test);
<input name="test" type="text" class="form-control" id="test" value="<?php echo $encoded_text ;?>">
Now in your model just decode this
$test = json_decode($test, $assoc = TRUE);
If you use serialize() on your controller and unserialize() on the view you should be able to access it in the same way. I believe that's what you're asking.
sorry to ask this dummy question. I have an HTML form like below that will result in a bunch of data lines to be parsed later by my PHP codes. But I get a bit stuck for how to get the data before I can parse and process it.
<form method="post" action="https://domain.com/cgi-bin/cgi.exe">
<input name="exec" value="viewproduct" type="hidden">
<input name="customer" value="customer_name" type="hidden">
<input name="sku" value="sku_number" type="hidden"> <br>
<input type="submit">
</form>
I need to store the result data in a string variable that I can parse it later line by line into multiple arrays.
Submit the data to (via the action attribute) a PHP program instead of a compiled Windows executable
Use $_POST['form_control_name']
$_POST holds your formdata. Access data like:
$_POST['exec'];
To string with for example delimiter ','
implode(',', $_POST);
Use $_POST[] array.
$exec = $_POST['exec'];
$customer = $_POST['customer'];
$sku = $_POST['sku'];
This may be a stupid question but I'm lost here. I need to send array with some data in it to another PHP file using POST variable. This is my form:
<form action="test.php" method="post">
<label name="html[]" hidden><?php echo $array; ?></label>
<input type="submit" value="submit">
</form>
And this is test.php
<?php
$html = $_POST['html'];
for($i = 1; $i<=9; $i++){
echo $html[$i];
}
?>
So this is what I tried, but it's not displaying anything. please help
You need to create a number of input elements with the same name, each of which will have one array item as its value:
<?php foreach ($array as $item) : ?>
<input type="hidden" name="html[]" value="<?= htmlspecialchars($item); ?>" />
<?php endforeach; ?>
Important points to keep in mind:
$item must always be a scalar value (string, integer, etc). You cannot pass in arrays piecemeal with this technique.
Never forget that since you are injecting variables into HTML output you must escape and/or sanitize them properly. In this case this is done with htmlspecialchars, which must know about your output encoding to work correctly in general (look up its third parameter).
There is also an alternative approach that can be used to pass arrays piecemeal through serialization:
<input type="hidden" name="html"
value="<?= htmlspecialchars(serialize($array)); ?>" />
And you would then unserialize it on the receiving end:
$html = unserialize($_POST['html']);
I 'm mostly including this option for completeness, as in practice session variables are a much better way of passing complex state between requests.
Is it necessary to put the data of the array in a hidden field? You can store the array in $_SESSION and access it. Btw, I think you have a problem, labels can be submitted, in that case you must put the data into an input field with type="hidden".
When I create i form - I do something like this:
<form name="form-name" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
[...some elements...]
<input type="submit" name="form-name" value="button">
</form>
Now I need to get the value of the name="" of the submit button, and not the actual value="".
In this case : "form-name".
And here's why:
When I submit a form; I write the action to database - and therefor need the name of the form submitted.
I know I can just have a hidden field with the form name. But I would like to make it simpler by just extracting the name from the submit button because I have a couple of other hidden form elements that I need to add on every single form I create to make my template system work.
And no javascript...
So, let's say your HTML form is this:
<form name="form-name" method="post" action="">
<input type="submit" name="form-name" value="button">
</form>
And you want to get what is inside name="form-name" in this case the form-name
Well, then in the PHP side you can, treat the $_POST global as associative array, and extract the key from it like this:
<?php
if(isset($_POST)){
foreach($_POST as $key=>$each){
echo $key; // this will output "form-name"
}
}
I might have come up with a solution to my question...
Here's a example form:
<form name="vehicle-vinNr" method="post" action="?<?=$_SERVER['QUERY_STRING']?>">
<input type="hidden" name="hello" value="world" readonly>
<input type="text" name="element">
<input type="submit" name="vehicle-vinNr" value="send">
</form>
First I need to extract and place the element-names into a new array:
<?php
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
$_FORM_ELEMENT_names[] = $_FORM_ELEMENT_name;
}
}
?>
In this case the array now contains:
hello
element
vehicle-vinNr
If the submit-button is, and always is, the last element in the form - this would work:
$_FORM_name = end($_FORM_ELEMENT_names); // vehicle-vinNr
But sometimes the submit-button is not the last element, so I needed to make a change:
If I always start the name of the submit-button with submit_ - e.g. submit__vehicle-vinNr or with multiple submit buttons for different actions like submit_update__vehicle-vinNr/submit_delete_vehicle-vinNr I can just do this:
if ($_POST){
foreach($_POST as $_FORM_ELEMENT_name => $_FORM_ELEMENT_value){
if(strstr($_FORM_ELEMENT_name,'submit_')){
$_FORM_ELEMENT_submit_name = explode('__',$_FORM_ELEMENT_name);
$_FORM_name = $_FORM_ELEMENT_submit_name[1]; // vehicle-vinNr
}
}
}
This is the solution I came up with - any thoughts?
Let me explain.
Normally when hidden fields are passed from a form to the page specified in the action of the form, those hidden fields can be accessed on the processing page like so:
The Form:
<form action="process.php" method="POST">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="hidden" name="loginTime" value="1:23PM" />
<input type="hidden" name="userIp" value="173.23.22.5" />
<input type="submit" name="submit" value="Submit" />
</form>
Processing Page (process.php):
<?php
if isset($_POST['submit']) {
echo $_POST['username'];
echo $_POST['loginTime'];
echo $_POST['userIp'];
}
?>
You see how I had to call the two hidden fields by name and individually. Is there any way I can call all hidden fields that are passed to a page from a form all at once despite what the field names of those are or how many there are?
In other words how do I make PHP do this:
// echo the contents of all hidden
fields here (if there were any)
EDIT
Additional info:
The form is designed in such a way (not the one above of course) that field names will be of the following sort:
product_name_1
product_quantity_1
product_price_1
product_name_2
product_quantity_2
product_price_2
and so incremented so on...
Depending on the user action there can be 3 hidden fields or thousands, there are no limits.
Make an array of valid hidden field names, then iterate through $_POST and if the $_POST field name is in the array of valid field names, echo them.
$valid = array('first_name', 'last_name');
foreach ( $_POST as $key => $value ) {
if ( in_array( $key, $valid ) ) {
echo $_POST[$key];
}
}
PHP does not care whether the field was hidden or not, HTTP doesn't tell PHP how it appeared on the website.
The closest thing I would come up with was saving all names of the hidden fields inside an array and echoing them all in a loop.
You can try the following:
<form action="process.php" method="POST">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="hidden" name="group_hidden[loginTime]" value="1:23PM" />
<input type="hidden" name="group_hidden[userIp]" value="173.23.22.5" />
<input type="submit" name="submit" value="Submit" />
</form>
And then print it:
echo htmlspecialchars(print_r($_POST, true));
This might give you a clue about how to solve this.
there's no way to tell the type of the input built-in, so instead you'll need to come up with a way to identify the ones you want yourself. This can be done either by coming up with a special naming scheme, or by storing a list of the names of the hidden fields in another field. I'd recommend the former option, since you don't have the risk of losing data integrity somehow. Look at using array_filter to parse through the array to get the specially-named fields out.
Maybe, assuming that your hidden fields will be in sequence (i.e. 1,2,3 and not 1,2,4) following all of the end users' actions (adding and taking away fields), you could try something along the lines of
$i = 1;
while(isset($_POST["product_name_$i"]))
{
echo $_POST["product_name_$i"];
echo $_POST["product_price_$i"];
$i++;
}
Or something along those lines?