Note that there are some explanatory texts on larger screens.

plurals
  1. POValidate fields of a form one by one
    primarykey
    data
    text
    <p>I have a form with 7 fields.When I submit it to "register.php" page it will process each field one by one and outputs the result. But this will output the results as whole. But I need to validate the data using ajax and collect the output one by one and display it to user as I leave the field.</p> <p>After this validations, also I need to send all fields in a single click; By using submit button.</p> <p>html file</p> <pre><code>&lt;form action="register.php" method="post" class="ajax"&gt; &lt;ul&gt; &lt;li&gt;Username*:&lt;br&gt; &lt;input type="text" name="username" required&gt;&lt;/li&gt; &lt;li&gt;Password*:&lt;br&gt; &lt;input type="password" name="password" required&gt;&lt;/li&gt; &lt;li&gt;Confirm the Password*:&lt;br&gt; &lt;input type="password" name="password_again" required&gt;&lt;/li&gt; &lt;li&gt;First Name*:&lt;br&gt; &lt;input type="text" name="first_name" required&gt;&lt;/li&gt; &lt;li&gt;Last Name:&lt;br&gt; &lt;input type="text" name="last_name"&gt;&lt;/li&gt; &lt;li&gt;Email*:&lt;br&gt; &lt;input type="text" name="email" required&gt;&lt;/li&gt; &lt;li&gt;&lt;input type="submit" value="Register"&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/form&gt; </code></pre> <p>register.js file</p> <pre><code>$('form.ajax').on('submit',function(){ var that = $(this), url = that.attr('action'), type = that.attr('method'), data = {}; that.find('[name]').each(function(index,value){ var that =$(this), name =that.attr('name'), value = that.val(); data[name] = value; }); $.ajax({ url: url, type: type, data: data, success: function(response) { document.getElementById("demo").innerHTML= response; } }); return false; </code></pre> <p>});</p> <p>My php files:</p> <pre><code> if (empty($errors)=== TRUE) { if (user_exists($_POST['username'])=== TRUE) { $errors[] = 'Sorry, the Username \''. $_POST['username'] . '\' is already in use'; } if (preg_match("/\\s/", $_POST['username'])== TRUE) { $errors[] = 'Username must not contain any spaces'; } if (strlen($_POST['password']) &lt; 6) { $errors[] = 'Your password must be at least 6 characters'; } if ($_POST['password'] !== $_POST['password_again']) { $errors[] = 'Passwords do not match'; } if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)=== FALSE) { $errors[] = 'Invalid email address'; } if (email_exists($_POST['email']) === TRUE) { $errors[] = 'Sorry, the email address \''. $_POST['email'] . '\' is already in use'; } } } if (isset($_GET['success']) &amp;&amp; empty($_GET['success'])) { echo "You've been registered successfully!"; } else{ if (empty($_POST)=== FALSE &amp;&amp; empty($errors) === TRUE) { $register_data = array( 'username' =&gt; $_POST['username'], 'password' =&gt; $_POST['password'], 'first_name'=&gt; $_POST['first_name'], 'last_name' =&gt; $_POST['last_name'], 'email' =&gt; $_POST['email'], ); register_user($register_data); header('Location: register.php?success'); exit(); }else if(empty($errors)=== FALSE){ </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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