SitePoint Sponsor |
|
User Tag List
Results 1 to 6 of 6
-
Jan 7, 2004, 05:11 #1
problem passing values in error checking
Hi,
I am working on a survey application. A user fills in different questions and submits the survey. At the moment I am working on error checking. I am doing this in PHP rather than javascript.
The way the survey is set up, it is split into different pages with a number of questions on each page. If, for example, a user is on a page with 5 questions and they only complete 4 then they will get an error message and the same 5 questions will be output. These questions are all created dynamically and are named like this:
PHP Code:echo '<input type="radio" name="answer[',$qid,']" value="',$q1r['aid'],'"';
When a user hits submit the error checking checks the number of passed values against the number of questions on the previous page. If this number is less than the number of questions then there is an error and the p[revious questions are loaded again. What I need to do is to make any of the questions that were completed, still maintain there values.
In pseudo code it is like this:
PHP Code:if(radio_button_value == question_variable_value_from_prev_page){ echo selected';
}
Thanks,
Martin
-
Jan 7, 2004, 06:51 #2
You could just use sessions to pass the variables back.
On your error checking store the answers in $_SESSION['answers'] (or something similar). Make sure of course to put session_start() at the top of the error checking page and the form page. In the form page you can just do:
PHP Code:if ($_SESSION['answer'] == radio_button_value){ echo selected;
}
Erh
-
Jan 7, 2004, 07:45 #3
- Join Date
- Oct 2003
- Location
- Germany
- Posts
- 2,195
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Is this what you're looking for?
PHP Code:function get_checked_answer($_qid, $_aid)
{
return ($_POST['anser'][$_qid] == $_aid) ? 'checked="checked"' : '';
}
echo '<input type="radio" name="answer[',$qid,']" value="',$q1r['aid'],'" ',get_checked_answer($qid, $q1r['aid']),' />';
-
Jan 7, 2004, 09:09 #4
I tried that code but it didn't work.
The value I want to check is the value that is passed from the previous page and I need to check this against the $q1r['aid'] so if i used the variable $qid that wouldnt work as that is just defined in my script from a DB entry. it is the ID of each question.
I have managed to get it working for radio buttons, it was simpler than i thought:
PHP Code:if($answer[$qid] == $q1r['aid']){echo ' checked><br>',"\n";}else{echo "><br>\n";}
The part I am having a problem with now is with checkboxes. Any idea how I could get that working?
-
Jan 7, 2004, 09:12 #5
- Join Date
- Oct 2003
- Location
- Germany
- Posts
- 2,195
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, I almost had the code correct, I just wasn't sure where the variables came from.
Can you post some sample check box markup, please?
-
Jan 7, 2004, 10:02 #6
Here's my code for checkboxes:
PHP Code:if($qrow['qtype'] == "checkbox"){
$qid = $qrow['qid'];
$q1 = "SELECT * FROM answers WHERE sid = '$sid' AND qid = '$qid'";
$r1 = mysql_query($q1) or die(mysql_error());
echo "<br>";
$totalarr = mysql_num_rows($r1);
while($q1r = mysql_fetch_array($r1)){
echo $q1r['answer'].' - <input type="checkbox" name=answer[',$qid,'][] value="',$q1r['aid'],'"';
$arrcount = 0;
while($arrcount < $totalarr){
if($answer[$qid][$arrcount] == $q1r['aid']){echo ' checked';}else{echo "";}
$arrcount++;
}
echo "><br>\n";
}//end checkbox loop
echo "<p>\n";
}//end if checkbox type question
Thanks,
Martin
Bookmarks