2012-12-04 54 views
1

是否有可能通过$ _POST或$ _GET数组,并使用值,而不使用serialize()和unserialize()? 这里是一个示例代码,试图找到一个数字..我输入值4而不是兰特,只是为了测试.. 我想到使用foreach使多个输入隐藏的可能性,以防万一我可以通过所有变量每一次,但它似乎不工作.. 任何想法..?或没有序列化是不可能的?传递数组值php

<?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> 
+0

是的,你可以在POST使用阵列和GET(通常复选框使用此方法)。你能解释一下这个场景的更多细节吗?我仍然无法理解你想达到的目标。 –

+0

Stephanus,这只是一个猜测数字的游戏..我只是想在数组中“存储”所有用户输入的数字..并且在每次提交时,要追加数组并添加nea值。 – nikolas

+0

任何人都有任何想法在这一个? – nikolas

回答

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

应该工作。

+0

它不配对..在程序员工具中我可以看到$ first_name [0] value = Array无论它运行多少次,它只会得到这个.. – nikolas

+1

它对我有用;) - $ first_name是我的数组,我可以在用print_r($ first_name)提交后检索数据; – Matoeil

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>