Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you are looking for is known as AJAX, and there's nothing hard about it, especially with jQuery! The problem with sending the data to PHP to validate is that it would immediately nix your requirement for "speed" - the form would need to hang while Javascript makes a request to the server to see if everything is alright. For this reason, it is common practice to have validation done in <em>both</em> server and client side. Note I'm saying both, as you should <em>always</em> validate data on the server.</p> <p>With this in mind, let's get started! I am going to show how to validate a form without using the validation plugin - for bigger projects, you might want to consider it, but it is easy enough without it:</p> <pre><code>&lt;form id='myform' action='whatever.php' method='POST'&gt; First Name: &lt;input type='text' name='first_name' class='required'&gt;&lt;br&gt; Last Name: &lt;input type='text' name='last_name'&gt;&lt;br&gt; Email: &lt;input type='text' name='email' class='required'&gt;&lt;br&gt; Zip: &lt;input type='text' name='zip'&gt;&lt;br&gt; &lt;/form&gt; </code></pre> <p>So as you can see we have first name and email with a class of required, so we can do this:</p> <pre><code>$(function() { // wait for the document to be loaded $('#myform').submit(function() { var valid = true; // assume everything is okay // loop through each required field in this form $('input.required', this).each(function() { // if the field is empty, mark valid as false // and add an error class if($.trim($(this).val()) == '') { valid = false; $(this).addClass('error_input'); } }); // additional validation for email, zip, perhaps return valid; // if valid is true, continue form submission }); }); </code></pre> <p>The Validation plugin makes this all a little neater and cleaner to do, but if you just need to do a quick form there is nothing wrong with the approach above. It does suck because you have to have your validation rules in two places, but apart from executing Javascript on the server or calling the server for the data there is no alternative if you want to quickly tell your users something is wrong.</p> <p>Hope this helps.</p> <p><strong>EDIT</strong>: Seems I misunderstood your question. If you want to pass the form data to a PHP script, have it validate the values, and return success or error back to the page without a load, you would do something like this:</p> <pre><code>$('#myform').submit(function() { // allow submit if we've validated this already if($(this).data('done')) return true; var data = $(this).serialize(); // serialize the form data // you could get the two variables below from the actual form (DRY) // with $(this).attr('action'); and $(this).attr('method'); but I am // not sure if that is what you want... var action = 'validate.php'; // what script will process this var method = 'POST'; // type of request to make $.ajax({ type: method, url: action, data: data, dataType: 'json', success: function(d) { if(d.code == 'success') { // everything went alright, submit $(this).data('done', true).submit(); } else { alert('something is wrong!'); } } }); return false; // don't submit until we hear back from server }); </code></pre> <p>And then all your PHP script has to do is return something like this, using <a href="http://www.php.net" rel="nofollow noreferrer"><code>json_encode</code></a>, to indicate whether everything is alright or not:</p> <pre><code>// verify everything... // data will be in $_POST just like a normal request if($everythingOK) { $code = 'success'; } else { $code = 'error'; } print json_encode(array('code' =&gt; $code)); exit; </code></pre> <p>I haven't tested the Javascript above, but I am pretty sure it should be about right. I hope <em>this</em> helps. :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload