1

是否可以通过 $_POST 或 $_GET 一个数组,其中包含值,而不使用 serialize() 和 unserialize() ?这是一个示例代码,试图找到一个数字..我输入值 4 而不是 rand,只是为了进行测试..我想到了使用 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 回答