Get POST variable that is an array - php

All,
I have the following hidden variables:
<input type="hidden" name="chk[10]" value = "cats">
<input type="hidden" name="chk[13]" value = "dogs">
<input type="hidden" name="chk[14]" value = "fish">
I want to get these variables through POST and print them. How can I do this in PHP?
Thanks

foreach($_POST['chk'] as $key => $value) {
echo $key, ' => ', $value, '<br />';
}

$chks = $_POST["chk"];
print_r($chks);

Related

set if choice is correct for each input field using checkbox in php mysql [duplicate]

I have a form like the one below which is posted to contacts.php, and the user can dynamically add more with jQuery.
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
If I echo them out in PHP with the code below,
$name = $_POST['name'];
$email = $_POST['account'];
foreach($name as $v) {
print $v;
}
foreach($email as $v) {
print $v;
}
I will get something like this:
name1name2name3email1email2email3
How can I get those arrays into something like the code below?
function show_Names($n, $m)
{
return("The name is $n and email is $m, thank you");
}
$a = array("name1", "name2", "name3");
$b = array("email1", "email2", "email3");
$c = array_map("show_Names", $a, $b);
print_r($c);
so my output is like this:
The name is name1 and email is email1, thank you
The name is name2 and email is email2, thank you
The name is name3 and email is email3, thank you
They are already in arrays: $name is an array, as is $email
So all you need to do is add a bit of processing to attack both arrays:
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $key => $n ) {
print "The name is " . $n . " and email is " . $email[$key] . ", thank you\n";
}
To handle more inputs, just extend the pattern:
$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];
foreach( $name as $key => $n ) {
print "The name is " . $n . ", email is " . $email[$key] .
", and location is " . $location[$key] . ". Thank you\n";
}
E.g. by naming the fields like
<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />
<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />
<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />
(which is also possible when adding elements via JavaScript)
The corresponding PHP script might look like
function show_Names($e)
{
return "The name is $e[name] and email is $e[email], thank you";
}
$c = array_map("show_Names", $_POST['item']);
print_r($c);
You could do something such as this:
function AddToArray ($post_information) {
//Create the return array
$return = array();
//Iterate through the array passed
foreach ($post_information as $key => $value) {
//Append the key and value to the array, e.g.
//$_POST['keys'] = "values" would be in the array as "keys"=>"values"
$return[$key] = $value;
}
//Return the created array
return $return;
}
The test with:
if (isset($_POST['submit'])) {
var_dump(AddToArray($_POST));
}
This for me produced:
array (size=1)
0 =>
array (size=5)
'stake' => string '0' (length=1)
'odds' => string '' (length=0)
'ew' => string 'false' (length=5)
'ew_deduction' => string '' (length=0)
'submit' => string 'Open' (length=4)
I came across this problem as well. Given 3 inputs: field[], field2[], field3[]
You can access each of these fields dynamically. Since each field will be an array, the related fields will all share the same array key. For example, given input data:
Bob, bob#bob.com, male
Mark, mark#mark.com, male
Bob and his email and sex will share the same key. With this in mind, you can access the data in a for loop like this:
for($x = 0; $x < count($first_name); $x++ )
{
echo $first_name[$x];
echo $email[$x];
echo $sex[$x];
echo "<br/>";
}
This scales as well. All you need to do is add your respective array vars whenever you need new fields to be added.
You can use an array of fieldsets:
<fieldset>
<input type="text" name="item[1]" />
<input type="text" name="item[2]" />
<input type="hidden" name="fset[]"/>
</fieldset>
<fieldset>
<input type="text" name="item[3]" />
<input type="text" name="item[4]" />
<input type="hidden" name="fset[]"/>
</fieldset>
I added a hidden field to count the number of the fieldsets.
The user can add or delete the fields and then save it.
However, VolkerK's solution is the best to avoid miss couple between email and username. So you have to generate HTML code with PHP like this:
<? foreach ($i = 0; $i < $total_data; $i++) : ?>
<input type="text" name="name[<?= $i ?>]" />
<input type="text" name="email[<?= $i ?>]" />
<? endforeach; ?>
Change $total_data to suit your needs. To show it, just like this:
$output = array_map(create_function('$name, $email', 'return "The name is $name and email is $email, thank you.";'), $_POST['name'], $_POST['email']);
echo implode('<br>', $output);
Assuming the data was sent using POST method.
This is an easy one:
foreach($_POST['field'] as $num => $val) {
print ' ' . $num . ' -> ' . $val . ' ';
}
Already is an array.
My inputs are:
<input name="name[]" value='joe'>
<input name="lastname[]" value='doe'>
<input name="name[]" value='jose'>
<input name="lastname[]" value='morrison'>
In the $_POST data, returns the following:
[name] => Array
(
[0] => 'joe'
[1] => 'jose'
)
[lastname] => Array
(
[0] => 'doe'
[1] => 'morrison'
)
You can access to these data, in the following way:
$names = $_POST['name']
$lastnames = $_POST['lastname']
// accessing
echo $names[0]; // joe
This way It is very useful for creating pivot tables.
Using this method should work:
$name = $_POST['name'];
$email = $_POST['account'];
while($explore=each($email)) {
echo $explore['key'];
echo "-";
echo $explore['value'];
echo "<br/>";
}

