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>";
Related
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\">";
I'm building a very basic questionnaire (with response page) and it works but my form takes up too much space and looks clunky. How would i loop these question arrays so i can save space?
Here is a small bit of my code asking 2 questions:
<h3>Organisation?</h3>
<p><b>It was well organised</b><p>
<?php
$answers = array("Disagree","Agree");
for($i=0;$i<2;$i++) {
print '<input type="radio" name="org" value="'.$answers[$i].'">'.$answers[$i];
};
?>
</p>
<p><b>Adequate materials?</b><p>
<?php
$answers = array("Disagree","Agree");
for($i=0;$i<2;$i++) {
print '<input type="radio" name="lib" value="'.$answers[$i].'">'.$answers[$i];
};
?>
</p>
You should put the two rows in one php tag and use foreach instead, also you should let your php print the headers.
<?php
$answers= array("Disagree","Agree");
echo "<p><b>It was well organised</b></p><p>";
foreach ($answers as $value) {
print '<input type="radio" name="org" value="'.$value.'"> '.$value;
};
echo "</p>";
echo "<p><b>Adequate materials?</b></p><p>";
foreach ($answers as $value) {
print '<input type="radio" name="lib" value="'.$value.'"> '.$value;
};
echo "</p>";
?>
Sidenote: You have 4 opening p tags <p> and just 2 closing tags </p> in your own code. I added another two closing tags in my code where I expected them to be.
You could do:
<?php $questions = array(
'org' => 'It was well organized',
'lib' => 'Adequate materials?',
); ?>
<h3>Organisation?</h3>
<?php foreach ($questions as $f => $question): ?>
<p><b><?= $question; ?></b></p>
<?php foreach (array('Disagree', 'Agree') as $answer): ?>
<input type="radio" name="<?= $f ?>" value="<?= $answer ?>"> <?= $answer ?>
<?php endforeach; ?>
<?php endforeach; ?>
I think you are looking for something like this:
<h3>Organisation?</h3>
<?php
$answers = array("Disagree","Agree");
$first = ""; $second = "";
foreach($answers as $answer) {
$first .= '<input type="radio" name="org" value="' . $answer . '">' . $answer;
$second .= '<input type="radio" name="lib" value="' . $answer . '">' . $answer;
}
echo "<p><b>It was well organised</b></p><p>" . $first . "</p>";
echo "<p><b>Adequate materials?</b></p><p>" . $second . "</p>";
?>
Why not just make it a function?
function answers($name, $array)
{
$returnStr="";
for($i=0;$i<count($array);$i++)
{
$returnStr.='<p><input type="radio" name="'.$name.'" value="'.$array[$i].'"/> '.$array[$i].'</p>';
}
return $returnStr;
}
Then just call it
<h3>Organisation?</h3>
<p><b>It was well organised</b></p>
<?php echo answers("org",array("Agree","Disagree"));?>
I get two warnings Warning: Invalid argument supplied for foreach() but I still retrieve my expected post variables. Where am I going wrong? It says the warning is in regard to line 7, which in this case is the line that begins; foreach($value as $k => $v)
<!------------- quote.php ----------------->
<body>
What services are you interested in? <br/><br/>
<form name="input" action="quote2.php" method="post">
<?php
$services = array('Tree Felling', 'Height Reduction', 'Crown Thinning', 'Deadwooding/Ivy Removal', 'Stump Grinding', 'Other');
foreach ($services as $option) { ?>
<input id="<?= $option ?>" type="checkbox" name="services[]" value="<?= $option ?>" />
<label for="<?= $option ?>"><?= $option ?></label>
<br />
<? }
?>
<br/>
<input name="name" type="text" />NAME</br>
<input name="place" type="text"/>TOWN</br/>
<input type="submit" value="Submit">
</form>
</body>
<!------------ quote2.php -------------->
<?php
echo '<h3>SERVICES REQUIRED</h3>';
foreach ($_POST as $key => $value) {
foreach($value as $k => $v)
{
echo '<p>'.$v.'</p>';
}
}
echo "<hr /><h3>DETAILS</h3>";
echo $name = $_POST['name'];
echo "</br>";
echo $place = $_POST['place'];
echo "<hr/>"
?>
A number of the form controls do not have names that end in [] and so are not arrays.
You can't loop over a string.
You should pull out each value of the submitted data individually and only loop over services.
The problem is that there is a very real possibility that not all of the items in your $_POST array will have values of type array; which you are assuming according to the code in quote2.php
A simple is_array() check will ensure that only arrays get iterated over in the foreach, here is the edited file contents:
<!------------ quote2.php -------------->
<?php
echo '<h3>SERVICES REQUIRED</h3>';
foreach ($_POST as $key => $value) {
if (is_array($value)) {
foreach($value as $k => $v)
{
echo '<p>'.$v.'</p>';
}
}
}
echo "<hr /><h3>DETAILS</h3>";
echo $name = $_POST['name'];
echo "</br>";
echo $place = $_POST['place'];
echo "<hr/>"
?>
That should do the trick.
Try wrapping your foreach loop in an if statement to check the array is an array. For example:
if (is_array($services)) {
foreach ($services as $option) {
// Some code
}
}
Although I wouldn't worry too much. This is just a cleaner way of writing your code.
I have two array. What I want to do is compare the key ['user_id'] of two arrays, if they are the same, pass the ['user_id'] and ['ref'] in hidden form. I tried to put them into two foreach, but system seems into a dead lock.
<?php foreach($_SESSION['printing_set'] as $data) { ?>
<?php foreach(getProvenaMailableUserlist() as $userlist){ ?>
<input type="hidden" name="reference[<?php echo $data['user_id'] ?>]" value="<? if($userlist['user_id'] == $data['user_id']){echo $userlist['ref'];} ?>" />
<?php } ?>
<?php } ?>
What is the right way to do that?
What you are doing is printing again and again the part of '<input type="hidden" name="...'. here is what you should do
<?php
echo '<input type="hidden" name="reference[' . $data['user_id'] . ']" value="'; //olny one time.
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
if($userlist['user_id'] == $data['user_id']){
echo $userlist['ref']; //only if condition is true
}
}
}
echo '" />'; //only one time
?>
You've got some funky formatting going on, so it's hard to tell where the error might be. Try it like this:
<?php
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
$value = "";
if($userlist['user_id'] == $data['user_id'])
$value = $userlist['ref'];
echo "<input type='hidden' name='reference$user_id' value='$value' /> \n";
}
}
?>
I am trying to add user input to a previously created array with array_merge. However, I am having trouble echoing the entire, new array as an unordered list. the user's entry is being processed correctly, but the original array is displaying as "Array" within the unordered list. Here is the code:
<?php
$travel = array("Automobile", "Jet", "Ferry", "Subway");
foreach ($travel as $t)
{
echo "<ul>";
echo "<li>$t</li>";
echo "</ul>";
}
?>
<form action="arrays.php" method="post">
<input type="text" name="added" />
<?php
foreach ($travel as $t)
{
echo "<input type=\"text\" name=\"travel[]\" value=\"$t\" />";
}
?>
<input type="submit" name="submit" value="Add More!" />
</form>
<?php
$travel = array($_POST["travel"]);
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
echo "<p> Here is the list with your additions:</p>";
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
?>
Try using a new variable name for the new array created by array_merge(). I think you may run into problems modifying the array you're storing into.
$travel = array($_POST["travel"]);
should be
$travel = $_POST['travel'];
The problem was solved thus:
if (isset($_POST["submit"]))
{
$travel = $_POST["travel"];
$added = explode(",", $_POST["added"]);
$travel = array_merge($travel, $added);
echo "<p> Here is the list with your additions:</p>";
echo "<ul>";
foreach ($travel as $t)
{
echo "<li>$t</li>";
}
echo "</ul>";
}
?>