Page 1 of 1

Help With Posting Arrays through Hidden Fields?

Posted: Mon Feb 11, 2008 12:53 pm
by Ryanc
Well ... I'm having problems using the "serialize()" and "unserialize()" functions to send an array through a hidden input in a form.

Here's two simple script pages. They go in a cycle ... The array is created in answer1.php which just makes the array and then processes it instead of recieveing the array and processing it, I havn;t included that one.

The array is simple numbers ... arr = array("1","2","3","4", ect.)

Answer.php :


Code: Select all

 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Quiz</title>
</head>
 
<body>
 
 
<?php 
$answer = $_POST['answer'];
$correct = $_POST['correct'];
$question = $_POST['question'];
 
 
$arr = unserialize($_POST['comparr']);
 
$random = array_rand($arr);
 
 
$nextq = $arr[$random];
 
$nextqn = $question + 1;
 
$newarray = array_splice($arr, array_search($nextq, $arr), 1);
 
if($answer == $correct) echo "<font color=green>CORRECT</font><br><form action=q" . $nextq . ".php method=post>
<input type=hidden name=question value=" . $nextqn . ">
<input type=hidden name=comparr value=" . serialize($newarray) . ">
<input type=submit name=Next value='Next Question'></form>";
else echo "INCORRECT<br><a href=index.php>Try again ...</a>";
 
 
 
?>
 
</body>
</html>
 
 

The hidden values in the answer.php page are then send to the next question which should be randomly picked from the previosly sent array and the question that has just ben answered should be removed from the array sot aht the same question does not come up 2 times, but the questions still come randomly.

Here is q2.php, which should be randomly picked, the next question is 'q" . $nextq . ".php' which is created in the php above the form.

q2.php:

Code: Select all

 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Quiz</title>
 
</head>
 
<body>
 
<h1>Q<?php 
$question = $_POST['question'];
echo $question; 
$array = unserialize($_POST['comparr']);
 
 
?>
 
 
</h1><br><b>Question?</b><br><br>
<form method="post" action="answer.php">
<input type="radio" name="answer" value="1" />answer1<br>
<input type="radio" name="answer" value="2" />answer2<br>
<input type="radio" name="answer" value="3" />answer3<br>
<input type="radio" name="answer" value="4" />answer4 - correct<br>
<input type="radio" name="answer" value="5" />answer5<br>
<input type="hidden" name="correct" value="4" />
<input type="hidden" name="question" value="
 
<?php echo $question; ?>" />
<input type=hidden name=comparr value=" . serialize($array) . ">
<input type="Submit" name="submit" value="Submit" />
</form>
</body>
</html>
 
 

I know that this is an awful way of doing this! I'm sorry about that but I don't have much experience in PHP and don't really know how to create sessions to use ect.

If anyone could rewrite or just give me tips onw hat to do with the script to make it work how I want it to work it would be great!

Thanks Alot!

Ryan

Re: Help With Posting Arrays through Hidden Fields?

Posted: Mon Feb 11, 2008 12:59 pm
by dayyanb

Re: Help With Posting Arrays through Hidden Fields?

Posted: Mon Feb 11, 2008 1:14 pm
by s.dot
Sessions would definitely be your best bet.

If you must pass it through a form, serialize() and unserialize().