Hi,
I am coding a survey system and at the moment I am working on the error checking. If a survey submission fails the error checking it goes back to the previous page. I want the survey questions to maintain the values they had before the error check.
Normally this would be easy but due to the way i am setting up the survey, it is more difficult. I am building a dynamic survey application with unlimited surveys and questions. Therefore, all question generation is dynamic. I insert the data into the database from the HTTP_POST_VARS array.
To get into detail of the problem, I generate the form elements for each survey question like this:
if($qrow['qtype'] == "radio"){
$qid = $qrow['qid'];
$q1 = "SELECT * FROM answers WHERE sid = '$sid' AND qid = '$qid'";
$r1 = mysql_query($q1) or die(mysql_error());
echo "<br>";
while($q1r = mysql_fetch_array($r1)){
echo $q1r['answer'],' - <input type="radio" name="answer[',$qid,']" value="',$q1r['aid'],'"';
if(${$answer[$qid]} == ${$q1r['aid']}){echo ' checked><br>',"\n";}else{echo "><br>\n";}
}//end radio button loop
echo "<p>\n";
In the code above you will see this if(${$answer[$qid] == ${$q1r['aid']
This code checks the variable variable ${$answer[$qid]} (which is the data that is passed from the error checking) against the current value of the form element.
In pseudo code its like this:
if(passed_value_for_this_element == form_value for this element){
echo "selected";
}
Here's another example. if I have a question with a radio button called "answer[q1]" and this has 3 options called "one", "two" and "three". If I select "three" and hit submit then in the next page $q1 == "three". This is then put into a hidden form field like this <input type="hidden" name="answer[q1]" value="three"> In the error checking the page is submitted, it fails the check and the hidden form field values are passed to the next page. i then want to do a check for each question to see which answers were already completed.
The check should check each element as it is created for the variable name (which would be passed as $answer[q1]) and then check the value of each element (for example, radio buttons) If that elements value is equal to the value if the passed variable of the same name then we know it was selected on the previous page and we can echo "selected" in the HTML.
The problem is, the code I have isn't working and I think it is to do with the variable variables.
Hopefully that explanation makes some sense!
Any help with this is appreciated.
Thanks,
Martin