0
votes

I am writing a form validation code in PHP. Below is the code.

the element with id #questionSubmit is a form with 6 text fields (code, question, answer, option1, option2, option3, option4, option5) and a submit button.

<form id="createQuestionForm" action="" method="POST">
    Question Code: <input id="code" class="createQuestionTextBox1" type="text" name="questionCode">
    Question Name: <input id="question" class="createQuestionTextBox1" type="text" name="questionName">
    Correct Answer: <input id="answer" class="createQuestionTextBox1" type="text" name="correctAnswer">
    Option 1: <input id="option1" class="createQuestionTextBox2" type="text" name="option_1">
    Option 2: <input id="option2" class="createQuestionTextBox2" type="text" name="option_2">
    Option 3 <input id="option3" class="createQuestionTextBox2" type="text" name="option_3">
    Option 4 <input id="option4" class="createQuestionTextBox2" type="text" name="option_4">
    Option 5 <input id="option5" class="createQuestionTextBox2" type="text" name="option_5">
    <input type="Submit" id="questionSubmit" value="Create Question"></input> 
</form>

function SubmitFormCreationData() {
    $("#questionSubmit").click(function() {
        if (CheckCodeField($("#code").val()) == false) {
            return false;
        } else if (CheckAnswerNameFields($("#question").val()) == false) {
            return false;
        } else if (CheckCorrectAnswerField($("#answer").val()) == false) {
            return false;
        } else if (CheckAnswerNameFields($("#option1").val()) == false) {
            return false;
        } else if (CheckAnswerNameFields($("#option2").val()) == false) {
            return false;
        } else if (CheckAnswerNameFields($("#option3").val()) == false) {
            return false;
        } else if (CheckAnswerNameFields($("#option4").val()) == false) {
            return false;
        } else if (CheckAnswerNameFields($("#option5").val()) == false) {
            return false;
        } else {
            $.post("InsertNewQuestion.php", $('#createQuestionForm').serialize());
            alert('Quiz Created');
            window.setTimeout(delay,2000);
            return true;
        }
        return false;
    });
}

function CheckAnswerNameFields(value) {
    var isValid = true;

    if (value == "") 
        isValid = false;
    if (value == null)
        isValid = false;
    for(LCV = 0;LCV <= (count(value)-1); LCV++) {
        if(value[LCV] == "'")
            isValid = false;
        if(value[LCV] == '"')
            isValid = false;
    }

    return isValid;
}

function CheckCodeField(value) {
    var isValid = true;

    if(isInteger(value) == false)
        isValid = false;
    if(value < 100000)
        isValid = false;
    if(value > 999999)
        isValid = false;

    return isValid;
}  

function CheckCorrectAnswerField(value) {
    var isValid = true;

    if(isInteger(value) == false)
        isValid = false;
    if(value < 1)
        isValid = false;
    if(value > 5)
        isValid = false;

    return isValid;
}

function isInteger(possibleInteger) {
    return /^[\d]+$/.text(possibleInteger);
}

Now if the first field is entered correctly then the output is as wanted, false is returned. However if the first field is entered correctly and the rest are blank then the page refreshes, however it should be returning false because I check if the question and option fields are blank. Why is this happening?

1
Client is bypassable, you must add the control on the page which called with ajax (on server-side) - Sam
why do you have three CheckCodeField functions?? Anyway that might not be the reason for the issue. And could you please post the form fields also? - Faizul Hasan
Do u mean the HTML for the form? - Veda Sadhak
yep.. it would help to find the problem.. :) - Faizul Hasan
@Sam I don't quite understand, could you please elaborate? Do you mean I should paste the php code that handles the data from the form? - Veda Sadhak

1 Answers

0
votes
    $("#questionSubmit").click(function() {
        $.post("InsertNewQuestion.php", $('#createQuestionForm').serialize(), function(data){
        if(data=='SUCCESS'){
               alert('Quiz Created');
               window.setTimeout(delay,2000);
               return true;
               }
            else{
                var alertx='';
                for(i=0;i<data.split('-').length-1;i++) alertx += data.split('-')[i].toString()+"\n";
                alert(alertx);
            }
        });
    return false;
});

insertnewquestion.php

extract($_POST);

 $errors = '';

if(!in_array($questionCode,range(100000,999999))) $errors .= 'Invalid code-';
if(!in_array($correctAnswer,range(1,5))) $errors .= "Invalid answer-";

 for($i=1;$i<=6;$i++){ 
   $var = $i==6 ? $questionName : ${'option_'.$i};
   if(empty($var) || strstr($var,'"') || strstr($var,"'")) $errors.= "Invalid ".($i==6?"question name":"option $i")."-"; 
    }

     echo $errors=='' ? 'SUCCESS':$errors;

Try and write if work correctly