使用AJAX从MySQL获取数据

时间:2022-09-25 23:34:13

I need to take an input (date) from a form in a php file, and use the date variable to select data from a mysql database and return an array in the same php file without refreshing the page. Right now, I have 3 files: index.php, global.js, and date.php

我需要从php文件中的表单获取输入(日期),并使用日期变量从mysql数据库中选择数据,并返回相同php文件中的数组,而无需刷新页面。现在,我有3个文件:索引。php,全球。js和date.php

index.php takes an input (a date, in this case)

索引。php接受输入(在本例中是日期)

Date: <input type="text" id="date">
  <input type="submit" id="date-submit" value="Submit">
  <div id="date-data"></div>
  <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="js/global.js"></script>

global.js listens for a click on the submit button and posts the input (date) to date.php

全球。js监听提交按钮上的单击并将输入(日期)发送到date.php

$('input#date-submit').on('click', function() {
    var date = $('input#date').val();
    $.post('../ajax/date.php', {date: date}, function(data) {
        $('div#date-data').html(data);
        alert(data);
    });

});

date.php takes the date and queries the mysql database to return an array. This array needs to be passed back to index.php but I can't figure out how to do it.

日期。php获取日期并查询mysql数据库以返回数组。这个数组需要传回索引。但是我不知道怎么做。

<?php 

$con = mysql_connect('localhost', 'root', 'pass');
mysql_select_db('gymflow', $con);
  $date = $_POST['date'];
  $query = mysql_query("SELECT * FROM table WHERE `date` = $date");
    $values = array();

  while ($row = mysql_fetch_array($query))
  {
    $values[] = $row['utilization'];
  }
echo json_encode($values);

?>

Ideally, I need to be able to have the $values variable from date.php passed to a $values variable in index.php

理想情况下,我需要能够拥有从日期开始的$values变量。php传递给index.php中的$values变量

There must be an easier way to do this...

一定有更简单的方法可以做到这一点……

1 个解决方案

#1


3  

The purpose of your AJAX call is to not refresh index.php, and instead deliver the result of data.php straight into the first page. You can modify the DOM via the javascript callback, and should make other database-requests in data.php. There is no purpose in letting index.php aware of the output of data.php, simply because when the Javascript call is made, your first page has already been processed and sent to the client and possibly already rendered in the browser window.

AJAX调用的目的是不刷新索引。php,而是传递数据的结果。php直接进入第一页。您可以通过javascript回调来修改DOM,并且应该在data.php中发出其他数据库请求。出租指数是没有目的的。php知道数据的输出。php,这仅仅是因为在进行Javascript调用时,您的第一个页面已经被处理并发送到客户端,并且可能已经在浏览器窗口中呈现。

If you need to know the input value in subsequent requests to index.php you may store in the value in the user's session.

如果您需要知道后续请求中的输入值以进行索引。php可以存储在用户会话的值中。

#1


3  

The purpose of your AJAX call is to not refresh index.php, and instead deliver the result of data.php straight into the first page. You can modify the DOM via the javascript callback, and should make other database-requests in data.php. There is no purpose in letting index.php aware of the output of data.php, simply because when the Javascript call is made, your first page has already been processed and sent to the client and possibly already rendered in the browser window.

AJAX调用的目的是不刷新索引。php,而是传递数据的结果。php直接进入第一页。您可以通过javascript回调来修改DOM,并且应该在data.php中发出其他数据库请求。出租指数是没有目的的。php知道数据的输出。php,这仅仅是因为在进行Javascript调用时,您的第一个页面已经被处理并发送到客户端,并且可能已经在浏览器窗口中呈现。

If you need to know the input value in subsequent requests to index.php you may store in the value in the user's session.

如果您需要知道后续请求中的输入值以进行索引。php可以存储在用户会话的值中。