在ajax帖子中单独显示数据和发布的值

时间:2017-08-25 01:20:31

标签: javascript php jquery ajax

我有一个关于ajax发布数据的任务,并且还要求脚本显示数据...我有3个文件..HTML文件执行ajax到php脚本,passwrapper.php然后包含另一个脚本到在html文件中的console.log上以json格式显示所有数据。我实现了一些ajax post值,例如'Abdullahlahlahlah'和'Muslim',以便passwrapper.php将接收这些数据并在控制台separatley上回显整个数据...开发人员说ajax不能回响2次......难道完全不可能????如果没办法,请告诉我其他选择...... 要求:请不要修改student.php,因为我希望那个php脚本能够回显json中的所有数据。 修改paswrapper.php和html文件..

当我运行html文件时,错误状态

Status Code: 200

ErrorThrown: SyntaxError: Unexpected token < in JSON at position 1634

jqXHR.responseText:

[{"student_id":"1","student_name":"Ashfur","student_gender":"F","student_age":"19","student_religion":"Muslim","student_course_id":"1"},{"student_id":"2","student_name":"Irfan","student_gender":"M","student_age":"17","student_religion":"Islam","student_course_id":"4"},{"student_id":"3","student_name":"Alice","student_gender":"F","student_age":"21","student_religion":"Chinese","student_course_id":"2"},{"student_id":"4","student_name":"Mohit","student_gender":"M","student_age":"20","student_religion":"Christian","student_course_id":"6"},{"student_id":"5","student_name":"Susy","student_gender":"F","student_age":"27","student_religion":"Chirstian","student_course_id":"5"},{"student_id":"6","student_name":"Ida","student_gender":"F","student_age":"23","student_religion":"Islam","student_course_id":"3"},{"student_id":"7","student_name":"Abdul","student_gender":"M","student_age":"22","student_religion":"Islam","student_course_id":"1"},{"student_id":"8","student_name":"Ernest","student_gender":"M","student_age":"25","student_religion":"Chinese","student_course_id":"4"},{"student_id":"9","student_name":"Wei Ling","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"10","student_name":"Ashtae","student_gender":"M","student_age":"23","student_religion":"Islam","student_course_id":"4"},{"student_id":"11","student_name":"Jasmine","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"65656","student_name":"yyyyty","student_gender":"F","student_age":"65","student_religion":"anything","student_course_id":"009090"}]

Also there is the posted values on the console.log which states LastRowname = Abdullahlahlahlah LastRowReligion = Muslim

HTML文件

<html>
<head>
<script type="text/javascript" src="/Cesium-1.34/ThirdParty/jquery-1.11.3.min.js"></script> 
</head>
<div id="results"</div>
<div id="resulte"</div>
<script type="text/javascript">
showData();
function showData()
{
    $.ajax({
        type: "post",
        url: "passwrapper.php",
        dataType: "json",
        data: {
            lastName: 'Abdullahlahlahlah',
            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("LastRowname = '.$_POST["lastName"].' LastRowReligion = '.$_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);
}
?>

在我的网络标签

[{"student_id":"1","student_name":"Ashfur","student_gender":"F","student_age":"19","student_religion":"Muslim","student_course_id":"1"},{"student_id":"2","student_name":"Irfan","student_gender":"M","student_age":"17","student_religion":"Islam","student_course_id":"4"},{"student_id":"3","student_name":"Alice","student_gender":"F","student_age":"21","student_religion":"Chinese","student_course_id":"2"},{"student_id":"4","student_name":"Mohit","student_gender":"M","student_age":"20","student_religion":"Christian","student_course_id":"6"},{"student_id":"5","student_name":"Susy","student_gender":"F","student_age":"27","student_religion":"Chirstian","student_course_id":"5"},{"student_id":"6","student_name":"Ida","student_gender":"F","student_age":"23","student_religion":"Islam","student_course_id":"3"},{"student_id":"7","student_name":"Abdul","student_gender":"M","student_age":"22","student_religion":"Islam","student_course_id":"1"},{"student_id":"8","student_name":"Ernest","student_gender":"M","student_age":"25","student_religion":"Chinese","student_course_id":"4"},{"student_id":"9","student_name":"Wei Ling","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"10","student_name":"Ashtae","student_gender":"M","student_age":"23","student_religion":"Islam","student_course_id":"4"},{"student_id":"11","student_name":"Jasmine","student_gender":"F","student_age":"23","student_religion":"Chinese","student_course_id":"2"},{"student_id":"65656","student_name":"yyyyty","student_gender":"F","student_age":"65","student_religion":"anything","student_course_id":"009090"}]<script>console.log("LastRowname = Abdullahlahlahlah LastRowReligion = Muslim");</script>

我的要求: 我想回显html文件中的所有数据(除了发布的值)。 我想在console.log上显示发布的值。它可以在passwrapper.php或html文件中.....请帮助我....

2 个答案:

答案 0 :(得分:0)

ajax不能回声2次; 1.student.php

<?php

include 'student.php';

$data['student_arr']=executePass();
$data['post_str']=receivePost();
echo json_encode($data);
function receivePost()
{
    if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"])))
    {
        //do nothing
    }
    else
    {
        $post_str= 'LastRowname = '.$_POST["lastName"].' LastRowReligion = '.$_POST["lastReligion"].'';
        return $post_str;
    }
}
?>

2.passwrapper.php

success: function(data){
   console.log(data.post_str);
   $('#results').text(JSON.stringify(data.student_arr));
},

3.html文件

select * from cabs where po ="$po" and test  >  "$t" ORDER BY title limit $cnt,1

答案 1 :(得分:0)

passwapper.php

<?php
include 'student.php';

ob_start();
executePass();
$string = ob_get_contents();
ob_end_clean();

$data['student_arr']=json_decode($string);
$data['post_str']=receivePost();
echo json_encode($data);
function receivePost()
{
    if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"])))
    {
        //do nothing
    }
    else
    {
        $post_str= 'LastRowname = '.$_POST["lastName"].' LastRowReligion = '.$_POST["lastReligion"].'';
        return $post_str;
    }
}
?>