Php Form validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saipavani123
Forum Newbie
Posts: 1
Joined: Mon Jun 01, 2009 1:01 am

Php Form validation

Post by saipavani123 »

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.
--
User avatar
deejay
Forum Contributor
Posts: 201
Joined: Wed Jan 22, 2003 3:33 am
Location: Cornwall

Re: Php Form validation

Post by deejay »

shouldn't the subject of this actually be 'Javascript Form Validation' .

sorry I'm not that hot on Javascript, would have been more help if it was php form validation.
Post Reply