dqwmhrxt68679 2019-04-06 14:28
浏览 75

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

I have two php files, file A and file B that get data from the same database. File A displays the data in a paragraph on the site while File B is used by AJAX to display data in a graph created with graph.js. Both files do their job, except.

In both files I have a variable called $employeeNumber which is the name of an input field in a form used by the user to input whichever employee number they would like to view the data of. File A allows me to use $employeeNumber = $_POST["employeeNumber"]; to get the input and displays data accurately but File B won't allow me to use $_POST[], $_REQUEST[], or $_GET[]. Instead it forces me to hardcode the unique number. This is not desired.

File B is meant to get the input entered into the form, and update the graph with the data relating to the employee number entered.

I used echo $row["empnumber"]; for File A to display the data and for File B I used print json_encode($data); Could this be where I went wrong?

File B (Doesn't update graph unless I hardcode 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);
?>

File A (Works fine)



<?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();
?>

Here's the AJAX code making the request

$.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, {

Here is the form code.

<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">
  • 写回答

1条回答 默认 最新

  • doulu5109 2019-04-06 15:34
    关注

    The problem is that when you call via AJAX, you are not passing any data along to the back end.

    Further, when you use method=GET, the $_POST array will be empty.

    Change your method to POST, and include the data like this:

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器