gpt4 book ai didi

php - 如何将表单输入设置为 Json 打印 php 文件中的变量?

转载 作者:行者123 更新时间:2023-11-29 16:08:00 25 4
gpt4 key购买 nike

我有两个 php 文件,文件 A 和文件 B,它们从同一数据库获取数据。文件 A 在网站的段落中显示数据,而 AJAX 使用文件 B 在使用 graph.js 创建的图表中显示数据。这两个文件都完成了它们的工作,除了。

在这两个文件中,我都有一个名为 $employeeNumber 的变量,它是用户用来输入他们想要查看其数据的员工编号的表单中的输入字段的名称。文件 A 允许我使用$employeeNumber = $_POST["employeeNumber"]; 获取输入并准确显示数据,但文件 B 不允许我使用 $_POST[]$_REQUEST[]$_GET[]。相反,它迫使我对唯一的数字进行硬编码。这是不希望的。

文件 B 旨在获取表单中输入的内容,并使用与输入的员工编号相关的数据更新图表。

我使用 echo $row["empnumber"]; 对于文件 A 来显示数据,对于文件 B 我使用 print json_encode($data); 这可能是我哪里出错了?

文件 B(除非我硬编码 employeeNum,否则不会更新图表)

<?php

header('Content-Type: application/json');

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";




//BELOW IS WHERE THE ISSUE LIES.
//WHEN I HARDCODE THE UNIQUE NUMBER LIKE THIS, THE GRAPH UPDATES ACCORDINGLY. WITH ACCURATE DATA FROM THE EMPLOYEE THAT THE UNIQUE NUMBER BELONGS TO.

$employeeNum = 8020;


//THIS (BELOW) I ANTICIPATED WOULD GET THE EMPLOYEE NUMBER FROM THE USERS INPUT IN THE FORM WHICH I HAVE NOW INCLUDED BUT IT PRODUCES AN ERROR.

$employeeNum = $_POST["employeeNum"]; //this is line 9

//HERE IS THE ERROR WHEN I OPEN THE Graphdata.php FILE

<br />
<b>Notice</b>: Undefined index: employeeNum in <b>C:\wamp64\www\Graphdata.php</b> on line <b>9</b><br />
<br />
<b>Warning</b>: Invalid argument supplied for foreach() in <b>C:\wamp64\www\Graphdata.php</b> on line <b>22</b><br />
<br />
<b>Fatal error</b>: Uncaught Error: Call to a member function close() on bool in C:\wamp64\www\Graphdata.php:26
Stack trace:
#0 {main}
thrown in <b>C:\wamp64\www\Graphdata.php</b> on line <b>26</b><br />




//Connect to database
$dbconnect = new mysqli($servername, $username, $password, $dbname);

//If failed to connect
if(mysqli_connect_error()){
die("Database connection error".mysqli_connect_error());
}
//Create query
$sql = "SELECT Mathematics, English, Afrikaans, Geography, Physics, History, Life_Orientation FROM studentresults WHERE employeeNumber = $employeeNum";

//Run query
$result = $dbconnect->query($sql);

//Produce result
$data = array();
foreach ($result as $row){ //THIS IS LINE 22
$data[] = $row;
}

$result -> close();

$dbconnect -> close();

//Print the result
print json_encode($data);
?>

文件A(工作正常)



<?php

//connecting to database
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";
$employeeNum = $_POST["employeeNum"];

$connect = new mysqli($servername, $username, $password, $dbname);

//checking connection

if (mysqli_connect_error()) {
die ("Database connection failed: ". mysqli_connect_error());
}

// Query the database
$sql = "SELECT Salary FROM myEmployees WHERE employeeNumber = $employeeNum";

$result = $connect->query($sql);

//print out result
while($row = mysqli_fetch_array($result)) {
echo $row["English"];
}

$result -> close();
?>

这是发出请求的 AJAX 代码

$.ajax({

//Graphdata.php is the name of FILE B

url: "http://localhost/Graphdata.php",
type: "GET",
success : function(data){
console.log(data);

var Salary = [];
var timeWorked = [];
var Age = [];
var userid = [];

for(var i in data){

userid.push("UserID " + data[i].userid);
Salary.push(data[i].Salary);
Age.push(data[i].Age);
timeWorked.push(data[i].timeWorked);


}
//Create the graph
var ctx = document.getElementById('myChart').getContext('2d');

var chart = new Chart(ctx, {

这是表单代码。

<form action="Graphdata.php" method="post">
<p class="name">Username</p>
<input class="inp1" type="text" name="name">
<p class="password">Password</p>
<input class="inp2" type="password" name="email">
<p class="employeeid">Employee ID</p>
<input id = "employeeNum" class="inp3" type="int" name="employeeNum">

最佳答案

问题是,当您通过 AJAX 调用时,您不会将任何数据传递到后端。

此外,当您使用 method=GET 时,$_POST 数组将为空。

将方法更改为 POST,并包含如下数据:

$.ajax({
method: "POST",
url: "http://localhost/Graphdata.php",
data: {employeeNum: someValue}
...
})

关于php - 如何将表单输入设置为 Json 打印 php 文件中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55550063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com