Receive data from $POST array - php

How i can take the data from test[] on action.php file?
<form action="action.php" method="post">
<input type="text" name="test[]"><br />
<input type="text" name="test[]"><br />
<input type="submit" value="invia" />
i try this but don't work. How can I print an array in separate data?
echo $_POST["test"];
echo $_POST["test"];

You have to use the index to get the specific items:
echo $_POST["test"][0];
echo $_POST["test"][1];
Or output the array:
print_r($_POST["test"]);
Or loop over it:
foreach($_POST["test"] as $item) {
echo $item;
}

Related

How can I get the array index from a button php

i have an array with objects that i have submitted to display in the form with foreach, i did it like that:
<?php session_start(); if(isset($_SESSION['objectList'])){
foreach($_SESSION['objectList'] as $object){
?>
<form action="control.php" method="post">
<input type="submit" name="op" value="-" /> <?php echo $object;?>
</form>
<?php }
}else{
echo "No hay objetos";
}
?>
When the "-" button is pressed, the position of the array of that button must be eliminated, that is done with the following code in another class:
unset($_SESSION['objectList'][$object]);
$_SESSION['objectList']=array_values($_SESSION['objectList']);
But I do not know how to send the index value of the pressed button
It looks like this
empty array
3 object in the array
you can add the index as a hidden input like this:
<?php session_start(); if(isset($_SESSION['objectList'])){
foreach($_SESSION['objectList'] as $index => $object){
?>
<form action="control.php" method="post">
<input type="hidden" name="index" value="<?= $index; ?>" />
<input type="submit" name="op" value="-" /> <?php echo $object;?>
</form>
<?php }
}else{
echo "No hay objetos";
}
?>
the index is then in the $_POST variable $_POST['index']
But be carefull if you use a numeric index, because if you unset it, then the indexes might not be correct anymore. Better use a associative array.

PHP parse array post

A page is posting an array to me like this:
<input type="text" name="fields[email_address][value]" value="1" />
<input type="text" name="fields[first_name][value]" value="jim" />
<input type="text" name="fields[zip_code][value]" value="45254" />...
An array.
I can loop through it like this easy enough
foreach ( $_POST['fields'] as $key => $field ) {
echo $key." ".$field['value'] ;
}
Result of above:
first_name jim
email_address 1
address_postal_code 45254
But what I really need to do is reference just the zip (45254) out of the array, maybe like:
echo $_POST['fields']['zip_code']; //or
echo $_POST['fields']['zip_code']['value'];
result: 45254
Is this possible?
Update
<input type="text" name="fields[zip_code][value]" value="45254" />
to be
<input type="text" name="fields[zip_code]" value="45254" />
Edit: I wasn't aware you can't modify the html, that wasn't specified in the original question.
The only thing you can really do is do:
$_POST['fields']['zip_code'] = $_POST['fields']['zip_code']['value'];
However at that point, you might as well just assign $_POST['fields']['zip_code']['value'] to a variable and use that.
If you can't update the html of the form, all you can do is manipulate the data after it's been assigned to the $_POST superglobal like it's any other array
Edit 2: Adding a complete snippet to try:
If you do, what do you get?:
<?php
foreach ( $_POST['fields'] as $key => $field ) {
echo $key." ".$field['value'] ."<br />";
}
echo $_POST['fields']['zip_code']['value'] . "<br />";
$_POST['fields']['zip_code'] = $_POST['fields']['zip_code']['value'];
echo $_POST['fields']['zip_code'];
?>
I just tried that with a simple form of:
<form method="post" action="test.php">
<input type="text" name="fields[email_address][value]" value="1" />
<input type="text" name="fields[first_name][value]" value="jim" />
<input type="text" name="fields[zip_code][value]" value="45254" />
<input type="submit" />
</form>
And it works as expected.
This seems sloppy but it worked:
foreach ( $_POST['fields'] as $key => $field ) {
$$key = $field['value'] ;
}
echo $address_postal_code;
45254
echo $_POST['fields']['zip_code']['value']
returns nothing

How to group $_POST variables into the right arrays

