Link to home
Start Free TrialLog in
Avatar of Neil_Bradley
Neil_BradleyFlag for New Zealand

asked on

echo specific information from a POSTED array

I have a form with input fields as follows:
<input name="fp[Title 1][]" type="text" id="test"  value=""/>
<input name="fp[Title 2][]" type="text" id="test"  value=""/>

I would like to output the form input like so:
<?php echo $fp[0][0]." ".$fp[0][1]; ?>
<?php echo $fp[1][0]." ".$fp[1][1]; ?>

I had though that the code might look something like this however this does not work.
<?php echo $_POST['fp'][0][0]." ".$fp[0][1]; ?>
<?php echo ['fp'][1][0]." ".$fp[1][1]; ?>
Can anyone show me best practice?
Cheers,
N
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

A question first: in your code I see

<input name="fp[Title 1][]"

Title 1 hasn't quotes, so it seems to be a constant, but what is its value? To be sure on how your fields are posted by your form, print the $_POST array content

if (isset($_POST['submit'])){

echo "<pre>"; // more readable output
var_dump($_POST); //print out the whole content of the $_POST array

Here you'll see the exact names of your fields. If you need help about, post here the output of the var_dump so I'll can help you better

Cheers
Agree with marqusG: var-dump() is  your friend.  But use it unconditionally.  In other words, do not test anything before calling it.  Just call it this way...
<?php // action script
error_reporting(E_ALL);
session_start(); // if needed
echo "<pre>";
var_dump($_POST);
echo "</pre>";
// REST OF PAGE GOES HERE

Open in new window

Also, you may want to learn about the nature of the "id" field in HTML.  I do not think you want to have two id's that are the same.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Neil_Bradley

ASKER

Thank you for you comments. I have re written the code to better illustrate my question
<?php error_reporting(E_ALL);
		 if (isset($_POST['fp'])){
$arrValues = array();
foreach( $_POST['fp'] as $list_id => $values ){
  foreach( $values as $k => $v ){
    $arrValues[] = $list_id.', ' .$v;
	echo '<li>'. $list_id .': '.$v.'</li><br>';
  }
}
		 }

?>
<form action='array.php' method='post' enctype="multipart/form-data">
<label for="fullName">title 1</label><input name="fp[Title 1 is][]" type="text" id="1"  value=""/><br />
<label for="email">title 2</label><input type="text" name="fp[Title 2 is][]" id="2"  value=""/><br />
<label for="email">title 3</label><input type="text" name="fp[Title 3 is][]" id="3"  value=""/><br />
<input type="submit" value="Register" id="submit" />
</form>

Open in new window

.

If you run this code (live view - http://www.beaconhilldesign.co.nz/sandbox/array.php) the entire contents of the array is successfully outputted using foreach.
My question is: If I wanted to output a specific part of the array as opposed to all of it , then how would this be achieved?
For example, if I wanted to output only the second array item (<?php echo $fp[0][0]." : ".$fp[0][1]; ?>) as opposed to listing the entire contents of the array what is the best way to achieve this?
Cheers,
N
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Let me say that I used

echo "<pre>";
var_dump($_POST);

If you do it, as me and Ray said, you see how info are formatted within the $_POST array:

array(1) {
  ["fp"]=>
  array(3) {
    ["Title 1 is"]=>
    array(1) {
      [0]=>
      string(4) "sasa"
    }
    ["Title 2 is"]=>
    array(1) {
      [0]=>
      string(4) "dada"
    }
    ["Title 3 is"]=>
    array(1) {
      [0]=>
      string(4) "caca"
    }
  }
}

This output leads you to the right way to output specific elements.
I said this for two reason: first, to allow you to understand better what we meant suggesting to use var_dump; second, to help you to correctly split points (if answer will be a good solution for you)

Cheers
Both yours and Rays comments are helping me to not only answer this question but also to comprehend the working of an array..
echo $_POST['fp']['Title 1 is'][0]; is close to answering  my question.

The above code will work to echo the value of the 0 however it relies on adding 'Title 1 is' in the square brackets before it.  I also want to echo the value of 'Title 1 is' without actually writing 'title 1 is' (as is show above) how would this be done? In the foreach code I assign the value of the field name to $list and output it that way..

My reasons for needing  to pass along the "title 1 is" text is that this will be a variable so I cant always bank of that text being the same.
Updated code here:

Open in new window

<?php error_reporting(E_ALL);
		 if (isset($_POST['fp'])){
$arrValues = array();
foreach( $_POST['fp'] as $list_id => $values ){
  foreach( $values as $k => $v ){
    $arrValues[] = $list_id.', ' .$v;
	echo '<li>'. $list_id .': '.$v.'</li><br>';
  }
} echo "The name and value of the form input field is:";
// this should echo Title 1 is : the value of the fleid

echo "<pre>";
var_dump($_POST);
		 }

?>
<form action='array.php' method='post' enctype="multipart/form-data">
<label for="fullName">title 1</label><input name="fp[Title 1 is][]" type="text" id="1"  value=""/><br />
<label for="email">title 2</label><input type="text" name="fp[Title 2 is][]" id="2"  value=""/><br />
<label for="email">title 3</label><input type="text" name="fp[Title 3 is][]" id="3"  value=""/><br />
<input type="submit" value="Register" id="submit" />
</form>

Open in new window

.
Cheers,
N
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Big thanks you to both Ray and marqusG for participating in my question..
marqusG, your last post has helped to clarify what was before missing from my comprehension of multi dimensional arrays. Ray, I will be sure to use the info from your second post to help properly understand the anatomy of an array.
Thanks again.
Neil
Thanks for the points.  Glad we could help -- it's a great question! ~Ray