princess
princess

Reputation:

I cannot get client side and server side validation to work

I have the following forms

<form name="courses">
  <div="requiredfield">
    <input type="text" name="coursename" id="coursename">
  </div>
  <input type="submit" name="submitform1">
</form>
<form name="students">
  <div="requiredfield">
    <input type="text" name="studentfirstname" id="studentfirstname">
  </div>
  <div="requiredfield">
    <input type="text" name="studentlastname" id="studentlastname">
  </div>
  <div="requiredfield">
    <input type="text" name="studentage" id="studentage">
  </div>
  <input type="submit" name="submitform2">
</form>

I use this code for client side validation

// this works fine
$('#courses').submit(e) {
  e.preventDefault();
  if ($('#coursename').val() == 0) {
    // error message is appended to the div containing the field
    // the same for the other form and its field
  }
}

What does not work is the server side validation

<?php
    if (isset($_POST['submitform1'])) {
        if (empty($_POST['coursename'])) {
            $course_mistake = "please fill this field"
        }
    }
?>

I have no idea why the server side validation does not work. Please help me.

Upvotes: 0

Views: 62

Answers (2)

Justinas
Justinas

Reputation: 43499

1 You prevent form submitting:

$('#courses').submit(e) {
    e.preventDefault(); // This prevents form submitting to PHP side

If you don't use Ajax (I don't see any code related), than you are note sending data to PHP

2 <form name="courses"> means you are using $_GET method, but in PHP you expect $_POST.
Either add method="post" or in PHP use $_GET/$_REQUEST.

3 You are not outputting errors nowhere - missing $course_mistake processing

Upvotes: 0

Bhavin Shah
Bhavin Shah

Reputation: 2482

<form name = "courses" method="post" action="validation.php">
 // Your form fields.
</form>

You should specify the method and the name of php file in <form> , in which your controls will go on submit .

See this http://www.w3schools.com/php/php_forms.asp

Upvotes: 1

Related Questions