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
index.php takes an input (a date, in this case)
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
$('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
$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
There must be an easier way to do this...
mysql_*functions in your code. These functions are no longer maintained and will be deprecated for good in PHP 5.5. Instead, you should use either MySQLi or PDO. Don't know which to use? This article should help.data.phpinindex.phpis by the AJAX callback. That will update the page. What are you asking for?$query = mysql_query("SELECT * FROM past_traffic WHERE 'date' = $date");because when i hard code the date, i get an array with data. The 2nd problem is I don't know how to get this array into a variable in the callback...right now I just have it printed inside a div.