title: OO PHP and AJAX Form Validation
body: <p>I need some help with getting JQuery AJAX to work with a method in one of my PHP classes. I validated the form using a PHP class and which extends a Database class. I just need the AJAX to work and I'm good. How do you get AJAX to work with OO PHP? I have been stuck on this for the past few days and have researched all across the internet and have not found anything that works. Can someone maybe post a simple example of getting JQuery's AJAX function to work with a PHP method?</p>
  
  <p>Here is the PHP method that returns whether the user passed validation or not (contact.class.php which extends database.class.php):</p>
  
  <pre><code>public function isValidData() {
  
  if ($this -&gt; firstName() &amp;&amp; $this -&gt; lastName() &amp;&amp; $this -&gt; email() &amp;&amp; $this -&gt; subject() &amp;&amp; $this -&gt; message()) {
  
          return true;
      } else {
          return false;
      }
  
  }
  </code></pre>
  
  <p>Here is the Jquery. The Ajax is at the bottom: </p>
  
  <pre><code>//Submit function called when the user clicks the submit button
  
  $('#contact_form').submit(function(e) {
  
      //Prevent submission until the user passes validation
      e.preventDefault();
  
      //If all the functions return true, then send form to the AJAX function
      if(validFirstName() &amp;&amp; validLastName() &amp;&amp; validEmail() &amp;&amp; validSubject() &amp;&amp; validMessage()) {
          //Serialize the data in the form for the AJAX Request
          var formData = $('#contact_form').serialize();
  
          //submitForm(formData);
          //Displays success message, clears contact form and hides the lightbox
          $('#contact_form').fadeOut(1000, function() {
              $('.success').html('Form submission successful.' + '&lt;br/&gt;' + 'Thank you ' + $('input.first').val() + "!").fadeIn(4000, function() {
                  //Clears contact form
                  $('.first').val('');
                  $('.last').val('');
                  $('.email').val('');
                  $('.subject').val('');
                  $('.message').val('');
                  //Hides success message
                  $('.success').hide();
                  //Hides lightbox
                  $('.mask, .main_contact').css('display', 'none');
              });
  
          });
          return true;
  
      } else {
          return false;
      }
  
  });
  
  //Validates the user's first name
  function validFirstName() {
      var firstName = $('.first').val();
      if(firstName.length &lt;= 2 || firstName == '') {
          $('.error').show().html('3 Characters required!&lt;br/&gt;');
          $('.first').css('box-shadow', ' 0 0 10px #B40404');
          return false;
      } else {
          $('.first').css('box-shadow', '0 0 4px #000');
          $('.error').hide();
          return true;
      }
  
  }
  
  //Validates the user's last name
  function validLastName() {
      var lastName = $('input.last').val();
      if(lastName.length &lt;= 2 || lastName == '') {
          $('.error').show().html('3 Characters required!&lt;br/&gt;');
          $('input.last').css('box-shadow', '0 0 10px #B40404');
          return false;
  
      } else {
          $('input.last').css('box-shadow', '0 0 4px #000');
          $('.error').hide();
          return true;
      }
  }
  
  //Validates the user's email
  function validEmail() {
      var email = $('.email').val();
      if(!email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/)) {
          $('.error').show().html('Invalid email address!&lt;br/&gt;');
          $('.email').css('box-shadow', ' 0 0 10px #B40404');
          return false;
  
      } else {
          $('.email').css('box-shadow', '0 0 4px #000');
          $('.error').hide();
          return true;
      }
  
  }
  
  //Validate the subject input in the contact form
  function validSubject() {
      var subject = $('.subject').val();
      if(subject.length &lt;= 2 || subject == '') {
          $('.error').show().html('3 Characters required!&lt;br/&gt;');
          $('.subject').css('box-shadow', ' 0 0 10px #B40404');
          return false;
      } else {
          $('.subject').css('box-shadow', '0 0 4px #000');
          $('.error').hide();
          return true;
      }
  }
  
  //Validate the message input
  function validMessage() {
  
      var message = $('.message').val();
      if(message.length &lt;= 2 || message == '') {
          $('.error').show().html('3 Characters required!&lt;br/&gt;');
          $('.message').css('box-shadow', ' 0 0 10px #B40404');
          return false;
      } else {
          $('.message').css('box-shadow', '0 0 4px #000');
          $('.error').hide();
          return true;
      }
  
  }
  
   });
  </code></pre>
  
  <p>//Ajax Request</p>
  
  <p>function submitForm(formData) {</p>
  
  <pre><code>$.ajax({
      type : "POST",
      url : "includes/function.php",
      data : formData,
      dataType: 'json',
      cache : false,
      success : function(formData) {
          if(formData.success) {
  
              alert(formData.msg);
          } else {
              alert("Error");
          }
          console.log(formData);
  
      }
  });
  </code></pre>
  
  <p>The Jquery seems to disable my PHP serverside validation too. When the JQuery is disabled, the server side validation works fine. Any idea why JQuery would disable the server validation? I am kinda new to programming and I would appreciate any help, thanks. </p>