1

is it possible to pass through $_POST or $_GET an array, with values in it, without using serialize() and unserialize() ? here is an example code, trying to find a number..i entered value 4 instead of rand, just to do the testing.. i thought of the potential of using a foreach to make multiple input hidden, in case i could pass all variables every single time, but it seems not to be working.. any ideas..??? or it is just not possible without serializing?

<?php
$x = $_POST['x'];
$Num = $_POST['Num'];
$first_name[] = $_POST['first_name'];
if (!$x)
{
Echo "Please Choose a Number 1-100 <p>";
$x = 4; //rand (1,4) ;
} 
else {
if ($Num >$x)
{Echo "Your number, $Num, is too high. Please try again<p>";}
elseif ($Num == $x)
{Echo "Congratulations you have won!<p>";
Echo "To play again, please Choose a Number 1-100 <p>";
$x = 4;// rand (1,4) ;
}
else 
{Echo "Your number, $Num, is too low. Please try again<p>";}
}
?> 
<form action = "<?php echo $_SERVER['PHP_SELF']; ?>" method = "post"> <p> 
Your Guess:<input name="Num" /> 
<input type = "submit" name = "Guess"/> <p> 
<input type = "hidden" name = "x" value=<?php echo $x ?>> 
<?php
foreach($first_name as $val){
echo "<input type=\"hidden\" name=\"first_name[]\" value=$val />";
}
?>
</form> 
</body> 
</html> 
7
  • Yes, you are able to use array in POST and GET (Usually checkboxes use this method). Can you explain the scenario a little bit more detail? I still cannot understand what you are trying to achieve. Commented Dec 4, 2012 at 23:46
  • Stephanus, this is just a game to guess a number..i just want to "store" in an array all numbers that user entered..and in each submit, to append the array and add the nea value.. Commented Dec 4, 2012 at 23:58
  • anyone has any idea on this one? Commented Dec 9, 2012 at 21:23
  • Hi nikolas, do you want to store the number in first_name[] or other variables? And does it mean that the value from Num should go to first_name[] as well? Commented Dec 9, 2012 at 23:41
  • hi Stephanus..i sorted out a possible solution of this...i actually post array every single time, by making multiple hidden inputs each one holding a value..this does actually do the trick.. what do you think of it..? do you have sth alternative to propose? Commented Dec 10, 2012 at 18:16

2 Answers 2

1
<?php
    foreach($first_name as $k => $val)
        echo "<input type='hidden' name='first_name[$k]' value='$val' />";

should work.

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

2 Comments

it doesn't mate..in the programmer tools i can see $first_name[0] value=Array no matter how many times it runs, it only gets this..
it does work for me ;) - $first_name is my array and i can retrieve the datas after submit with print_r($first_name);
0

here is the solution that i figured out...

    $x = $_POST['x'];
    $Num = $_POST['Num'];
    $first_name = $_POST['first_name']; //creating array from the begining
    $first_name[] = $Num; // add current num to next available slot in array
    $counter = $_POST['counter'] +1;
    if (!$x) // below this is the same..
    ....
    ....
    <input type = "hidden" name = "x" value=<?php echo $x; ?>> 
    <input type = "hidden" name = "counter" value=<?php echo $counter; ?>> //add a counter to count loops
    <?php 
    if ($counter>=2){ //counter in first load of page will be 1, so we need to read enter value from the next load of page
    for ($i=0; $i<=($counter-2); $i++){  // counter-2 gives us the actual number of elements
    echo "<input type=\"hidden\" name=\"first_name[]\" value=$first_name[$i] />";
    }
    }
    ?>
     </form> 

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.