Best way to submit many same-record? [duplicate]

I have a form like the one below which is posted to contacts.php, and the user can dynamically add more with jQuery.
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
<input type="text" name="name[]" />
<input type="text" name="email[]" />
If I echo them out in PHP with the code below,
$name = $_POST['name'];
$email = $_POST['account'];
foreach($name as $v) {
print $v;
}
foreach($email as $v) {
print $v;
}
I will get something like this:
name1name2name3email1email2email3
How can I get those arrays into something like the code below?
function show_Names($n, $m)
{
return("The name is $n and email is $m, thank you");
}
$a = array("name1", "name2", "name3");
$b = array("email1", "email2", "email3");
$c = array_map("show_Names", $a, $b);
print_r($c);
so my output is like this:
The name is name1 and email is email1, thank you
The name is name2 and email is email2, thank you
The name is name3 and email is email3, thank you
They are already in arrays: $name is an array, as is $email
So all you need to do is add a bit of processing to attack both arrays:
$name = $_POST['name'];
$email = $_POST['account'];
foreach( $name as $key => $n ) {
print "The name is " . $n . " and email is " . $email[$key] . ", thank you\n";
}
To handle more inputs, just extend the pattern:
$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];
foreach( $name as $key => $n ) {
print "The name is " . $n . ", email is " . $email[$key] .
", and location is " . $location[$key] . ". Thank you\n";
}
E.g. by naming the fields like
<input type="text" name="item[0][name]" />
<input type="text" name="item[0][email]" />
<input type="text" name="item[1][name]" />
<input type="text" name="item[1][email]" />
<input type="text" name="item[2][name]" />
<input type="text" name="item[2][email]" />
(which is also possible when adding elements via JavaScript)
The corresponding PHP script might look like
function show_Names($e)
{
return "The name is $e[name] and email is $e[email], thank you";
}
$c = array_map("show_Names", $_POST['item']);
print_r($c);
You could do something such as this:
function AddToArray ($post_information) {
//Create the return array
$return = array();
//Iterate through the array passed
foreach ($post_information as $key => $value) {
//Append the key and value to the array, e.g.
//$_POST['keys'] = "values" would be in the array as "keys"=>"values"
$return[$key] = $value;
}
//Return the created array
return $return;
}
The test with:
if (isset($_POST['submit'])) {
var_dump(AddToArray($_POST));
}
This for me produced:
array (size=1)
0 =>
array (size=5)
'stake' => string '0' (length=1)
'odds' => string '' (length=0)
'ew' => string 'false' (length=5)
'ew_deduction' => string '' (length=0)
'submit' => string 'Open' (length=4)
I came across this problem as well. Given 3 inputs: field[], field2[], field3[]
You can access each of these fields dynamically. Since each field will be an array, the related fields will all share the same array key. For example, given input data:
Bob, bob#bob.com, male
Mark, mark#mark.com, male
Bob and his email and sex will share the same key. With this in mind, you can access the data in a for loop like this:
for($x = 0; $x < count($first_name); $x++ )
{
echo $first_name[$x];
echo $email[$x];
echo $sex[$x];
echo "<br/>";
}
This scales as well. All you need to do is add your respective array vars whenever you need new fields to be added.
You can use an array of fieldsets:
<fieldset>
<input type="text" name="item[1]" />
<input type="text" name="item[2]" />
<input type="hidden" name="fset[]"/>
</fieldset>
<fieldset>
<input type="text" name="item[3]" />
<input type="text" name="item[4]" />
<input type="hidden" name="fset[]"/>
</fieldset>
I added a hidden field to count the number of the fieldsets.
The user can add or delete the fields and then save it.
However, VolkerK's solution is the best to avoid miss couple between email and username. So you have to generate HTML code with PHP like this:
<? foreach ($i = 0; $i < $total_data; $i++) : ?>
<input type="text" name="name[<?= $i ?>]" />
<input type="text" name="email[<?= $i ?>]" />
<? endforeach; ?>
Change $total_data to suit your needs. To show it, just like this:
$output = array_map(create_function('$name, $email', 'return "The name is $name and email is $email, thank you.";'), $_POST['name'], $_POST['email']);
echo implode('<br>', $output);
Assuming the data was sent using POST method.
This is an easy one:
foreach($_POST['field'] as $num => $val) {
print ' ' . $num . ' -> ' . $val . ' ';
}
Already is an array.
My inputs are:
<input name="name[]" value='joe'>
<input name="lastname[]" value='doe'>
<input name="name[]" value='jose'>
<input name="lastname[]" value='morrison'>
In the $_POST data, returns the following:
[name] => Array
(
[0] => 'joe'
[1] => 'jose'
)
[lastname] => Array
(
[0] => 'doe'
[1] => 'morrison'
)
You can access to these data, in the following way:
$names = $_POST['name']
$lastnames = $_POST['lastname']
// accessing
echo $names[0]; // joe
This way It is very useful for creating pivot tables.
Using this method should work:
$name = $_POST['name'];
$email = $_POST['account'];
while($explore=each($email)) {
echo $explore['key'];
echo "-";
echo $explore['value'];
echo "<br/>";
}

