Prevent submitting form before php validation succeed

Discussion in 'PHP' started by Kinaski, Jun 16, 2009.

  1. #1
    Hi all,

    I would appreciate some help considering my php form validation script.

    The code goes like this:
    
    if (isset($_POST['Submit']))
    {
        $validator = new FormValidator();
        $validator->addValidation("first_name","req","Please fill in Name");
        $validator->addValidation("email","email","The input for Email should be a valid email value");
    	
        if($validator->ValidateForm())
        {
            echo "<h2>Validation Success!</h2>";
            $show_form=false;
        }
        else
        {
            echo "<B>Please fix folowing errors in the form:</B>";
    
            $error_hash = $validator->GetErrors();
            foreach($error_hash as $inpname => $inp_err)
            {
                echo "<p class=\"form_phperror\">$inpname : $inp_err</p>\n";
            }       
        }
    }
    
    <form name="test-form" id="test-form" action="http://other-server.com/submited-form.php" method="POST">
     <div>
            <label for="first_name">Contact Person - First Name <span class="req">*</span></label>
            <input  id="first_name"  class="textfield required" maxlength="40" name="first_name" size="20" type="text" title="Please enter your first name." />
          </div>
          <div>
            <label for="email">Email <span class="req">*</span></label>
            <input  id="email" class="textfield required email" maxlength="80" name="email" size="20" type="text" title="Please enter your valid email address." />
          </div>
    </form>
    
    
    PHP:
    By the way I'm using http://www.html-form-guide.com/php-form/php-form-validation.html for the validation part.

    What I'm not sure how to achieve is to stop the form from submitting before validation process finishes and is successful . Now it immediately goes to the form action page http://other-server.com/submited-form.php not validating the script.

    I want this to be a completely php server validation if possible, and not client javascript one.

    Thank you!
     
    Kinaski, Jun 16, 2009 IP
  2. Sudoku-Master

    Sudoku-Master Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    lol... you have to send the form to the validationscript back... validation is only possible after submitting, and so you have to submit to the form... after validation you can give out a redirect to the other page over php..
     
    Sudoku-Master, Jun 16, 2009 IP