How to group $_POST variables into the right arrays - php

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>

Related

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]);
}
}

How to pass input type as array with comma seperated and read them in php

Hi friends am trying to pass the input type as array.
Here is my code..
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
echo sizeof($videoid);
?>
}
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
But am unable to read them for example I want to pass hello,how,are,you,why
but am unable to read them after when I pass them
Here's how I would do it. You only need one texbox. No need for multiple textbox. Try this:
<?php
if(isset($_POST['submit_tags'])){
$tagsList=$_POST['tags_list'];
$videoids = explode(",", $tagsList);
echo sizeof($videoids);
}
?>
Also you can remove the [] from your textbox name name="tags_list[]" to name="tags_list"
1) To send a input value as array to PHP, you need to set one value per input
<input type="text" name="tags_list[]" value="hello">
<input type="text" name="tags_list[]" value="world">
Now the PHP can understand $_POST['tag_list'] as array.
2) You can change your approach and split the string.
<input type="text" name="tags_list" value="hello, world">
And transform the $_POST['tag_list'] string into an array. Example:
$tag_list = explode(', ', $_POST['tag_list']);
Or you can use preg_split function to add more intelligence to your string transformation.
Regards,
To read(convert string) from array you can use implode
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
echo implode(",",$videoid);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>
Sravya try to understand the concept first, name as an array in a html element is used when you want to make more than one html element as same type so that you can get its value by iterating the array like:
<input type="text" name="tags_list[]"> -> its value is one
<input type="text" name="tags_list[]"> -> its value is two
<input type="text" name="tags_list[]"> -> its value is three
You can print its values like:
print_r($tags_list);
Otherwise use single html element.
try this.
$variableAry=explode(",",$variable); //you have array now
foreach($variableAry as $var)
{
echo $var. "<br/>";
}
<?php
if(isset($_POST['submit_tags'])){
$videoid=$_POST['tags_list'];
$tag_list = explode(', ', $videoid);
print_r($tag_list);
}
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="text" name="tags_list[]">
<input type="submit" name="submit_tags">
</form>

Receive data from $POST array

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;
}

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 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