如何解决实现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表单验证插件。它处理您需要的所有基本表单验证。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。