实现php&jquery进行表单验证

问题描述

| 我有一个实时预览表:http://davidwalsh.name/jquery-comment-preview 它使用jquery实时显示预览:
$(document).ready(function() {
  $(\'#live-preview-form input,#live-preview-form textarea\').bind(\'blur keyup\',function() {
    $(\'#lp-comment\').text($(\'#comment\').val());
    $(\'#lp-comment\').html($(\'#lp-comment\').html().replace(/\\n/g,\'<br />\'));
  });
});
如果我需要在来自textarea的\“#comment \”上应用一些PHP函数,然后将其放入jquery验证中怎么办?     

解决方法

如果要应用PHP函数,则必须使用jquery ajax(将值发布到php页面并获取返回的结果)。 例如: process.php:
<?php
function my_function($in){
    ....
    return $out;
}

$result = my_function($_POST[\'q\']);
echo json_encode($result);
?>
js:
$(document).ready(function() {
  $(\'#live-preview-form input,#live-preview-form textarea\').bind(\'blur keyup\',function() {

    $.ajax({type: \"POST\",url: \"function.php\",data:{ \"q\": $(\'#comment\').val()},dataType: \"json\",success: function(result){
        $(\'#lp-comment\').text(result);
        $(\'#lp-comment\').html($(\'#lp-comment\').html().replace(/\\n/g,\'<br />\'));
      }});
  });
});
但是,不建议这样做,因为用户每次键入单个字符时都会查询您的服务器。 最好的方法是通过javascript函数而不是PHP函数。     ,我认为您应该使用此处提供的jquery表单验证插件。它处理您需要的所有基本表单验证。     

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...