weixin_33720956 2017-03-14 18:43 采纳率: 0%
浏览 38

jQuery插件:表单和验证

I have a contact webform that collects a name, email, and message then uses phpmailer to send an email. In order to do some client side validation and provide immediate feedback via ajax, I was looking into jQuery Form Plugin ($form for short) and jQuery Validation Plugin ($validate).

I am a bit confused on how exactly i use these together or if it is even necessary to use both.

$validate's documentation states that using submitHandler function will submit the form via Ajax after it is validated.

$("#myform").validate({
  submitHandler: function(form) {
  $(form).ajaxSubmit();}
});

That example was provided with a link to $form's page and uses the $form method ajaxSubmit().Am I right to assume that this example provided is using the $form plugin? As in this would be somewhere before the above js to prepare the form for Ajax submission.

$('#myForm').ajaxForm();

Or would the better approach be to validate with rules() specified in $validate, and then use $form's ajaxSubmit?

$('#myForm').submit(function() {  
$(this).ajaxSubmit(); 
return false; 
});

lastly, how much server side validation/sanitation with php is necessary afterwards? If $validate allows the form to post, wouldn't i just need to sanitize against xss with:

$name    = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email   = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);

Edit: After doing some digging i've found a thread that answers my first question and that the top code fragment will work. However i still don't know if i need to have this declared.

$('#myForm').ajaxForm();
  • 写回答

1条回答 默认 最新

  • 10.24 2017-05-08 02:32
    关注

    To wrap up this question in order to use both I've found putting

    $(form).ajaxSubmit(options);
    

    at the end of jquery validate's

    submitHandler:
    

    After any var options will do the trick.

    评论

报告相同问题?

悬赏问题

  • ¥15 vue2项目里接口请求报错,把接口放到浏览器里面正常
  • ¥15 Qt安装后运行不了,这是我电脑的问题吗
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上