2011-04-13 37 views
0

我有一个实时预览形式:http://davidwalsh.name/jquery-comment-preview 其中用了jQuery实时显示预览:执行PHP和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 />')); 
    }); 
}); 

如果我需要办理的“#COMMENT一些PHP函数“它来自textarea,然后把它放入jQuery验证?

+0

将表单提交上的jQuery验证码 – diEcho 2011-04-13 06:12:35

回答

0

如果你想应用PHP函数,你必须使用jQuery ajax(POST值到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函数。

0

我认为你应该使用jQuery的表单验证插件here。它处理您需要的所有基本表单验证。