ajax post值为空

时间:2017-08-24 05:35:21

标签: php jquery json ajax

我想从passjrapper.php中获取ajax帖子中的数据,这些数据是学生姓名和学生宗教信仰(abdullah和muslim)值,并将这些值发布在console.log上。但是,我无法在console.log上找到发布的值。我想在console.log上显示这些发布的值。

以下是我的代码.... 我的代码是关于执行passwrapper.php的ajax帖子 然后passwrapper.php包含另一个脚本student.php来显示html文件中的所有数据。

HTML FIle

<html>
<head>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<div id="resulte"</div>
<script type="text/javascript">
showData();
function showData()
{
    $.ajax({
        type: "post",
        url: "passwrapper.php",
        contentType: "application/json",
        dataType: "json",
        data: {
            lastName: 'Abdullah',
            lastReligion: 'Muslim',
        },      
        success: function(data){
            console.log(data);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            alert('An error occurred... Look at the console (F12 or Ctrl+Shift+I, Console tab) for more information!');
            $('#resulte').html('<p>Status Code: '+jqXHR.status+'</p><p>ErrorThrown: ' + errorThrown + '</p><p>jqXHR.responseText:</p><div>'+jqXHR.responseText + '</div>');
            console.log('jqXHR:');
            console.log(jqXHR);
            console.log('textStatus:');
            console.log(textStatus);
            console.log('errorThrown:');
            console.log(errorThrown);
        },

    });
};
</script>
</body>
</html>

Passwrapper.php

    <?php
include 'student.php';
executePass();

receivePost();
function receivePost()
{
    if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"])))
    {
        //do nothing
    } 
    else 
    {
        echo '<script>console.log("Firstname='.$_POST["lastName"].' lastReligion='.$_POST["lastReligion"].'");</script>';

    }
}

?>

student.php

<?php
function executePass()
{

    $conn = mysqli_connect('localhost','root','netwitness') or die ("Could not connect database");
    $db = mysqli_select_db($conn,'abdpractice') or die ('Could not select database');

    $result = mysqli_query($conn,"select * from student");
    $json_array = array();
    while ($row = mysqli_fetch_assoc($result))
    {
        $json_array[] = $row;
    }

    echo json_encode($json_array);
}
?>

我的问题是如何在控制台上发布这些价值的阿卜杜拉和穆斯林。

1 个答案:

答案 0 :(得分:0)

试试这个,

<?php
include 'student.php';
executePass();

if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"])))
{
    //do nothing
} 
else 
{
    //post the values(Abdullah and Muslim) on the console.log on the passwrapper.php
    //i do not want to disrupt the executepass()

echo '<script>console.log("Firstname='.$_POST["lastName"].' lastReligion='.$_POST["lastReligion"].'");</script>';

}
?>