Generate multiple Form Inputs php

How do i get the values when submitted
I am generating the input via a loop based on the users selection but don't know how to retrieve the input values via post method
here is a sample of what i have
// string is based on database values it can be anything which i can't tell
Example code
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach($exp as $value){
print '<input type="text" name="'.$value.'[]" value="" />
}
You don't have to use name array (name="blabla[]")
$string = 'math,english,biology';
$exp = explode(',', $string);
if ($_POST) {
foreach ($exp as $name) {
if (isset($_POST[$name])) {
echo 'input ' . $name . ' is ' . $_POST[$name] . '<br>';
}
}
exit();
}
echo '<form method="post">';
foreach($exp as $value){
print '<input type="text" name="'.$value.'" value="" />';
}
echo '<button type="submit">Submit</button></form>';
Enter a, b, c to each input and submit. Here is the result:
input math is a
input english is b
input biology is c
Put the value in value="", name the field and make it an array [].
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $value) {
echo '<input type="text" name="fieldName[]" value="<?= htmlentities($value) ?>" />
}
Then it will be accessible in *$_POST['fieldName'] as an array.
*presuming you are using method="POST" on the form
If math,english,biology are form keys, then do:
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $key) {
echo '<input type="text" name="fieldName[<?= htmlentities($key) ?>]" value=""/>
}
or
$string = 'math,english,biology';
$exp = explode(',', $string);
foreach ($exp as $key) {
echo '<input type="text" name="<?= htmlentities($key) ?>" value=""/>
}

Dynamic $row output from array

