简体   繁体   English

来自jQuery Ajax的PHP表单验证

[英]PHP form validation from jQuery ajax post

I have a form (firstname, lastname etc) which is validated client side using http://bassistance.de/jquery-plugins/jquery-plugin-validation/ validator. 我有一个使用http://bassistance.de/jquery-plugins/jquery-plugin-validation/validator验证的客户端形式(名字,姓氏等)。 I am then posting the form fields to the server using ajax serialize() however I was wondering if someone could share a class (or link to class) for the server side validation. 然后,我使用ajax serialize()将表单字段发布到服务器,但是我想知道是否有人可以共享一个类(或链接到类)以进行服务器端验证。

I have tried using http://www.html-form-guide.com/php-form/php-form-validation.html but this looks like this class is expecting the name of the form field to apply validation. 我已经尝试使用http://www.html-form-guide.com/php-form/php-form-validation.html,但这似乎是此类希望表单字段的名称适用于验证。

Thanks 谢谢

That library does not require the name of the form. 该库不需要表单的名称。 If you look at the example that they have given you, they only use the names of the input fields. 如果您查看他们给您的示例,他们仅使用输入字段的名称。

<?PHP
require_once "formvalidator.php";
$show_form=true;
if(isset($_POST['Submit']))
{
    $validator = new FormValidator();
    $validator->addValidation("Name","req","Please fill in Name");
    $validator->addValidation("Email","email",
"The input for Email should be a valid email value");
    $validator->addValidation("Email","req","Please fill in Email");
    if($validator->ValidateForm())
    {
        echo "<h2>Validation Success!</h2>";
        $show_form=false;
    }
    else
    {
        echo "<B>Validation Errors:</B>";

        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
          echo "<p>$inpname : $inp_err</p>\n";
        }
    }
}

if(true == $show_form)
{
?>
<form name='test' method='POST' action='' accept-charset='UTF-8'>
Name: <input type='text' name='Name' size='20'>
Email: <input type='text' name='Email' size='20'>
<input type='submit' name='Submit' value='Submit'>
</form>
<?PHP
}//true == $show_form
?>

The name of the form is "test". 表单的名称是“ test”。 It is not being checked. 没有检查。 Please post your source code so that someone can help you find what the issue is. 请发布您的源代码,以便有人可以帮助您找到问题所在。

You can also checkout the following post for more libraries - Easiest Form validation library for PHP? 您还可以查看以下博文以获得更多库- 最简单的PHP表单验证库?

...ok, if you are looking for sanitizing the passed value to make sure no bad things get through than you can look here: http://php.net/manual/en/filter.filters.sanitize.php ...好吧,如果您要对传递的值进行清理以确保没有坏事通过,那么可以在这里查看: http : //php.net/manual/en/filter.filters.sanitize.php

if you are looking for 'validation' based on some pre-set rules, please specify. 如果您要根据某些预设规则寻找“验证”,请指定。

You can also partially sterilize the passed values like this if you do need only basic char. 如果您只需要基本字符,则还可以对传递的值进行部分灭菌。 set: 组:

function sterilize($input) {

$input = htmlentities($input);

$input = strip_tags($input);

    return $input;

}

FYI as you mentioned below, for the email address you could youse something like this: 如下所述,仅供参考,您可以使用以下电子邮件地址:

$email = isset($_POST['email']) ? filter_var($_POST['email'], FILTER_SANITIZE_EMAIL): '';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM