簡體   English   中英

echo不適用於不刷新頁面的ajax調用

[英]echo not working for an ajax call which doesn't refresh the page

我正在嘗試實現一個簡單的javascript程序,其中我正在對php腳本進行ajax調用。 我確保頁面沒有自己刷新。所以,現在,如果我在PHP中使用echo函數,那么它無法正常工作。

    $("#check").on('click',function(){
        //alert("hello");
        var user_name=document.getElementById("user_name").value;
        var pwd=document.getElementById("pwd").value;

        $.ajax({
            url:'checkUser.php',
            type:'POST',
            data:{
                user_name:user_name,
                pwd:pwd
            },
            success:function(){
                alert("hello"); //This section works fine so the php file is getting called
            }
        });

return false;

})

checkUser.php

<?php require 'database.php';
if($conn->connect_error){
    die("Connection failed: " . $conn->connect_error);
}
//Database is getting connected properly
$user_name=$_POST['user_name'];
$password=$_POST['pwd'];
echo("<script>console.log('PHP:');</script>"); //not getting displayed on console
$sql="Select * from login where User_name='$user_name' and Password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
echo $count; //not getting displayed on console
 ?>

如果我想使用echo函數,那我該怎么做呢?謝謝!

php文件中的echo命令基本上將值返回給jQuery的ajax函數。 但是你沒有正確處理響應。

如何正確使用?

將參數添加到$ .ajax函數的success事件中。 此參數保存php文件的返回值。

    $.ajax({
                url:'checkUser.php',
                type:'POST',
                data:{
                    user_name:user_name,
                    pwd:pwd
                },
                success:function(data){
console.log(data);
                }
            });

在你的PHP文件中:

echo "PHP:';

所以你想使用echo,那沒關系。 但你需要理解的是,它只是將文本輸出到一個只會被ajax讀取的請求,在你的ajax.success()函數中你需要將它讀取的字符串附加到用戶看到的DOM:

$.ajax({
        url:'checkUser.php',
        type:'POST',
        data:{
            user_name:user_name,
            pwd:pwd
        },
        success:function(data){
            console.log('data');
            alert("hello"); //This section works fine so the php file is getting called
        }
    });

還要記住,你是背靠背回應兩件事所以ajax會看到這樣的事情:

<script>console.log('PHP:');</script>15

其中15是你的$count

由您決定如何將其拆分為正確的日志記錄,大多數人都使用json字符串。

您沒有通過success()函數傳遞結果。 您必須顯示頁面的響應結果。 你可以這樣做:

    $("#check").on('click',function(){
        //alert("hello");
        var user_name=document.getElementById("user_name").value;
        var pwd=document.getElementById("pwd").value;

        $.ajax({
            url:'checkUser.php',
            type:'POST',
            data:{
                user_name:user_name,
                pwd:pwd
            },
            success:function(data){
                alert(data); //data will contain the echo value
            }
        });

return false;

})

“data”變量將始終包含頁面上打印的響應,包括錯誤,因此請確保將其用於考慮是否將其用作驗證。

您將在“成功”回調中收到服務器響應

success:function(data){
            alert(data); 
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM