【问题标题】:Using extra php files to get the ajax post request and then load the php script使用额外的 php 文件获取 ajax post 请求,然后加载 php 脚本
【发布时间】:2017-08-24 01:27:19
【问题描述】:

我有一个对我来说非常复杂的任务,也向你解释一下.. 我会总结一下我应该做什么.. 有 3 个文件是 1.html 执行 ajax 发布到 passwrapper.php 的文件 2. passwrapper.php 将接收 ajax post 请求并包含另一个文件 3.student.php,其中包含有关如何执行与数据库的连接并将所有数据转换为 json 的代码,然后显示所有数据.. 我被要求执行 ajax 发布多个项目。换句话说,我被要求从数据库的最后一行接收姓名和宗教。下面是我的代码...

html文件

<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",
        dataType: "json",
        data: {
            lastName: true,
            lastReligion: true,
        },      
        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';
if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"]))){
    executePass();
} 
else 
{
    //how to get name and religion from the last row and then perform executepass() to show all data and also data from the last row
}
?>

学生.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);
}

我的问题是如何从 passwrapper.php 中的 student_name 和 student_religion 的最后一行获取最后一行数据,并执行 executepass() 以显示所有数据...请不要修改 sql 代码。我也不想从成功中获取数据。例如:data[count(data)-1]['student_name']... 我的问题是如何获取 passwrapper.php 中的 student_name 和 student_religion 的最后一个值...并执行 executepass() 函数以显示所有数据..请帮助我...您可以将最后一个数据写入文件或在 html 控制台中显示..

【问题讨论】:

  • 您的 SQL 中没有 ORDER BY,因此行的顺序是不可预测的。最后一行是什么意思?
  • 什么是abdpass.php?应该是student.php
  • 我知道没有 order by 但我想默认显示从 1 到结尾的所有数据。我还执行 ajax 发布请求以从数据库的最后一行获取数据,即 student_name 和 student_religion。例如,有 10 行数据。我想从最后一行获取值..
  • @Barmar 我已经纠正了我的错误..
  • 为什么不获取 Javascript 中的最后一行? data[data.length-1].student_name

标签: javascript php jquery ajax


【解决方案1】:

最好将executePass() 更改为以return $json_array; 结尾,而不是回显它。那么passwrapper.php 可以这样做:

$students = executePass();
if ((!isset($_POST["lastName"])) and (!isset($_POST["lastReligion"]))){
    echo json_encode($students);
} 
else {
    file_put_contents("all_students.json", json_encode($students);
    $last_student = end($students);
    echo json_encode(array($last_student));
}

以及其他想要返回所有学生的脚本可以这样做:

$students = executePass();
echo json_encode($students);

如果您不能这样做,请使用输出缓冲函数来捕获来自executePass() 的输出,而不是将其发送到客户端。

else {
    ob_start();
    executePass();
    $json = ob_end_clean();
    file_put_contents("all_students.json", $json);
    $students = json_decode($json, true);
    $last_student = end($users);
    echo json_encode(array($last_student));
} 

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 2011-11-06
  • 2011-12-20
  • 1970-01-01
  • 1970-01-01
  • 2012-05-28
相关资源
最近更新 更多