SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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'],'"'
    $qid and $q1r are values from a DB. The reason each question is named like answer[$qid] is to do with the way I insert them into the database. I loop through the HTTP_POST_VARS array with a foreach looking for every element in the 'answer' array.

    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';

    Any help with this is appreciated.

    Thanks,
    Martin

  2. #2
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    PHP manual bot bronze trophy Gaheris's Avatar
    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']),' />'

  4. #4
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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";} 
    However, it would be better if i could do this in a function.

    The part I am having a problem with now is with checkboxes. Any idea how I could get that working?

  5. #5
    PHP manual bot bronze trophy Gaheris's Avatar
    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?

  6. #6
    SitePoint Addict mcrumlish's Avatar
    Join Date
    Jan 2002
    Posts
    384
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 
    This sort of works but i am pretty sure there is a much better way of doing it! At the moment if i fill in 1 checkbox item and leave another question 9ie radio button) blank then it keeps the checkbox value. However, in this page if I decide I want to tick another checkbox it starts acting strangely and doesn't work or checks the wrong box.

    Thanks,
    Martin


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •