0

I have been all over stack overflow and read the various answers to similar questions but I'm still experiencing a problem so I'm definitely missing something. My problem is simple:

I'm trying to pass an array from one page to another using POST. I'm not getting the response I want as demonstrated below. Any ideas?

Here is my form code:

$my_array = array("a", "b", "c", "d");

echo "<form action='post_to_this_page.php' method='post'>";
echo "<input type='hidden' name='book_ids[]' id='hiddenField' value=" $my_array " />";
echo "<input type='submit' name='formSubmit' value='Submit' />";
echo "</form>;

Here is "post_to_this_page.php:

$bookids = $_POST['book_ids'];
foreach($bookids as $thebookid)
{
     echo "Book Id : " . $thebookid . "<br />";
}

This is what prints out:

Book Id : Array

Obviously I'm trying to get a response like:

Book Id : a

Book Id : b

Book Id : c

Book Id : d

I'm sure I'm doing something fundamentally wrong but I can't figure out what it is.

Thanks in advance for looking at my problem. Any insight is greatly appreciated!

1
  • The code is ok. First, obviously, it loops once because you only assign one position to book_id array. If you want more positions, you have to create more field inputs with book_id[] as name. And finally, you are echoing an array which is not correct because you will get a nice string saying Array and for that reason prints out Array. Commented Feb 16, 2014 at 18:00

5 Answers 5

3

When you want to pass arrays in the POST, you must change your code a little bit. You should iterate trough your '$my_array' and add multiple hidden input fields using the following code:

foreach ($my_array as $identifier) {
    echo "<input type='hidden' name='book_ids[]' value='$identifier' />";
}

Also, I removed the 'id' field from your input field, because there is not just 1 anymore.

Sign up to request clarification or add additional context in comments.

Comments

0

Try more like:

$bookids = $_POST['book_ids'];
foreach($bookids as $thebookid)
{
     echo "Book Id : " . print_r($thebookid, true) . "<br />";
}

echo cannot echo array, so you can use print_r to find out the value.

1 Comment

This doesn't really explain why his code loops only once.
0

Either serialise your current value or have multiple inputs named book_id[] and listen for each $_POST['book_id']

Comments

0

You might try something like this

$my_array = array("a", "b", "c", "d");

echo "<form action='post_to_this_page.php' method='post'>";
echo "<input type='hidden' name='book_ids' id='hiddenField' value="implode('-', $my_array) " />";
echo "<input type='submit' name='formSubmit' value='Submit' />";
echo "</form>;

then turn it back to an array on the page you posted to.

$bookIds = explode('-',$_POST['book_ids']);
foreach($bookIds as $id)
    echo "Book Id: ".$id;

You're not actually passing an array, but the outcome is the same. just be sure your ids don't contain the character you're using for the delimiter('-' in y example.)

1 Comment

Thanks Bryan! Implementing your suggestion worked like a charm! Much appreciated. :)
0

You can do it using serialize:

<input name="my_var_name" value="<?php echo serialize($mi_array) ?>" 

Then, in server side:

$my_var=unserialize($_POST['my_var_name']);

You can send string using post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.