Re: error checking woes
| From: | Jack Jackson | Date: | Sat, 30 Jul 2005 12:58:37 +0000 |
| Subject: | Re: error checking woes | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-219670@lists.php.net to get a copy of this message | ||
Okay, last attempt before I hit my head against wall. I thought perhaps to add the error check to the dropdown function itself:
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){
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-->"; } //error checking if ( !strcmp($action,'process') ) { if(empty($_POST[$row['q_name']])){ $message[$row['q_name']] == "1"; } } }
}//function GetQuestionsDropdownit didn't work either. 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){ 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 GetQuestionsDropdownNOW 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 willa) 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 saysif ($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 setwhile($row = mysql_fetch_assoc($result)) { if(empty($_POST[$row['q_name']])){ $message[$row['q_name']] == "1"; } }}//function ValidatePost Can anyone tell me what I am doing wrong? Thanks! JJ