1

serialize() および unserialize() を使用せずに、値を含む配列を $_POST または $_GET を介して渡すことは可能ですか? これは、数値を見つけようとするコードの例です..テストを行うために、randの代わりに値4を入力しました..すべての変数を渡すことができる場合に備えて、foreachを使用して複数の入力を非表示にする可能性を考えました毎回、しかしそれは機能していないようです..任意のアイデア..??? それともシリアライズしないと無理ですか?

<?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> 
4

2 に答える 2

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

動作するはずです。

于 2012-12-04T23:46:49.850 に答える
0

ここに私が考え出した解決策があります...

    $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> 
于 2012-12-10T18:14:57.207 に答える