I am generating the below html form with a foreach loop. Within $people there are 4 $person arrays, and thus 4 repetitions of the below input set.
<form action="handler.php" method="post">
<?php foreach($people as $person) { ?>
<input type="text" name="first_name">
<input type="text" name="middle_name">
<input type="text" name="last_name">
<input type="hidden" name="<?php echo $person['id'];?>
<?php } ?>
</form>
When this is submitted, it only passes the last one, and I would like it to pass each set($person) as an array(as below), so then I can then have a nice array of each one to work with.
Array([0]=>Array(['first_name']=>'James'['middle_name']=>'Green'['last_name']=>'McIntosh')
[1]=>Array(['first_name']=>'Bian'['middle_name']=>'Chip'['last_name']=>'Simpson)'
etc..
But when I return in it, i can't figure out how to get each person into a separate array. I think it might require something a little tricky with the "name" attribute, but haven't been able to get it to work yet.
<?php
foreach ($people as $person) {
printf('<input type="text" name="person[%d][first_name]">', $person['id']);
printf('<input type="text" name="person[%d][middle_name]">', $person['id']);
printf('<input type="text" name="person[%d][last_name]">', $person['id']);
}
?>
Then:
var_dump($_POST['person']);
This is probably what you are looking for:
<form action="handler.php" method="post">
<?php foreach($people as $person) {
echo sprintf('<input type="text" name="first_name[%s]">', $person['id']);
echo sprintf('<input type="text" name="middle_name[%s]">', $person['id']);
echo sprintf('<input type="text" name="last_name[%s]">', $person['id']);
} ?>
</form>

Storing HTML form values into an array in PHP and printing

I am trying to get user input of 10 numbers in an html file that will be stored into an array on a php file. Although when I try to print the array out all I am getting is the word "Array", any idea as to why?
HTML File:
<!DOCTYPE HTML>
<html>
<head>
<meta charset= "utf-8">
</head>
<body>
Please enter 10 numbers that will be stored in an array:
<form action="practice3.php" method="post">
<input type="text" name="number1[]" placeholder="First number"><br />
<input type="text" name="number2[]" placeholder="Second number"><br />
<input type="text" name="number3[]" placeholder="Third number"><br />
<input type="text" name="number4[]" placeholder="Fourth number"><br />
<input type="text" name="number5[]" placeholder="Fifth number"><br />
<input type="text" name="number6[]" placeholder="Sixth number"><br />
<input type="text" name="number7[]" placeholder="Seventh number"><br />
<input type="text" name="number8[]" placeholder="Eighth number"><br />
<input type="text" name="number9[]" placeholder="Ninth number"><br />
<input type="text" name="number10[]" placeholder="Tenth number"><br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
PHP File:
<?php
$number1 = $_POST['number1'];
$number2 = $_POST['number2'];
$number3 = $_POST['number3'];
$number4 = $_POST['number4'];
$number5 = $_POST['number5'];
$number6 = $_POST['number6'];
$number7 = $_POST['number7'];
$number8 = $_POST['number8'];
$number9 = $_POST['number9'];
$number10 = $_POST['number10'];
$myArray = Array($number1, $number2, $number3, $number4, $number5, $number6, $number7, $number8, $number9, $number10);
echo $myArray;
?>
Just use print_r :
echo '<pre>';
print_r ($myArray);
echo '</pre>';
It will display information about your variable in a way that's readable by humans. can be used with arrays and objects.
Just to elaborate, echo expects a string and doesn't work for Arrays. In general when debugging people tend to use var_dump () which works on Objects, Arrays, Strings, etc.
You don't need the [] in your input names, number1, number2, ... will suffice. Then just use print_r to print the array.
print_r will work but if you want control over formating you will need to iterate over the array
foreach ($myArray as $value) {
echo "Value: $value<br />\n";
}

How do I receive post from a form where I name an input as <?php echo $var ?>?

I tried $_POST['<?php echo $var ?>] but I should have known that it wouldn't be that easy.
The reason why I try to do is because I have several input boxes with values I take from a database and I'm trying to create an updation script where any of the input box values can be changed.
for example
<form action="process.php" method="post">
<?php
while($variable=mysql_fetch_array($sqlconnec))
{
?>
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />
<?php
}
?>
<input type="submit" />
</form>
Any help is appreciated.
I think that what you need is:
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
$_POST[$col] //this will have the input value defined above.
In process.php you have to do the same query as mentioned above. If you iterate through those results $_POST[$col] will contain the posted values.
You need to do like this:
<form action="process.php" method="post">
<?php
$variable = mysql_fetch_assoc($sqlconnec);
foreach($variable as $col => $val)
{
?>
<input type="text" name="<?php echo $col; ?>" value="<?php echo $val; ?>" />
<?php
}
?>
<input type="submit" />
</form>
Now, mysql_fetch_assoc gets you the database row in a associative array. Then, the code iterates each column in the row and displays the name/value pair for it. And yes, you were not closing the value tag correctly.
foreach($_POST as $k=>$v) {
//do something with $v or $_POST[$k]
}
I think that you want to change the name of the input to something that is constant.
For example:
<input type="text" name="testname" value="<?php echo $variable['val'] ? />
And then retrieve your variable like so:
$_POST['testname']
For example you could print the variable you sent in the input to test it like so:
echo $_POST['testname'];
You are not closing your input 'value' tag with ". Also your second php closing tag is incorrect.
<input type="text" name="<?php echo $variable['col1']?>" value="<?php echo $variable['val'] ?>" />

Categories