使用PHP的Javascript输出


Javascript Output using PHP

我正在尝试执行下面的代码,它完美地返回了数据库的输出。

<?php if(isset($_POST['submit']))
{
    $email = $_POST["Email_i"];
    $checkRe = mysql_query("select * from contact_form where email='$email'",$con);
   if(mysql_num_rows($checkRe)>0)
   {
      $check = 1;
   }
?>

我正在尝试使用onSubmit事件调用一个函数,如下所示;

<form action="#" method="post" name="myForm" onSubmit="return CheckForm()">
    <input type="submit" name="submit">
</form>
<script type="text/javascript">
function CheckForm()
{
    var calc =1;
    var Checkre = "<?php 
    $check="";
    echo $check; ?>";
   if(calc == Checkre)
   {
        alert('phone number exists');
        return false;
   }
}
</script>

上述函数不设置CCD_ 1的值。

不要使用HTML FORMS,而是尝试使用jQuery AJAX

示例代码(其中数据作为对象[或命名数组]传递,dataType为您的api.php响应声明,超时包括网络错误、错误+成功+完成函数):

$(document).ready(function(){
    $("#btnSubmit").click(function(){
        var x = $("#textbox").val();
        $.ajax({
            type: "POST",
            url: "url-to-php-api",
            data: { reference:x },
            dataType: "HTML-or-JSON-or-JSONP",
            timeout: 30000, //1 sec = 1000 ms
            error: function(x, t, m) {
                if (t === "timeout") {
                    alert("Network Connection Delayed.");
                    //this is for network error i.e.: connection delays
                }
                //and some other codes for other errors~
                //error function runs when there is some error with the jQuery AJAX syntax
            },
            success: function(retData) {
                alert("PHP RESPONSE: " + retData);
                //success function runs when the js successfully communicated, passed the values to PHP, gets result and back
            },
            complete: function() {
                alert("jQuery AJAX Function Complete.");
                //complete function runs after the process is completed, regardless of being error of successful
            }
        });
    });
});

当然,就我个人的编码而言,这个代码就是