2011-04-13 82 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。它處理您需要的所有基本表單驗證。