The proper way to validate a form with php (or possibly something else; aka AJAX)
How is this done. Now, let me g开发者_JAVA百科o into some more detail. I want to make a professional or at least professional seeming validation method for all my forms, from now onwards with my career. I hear a lot about using AJAX now to properly validate some fields before the user has even left the page, this interests me.
Here is my example form;
<form action="<?=$frontend['url']?>php/php-login-submission.php" method="post" id="loginForm">
<table width="300px" border="0px" cellpadding="5">
<tr>
<td><label><b>Email:</b></label></td>
<td align="right"><input name="email" type="text" id="email" placeholder="registered@email.co.uk" tabindex="1" title="email" size="30" /></td>
</tr>
<tr>
<td><label><b>Password:</b></label></td>
<td align="right"><input name="password" type="password" id="password" placeholder="********" tabindex="2" title="password" size="30" /></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" id="loginBtn" value="Login" /></td>
</tr>
</table>
</form>
Using AJAX, and php, how can i properly validate this form. I dont mind sending the data to an intermediate page such as php/php-login-submission.php
to then have it redirect them to the pages/members-area.php
if they pass validation.
So far i've been using such methods as;
if (!isset($_POST['email'])) $_SESSION['errortext'] = 'You must provide an email.';
And then just printing the $_SESSION['errortext']
on the original page. This feels a bit scrappy.
I am completely new to AJAX and I'm not hugely confident with Javascript.
By the way, if this is a lil' on the daft side for a question, then just let me know and I have no issue just deleting it. Thanks in advance,
Alex.
Well I use two methods first on the client side I use jQuery and jQuery validate plugin so that I'm not putting more work on my server then when I submit the form I check everything again to make sure nothing got through the cracks. But that's just how I do it might not be the "best" way
精彩评论