Re: error checking woes

From: Date: Mon, 01 Aug 2005 14:39:20 +0000
Subject: Re: error checking woes
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-219759@lists.php.net to get a copy of this message
Jack, Read below: Jack Jackson wrote:
Hi, Now that the drop down is working properly (thanks!!), I am trying to validate, and having LOTS of trouble. After being ceaselessly derided last night on an irc channel for my dimwitedness, I am still not any closer. The code which works is this: function GetQuestionsDropdown($cat){ //first get all the questions $sql = "SELECT * FROM questions WHERE questions.q_cat=$cat AND questions.q_style=1"; $result = mysql_query($sql);
    //now one-by-one go through the questions
    while($row = mysql_fetch_assoc($result)) {
               //if the form has been submitted, and the question unanswered
        //highlight this whole question and answer block in red.
        if ($message[$row['q_name']] == 1){
How does this funtion have access to $message? You need to a) make it global or b) pass it in.
            echo "<div class='error'>";
            }
               //Make a question set div wrapper
        echo "<div class='q_set'>\n";
        //State the question
        echo "<div class='question'>";
        echo $row['q_text'] . "</div>";
        echo "\n\n";
        echo " <div class='answer'>";
        echo "\n";
               //Create the dropdown for the answers
        echo '  <select name="' . $row['q_name'] . '">';
        echo "\n";
               //get all of the answers for THIS question
    $ans_sql = "select * from answers where answers.q_id=" . $row['q_id'];
    $ans_result = mysql_query($ans_sql);
        echo "    <option value=\"\">Select from this list</option>\n";
    while($ans_row = mysql_fetch_assoc($ans_result)) {
               //list the answers for THIS question
        echo "    <option ";
        echo "value=\"" . $ans_row['a_id'] . "\"";
        if  ($row['q_name'] == $ans_row['a_id']) {
            echo " selected";
            }
        echo ">" . $ans_row['a_answer'] . "</option>";
        echo "\n";
        }
        echo '  </select>' . "\n" . ' </div>' . "\n";
        echo "</div><!--q_set-->\n\n";
        //If there *was* an error div, close it
        if (sizeof($message[$row['q_name']])){
            echo "</div><!--/error-->";
            }
    }
    }//function GetQuestionsDropdown
NOW I have to validate after it's submitted. I am trying to look and see, if $_POST[$row['q_name']] is empty then make message of the same name = 1, so if if(empty($_POST['partners'])) {
       $message['partners'] = '1'
} Then when I restate the dropdown function, and it shows the questions again, those error checking things I built in will
    a) show a div class=error around questions with a message
    b) pre-select the previously selected answers (because I have put in the thing in the dropdown which says
if ($row['q_name'] == $ans_row['a_id']) { echo " selected";} The validate code I was trying, the subject of such howling on IRC (and I know it doesn't work, but not why) was: function ValidatePost($cat){
    //first get the q_names
$sql = "SELECT * FROM questions WHERE q_cat=$cat"; $result = mysql_query($sql); //go through the question set
    while($row = mysql_fetch_assoc($result)) {
          if(empty($_POST[$row['q_name']])){
          $message[$row['q_name']] == "1";
You want $message = "1" here. = = is for comparison. You're setting $message inside a function. This means it doesn't exist outside this scope!
      }
    }
}//function ValidatePost Can anyone tell me what I am doing wrong? Thanks! JJ
Hope this helps you get somewhere, kgt

« previous php.general (#219759) next »