Php Form validation
Posted: Tue Jun 02, 2009 1:06 am
Please help me with this problem
Here it goes :
This code needs validation if the user has filled all the fields or answers that he has to. The user side of the page is displayed by extracting the questions from the database. Depending upon type of question ( if its multiple choice , or has multiple answers,i.e checkbox or is text answer) and number of options( in case of multiple choice and multiple answers ) the questions are displayed using HTML code embedded in PHP for loops. So the code would look like for example :-
/****************************************************************************************************************************************/
if($Question_Type== "multiple_answers"){
$each_option=explode(",",$Options);
echo "
<input type=\"hidden\" name=\"options$Serial_No\" value=\"$Options\">
<input type=\"hidden\" name=\"no_of_options$Serial_No\" value=\"$No_of_Options\">
<input type=\"hidden\" name=\"type_of_question$Serial_No\" value=\"$Question_Type\">
<input type=\"hidden\" name=\"question$Serial_No\" value=\"$temp\">";
for($i=0; $i<$No_of_Options;$i++){
$j=$i+1;
echo "<input type=\"checkbox\" name=\"answers$i$Serial_No\" value=$j />
<label for=\" option$i\" class=\"text\"> $each_option[$i]</label>";
}
echo "<br><br>";
}
if($Question_Type== "Textbox"){
echo "
<input type=\"hidden\" name=\"type_of_question$Serial_No\" value=\"$Question_Type\">
<input type=\"hidden\" name=\"question$Serial_No\" value=\"$temp\">";
echo "<input type=\"text\" id=\"textbox$Serial_No\" name=\"textbox$Serial_No\" size=\"50\" value=\"\" />";
echo "<br><br>";
}
/****************************************************************************************************************************************/
Now the Problem is I wanted to use JavaScript to verify if the user filled all the questions. The defined a javascript function in html
/************************************************************************************************************************/
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>Online Questionnaire </title>
<SCRIPT LANGUAGE=\"Javascript1.2\">
function verify() {
var message = \"You are required to complete at least these following fields: \";
for( var i=1; i<= 2 ;i++){
var str1= \"answers\"+i;
var str2= \"textbox\"+i;
if (document.getElementById(str1).value==\"\") {
message = message + \" - answers\"+ \" \"+i;
alert(message);
}
if (document.getElementById(str2).value==\"\") {
message = message + \" - textbox\" +\"\"+i;
}
if (message == \"You are required to complete at least these following fields: \") {
return this.user_input.action='survey_answers.php';
document.user_input.submit();
return true;
}
else {
alert(message);
return false;
}
}
}
</SCRIPT>
</head>
/********************************************************************************************************************************************/
which is not working. So please suggest me how to do it in JavaScript. After the fields are filled the information would be enetered in database.
--
Here it goes :
This code needs validation if the user has filled all the fields or answers that he has to. The user side of the page is displayed by extracting the questions from the database. Depending upon type of question ( if its multiple choice , or has multiple answers,i.e checkbox or is text answer) and number of options( in case of multiple choice and multiple answers ) the questions are displayed using HTML code embedded in PHP for loops. So the code would look like for example :-
/****************************************************************************************************************************************/
if($Question_Type== "multiple_answers"){
$each_option=explode(",",$Options);
echo "
<input type=\"hidden\" name=\"options$Serial_No\" value=\"$Options\">
<input type=\"hidden\" name=\"no_of_options$Serial_No\" value=\"$No_of_Options\">
<input type=\"hidden\" name=\"type_of_question$Serial_No\" value=\"$Question_Type\">
<input type=\"hidden\" name=\"question$Serial_No\" value=\"$temp\">";
for($i=0; $i<$No_of_Options;$i++){
$j=$i+1;
echo "<input type=\"checkbox\" name=\"answers$i$Serial_No\" value=$j />
<label for=\" option$i\" class=\"text\"> $each_option[$i]</label>";
}
echo "<br><br>";
}
if($Question_Type== "Textbox"){
echo "
<input type=\"hidden\" name=\"type_of_question$Serial_No\" value=\"$Question_Type\">
<input type=\"hidden\" name=\"question$Serial_No\" value=\"$temp\">";
echo "<input type=\"text\" id=\"textbox$Serial_No\" name=\"textbox$Serial_No\" size=\"50\" value=\"\" />";
echo "<br><br>";
}
/****************************************************************************************************************************************/
Now the Problem is I wanted to use JavaScript to verify if the user filled all the questions. The defined a javascript function in html
/************************************************************************************************************************/
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
<title>Online Questionnaire </title>
<SCRIPT LANGUAGE=\"Javascript1.2\">
function verify() {
var message = \"You are required to complete at least these following fields: \";
for( var i=1; i<= 2 ;i++){
var str1= \"answers\"+i;
var str2= \"textbox\"+i;
if (document.getElementById(str1).value==\"\") {
message = message + \" - answers\"+ \" \"+i;
alert(message);
}
if (document.getElementById(str2).value==\"\") {
message = message + \" - textbox\" +\"\"+i;
}
if (message == \"You are required to complete at least these following fields: \") {
return this.user_input.action='survey_answers.php';
document.user_input.submit();
return true;
}
else {
alert(message);
return false;
}
}
}
</SCRIPT>
</head>
/********************************************************************************************************************************************/
which is not working. So please suggest me how to do it in JavaScript. After the fields are filled the information would be enetered in database.
--