Jump to content

sending array with post


TheNix

Recommended Posts

if I send an array with post and I try to print it out on the next page it ends up just printing out "Array". I'd rather post all the elements of the array. For example.

 

 <form action="seats.php" method="post">
<?php
foreach ($_POST as $value){
echo $value; 
echo "<input type=\"hidden\" name=\"pnames[]\" value=\"$value\">";
} ?>
Enter Names: <input type="text" name="name" /><br/>
Finished signing in? <input type="checkbox" name="ready" /><br/>
<input type="submit" />

 

The form is posting to itself and adding the new entered name to the array of names already posted to be used later. What is happening is when it hits the foreach it will loop once for the hidden variable where the $value is "Array" no matter how many values are in the array, and once for the text box. Any help is appreciated, thanks.

Link to comment
Share on other sites

The way I do it is-

 

I have several checkboxes:

 

<input type="checkbox" name="checkbox[]" value="whatever" />

 

And the php process-

 

foreach($_POST['checkbox'] as $key=>$value)

{

echo $value;

echo '<input type="hidden" name="checkbox" value="'.$value.'" />';

}

Link to comment
Share on other sites

well I'm trying to make a web based application that can take in names of poker players then assign them randomly to tables. My best idea was to keep a running array for every time someone enters a name and submits. Once they check the box that means they have all the names and I'd then have the form assign the names in the array randomly to a set amount of tables based on how many players there are.

 

I'm trying to get the array to be the hidden value and add to it the text value, so it will be pname[] + name (ready is just to check if i should make the seatings instead)

Link to comment
Share on other sites

jackpf has it right I think just need the [] in the loop. You want to add the new name so you would have to add it in after of before the loop.

 

<?php

foreach($_POST['pnames'] as $key=>$value)
{
echo $value;
echo '<input type="hidden" name="pnames[]" value="'.$value.'" />';
}

//Now see if the form was submitted and if so add the new hidden name input field
if (isset($_POST['submit'])) {
      //Was submitted so add new hidden input
     $name = $_POST['name'];
     echo '<input type="hidden" name="pnames[]" value="'.$name.'" />';
}

?>

  Didn't test this code but I think this might get you close to what you would like

 

Link to comment
Share on other sites

Sorry the I have been no help and wish I could remove my last post  ::)

 

So you are going to have one person submit all the names of the players one at a time?

Then once you hit a number you are going to create your tables with the players randomly

picked to fill them?

Link to comment
Share on other sites

<form action="seats.php" method="post">
<?php
foreach ($_POST as $key => $value){
echo $value; 
echo "<input type=\"hidden\" name=\"pnames[$key]\" value=\"$value\">";
} ?>
Enter Names: <input type="text" name="name" /><br/>
Finished signing in? <input type="checkbox" name="ready" /><br/>
<input type="submit" name="submit" />

 

See if that works how you want it.

 

On a side note, is there a reason you are not using sessions to do this?

 

<?php 
session_start();
$_SESSION['POST'] = $_POST;
?>
<form action="seats.php" method="post">
Enter Names: <input type="text" name="name" /><br/>
Finished signing in? <input type="checkbox" name="ready" /><br/>
<input type="submit" name="submit" />

 

seats.php

<?php
session_start();
$postData = isset($_SESSION['POST'])?$_SESSION['POST']:array();
$_POST = array_merge($_POST, $postData);

echo "<pre>" . print_r($_POST, 1) . "</pre>";
?>

 

Should give you a rough overview of how that would work.

Link to comment
Share on other sites

Well the session would not exactly work, as it would overwrite the previous data. At least in the sense I showed you.

 

What I would do, given that only 1 name per page load is allowed is still use sessions, but do this instead.

 

<?php 
session_start();

if (isset($_POST['name'])) {
    $_SESSION['names'][] = $_POST['name'];
}
?>
<form action="seats.php" method="post">
Enter Names: <input type="text" name="name" /><br/>
Finished signing in? <input type="checkbox" name="ready" /><br/>
<input type="submit" name="submit" />

 

Then to display them:

<?php
session_start();
$names = isset($_SESSION['names'])?$_SESSION['names']:array();

echo "<pre>" . print_r($names, 1) . "</pre>";
?>

 

Sorry for the first post, sometimes it makes sense in your head till you re-read it.

Link to comment
Share on other sites

Yeah, sorry guys, I forgot to put the [] at the end of the second checkbox.

 

But that's exactly what I use to allow people to delete multiple emails on my site, and it works. If it doesn't work exactly, it's on the right lines at least. Just experiement :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.