I am trying to achieve dynamic $row values from an array, and show them as output. It keeps showing nothing while the values are already there.
This is what I have so far, where:
$row["lg_".$val.""]; should return:
$lg_it
'it' is the $val from the array.
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row["lg_".$val.""];
<input type="text" name="lg_$val" value="$shrtKey">
}
Anyone an idea?
What you have should result in a syntax error. Try the following:
<?php
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row['lg_'.$val];
?>
<input type="text" name="lg_<?= $val ?>" value="<?= $shrtKey ?>">
<?php
}
You've missed to echo your input-field:
foreach($arrMapCookieToLang as $key => $val) {
$shrtKey = $row["lg_".$val.""];
echo '<input type="text" name="lg_' . $val .'" value="' . $shrtKey . '">';
}
Further, if you don't use the array key in the foreach-loop, you can omit the $key =>-part and just write
foreach($arrMapCookieToLang as $val) {
// ...
}
Instead, the
<input type="text" name="lg_$val" value="$shrtKey">
Maybe you should use the
echo "<input type=\"text\" name=\"lg_" . "$val\" value=\"$shrtKey\">";

How to print_r $_POST array?

I have following table.
<form method="post" action="test.php">
<input name="id[]" type="text" value="ID1" />
<input name="value[]" type="text" value="Value1" />
<hr />
<input name="id[]" type="text" value="ID2" />
<input name="value[]" type="text" value="Value2" />
<hr />
<input name="id[]" type="text" value="ID3" />
<input name="value[]" type="text" value="Value3" />
<hr />
<input name="id[]" type="text" value="ID4" />
<input name="value[]" type="text" value="Value4" />
<hr />
<input type="submit" />
</form>
And test.php file
<?php
$myarray = array( $_POST);
foreach ($myarray as $key => $value)
{
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
?>
But it is only returning this: <p>0</p><p>Array</p><hr />
What I'm doing wrong?
The foreach loops work just fine, but you can also simply
print_r($_POST);
Or for pretty printing in a browser:
echo "<pre>";
print_r($_POST);
echo "</pre>";
<?php
foreach ($_POST as $key => $value) {
echo '<p>'.$key.'</p>';
foreach($value as $k => $v)
{
echo '<p>'.$k.'</p>';
echo '<p>'.$v.'</p>';
echo '<hr />';
}
}
?>
this will work, your first solution is trying to print array, because your value is an array.
$_POST is already an array, so you don't need to wrap array() around it.
Try this instead:
<?php
for ($i=0;$i<count($_POST['id']);$i++) {
echo "<p>".$_POST['id'][$i]."</p>";
echo "<p>".$_POST['value'][$i]."</p>";
echo "<hr />";
}
?>
NOTE: This works because your id and value arrays are symmetrical. If they had different numbers of elements then you'd need to take a different approach.
You are adding the $_POST array as the first element to $myarray. If you wish to reference it, just do:
$myarray = $_POST;
However, this is probably not necessary, as you can just call it via $_POST in your script.
Why are you wrapping the $_POST array in an array?
You can access your "id" and "value" arrays using the following
// assuming the appropriate isset() checks for $_POST['id'] and $_POST['value']
$ids = $_POST['id'];
$values = $_POST['value'];
foreach ($ids as $idx => $id) {
// ...
}
foreach ($values as $idx => $value) {
// ...
}
Just:
foreach ( $_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
Because you have nested arrays, then I actually recommend a recursive approach:
function recurse_into_array( $in, $tabs = "" )
{
foreach( $in as $key => $item )
{
echo $tabs . $key . ' => ';
if( is_array( $item ) )
{
recurse_into_array( $item, $tabs . "\t" );
}
else
{
echo $tabs . "\t" . $key;
}
}
}
recurse_into_array( $_POST );
Came across this 'implode' recently.
May be useful to output arrays. http://in2.php.net/implode
echo 'Variables: ' . implode( ', ', $_POST);
$_POST is an array in itsself you don't need to make an array out of it. What you did is nest the $_POST array inside a new array. This is why you print Array.
Change it to:
foreach ($_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
$_POST is already an array. Try this:
foreach ($_POST as $key => $value) {
echo "<p>".$key."</p>";
echo "<p>".$value."</p>";
echo "<hr />";
}
As you need to see the result for testing purpose. The simple and elegant solution is the below code.
echo "<pre>";
print_r($_POST);
echo "</pre>";

Categories