implementing php & jquery for form validation
I have a live preview form: http://davidwalsh.name/jquery-comment-preview Which uses jquery to display the preview in real time:
$(document).ready(function() {
$('#live-preview-form input, #live-preview-form textarea').bind('blur keyup',function() {
$('#lp-comment').text($('#comment').val());
$('#lp-comment').h开发者_运维百科tml($('#lp-comment').html().replace(/\n/g,'<br />'));
});
});
what if i need to apply some PHP function on "#comment" which comes from textarea, before putting it in the jquery validate?
If you want to apply PHP functions, you must use jquery ajax(POST the value to a php page and get the returned result).
For example:
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 />'));
}});
});
});
However, this is not recommended because users query your server every time when they type a single character. The best way is to by a javascript function instead of a PHP function.
I think you should use the jquery form validation plugin available here. It deals with all the basic form validation you need .
精彩评论