Note that there are some explanatory texts on larger screens.

plurals
  1. POGet data from MySQL using AJAX
    primarykey
    data
    text
    <p>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: <code>index.php</code>, <code>global.js</code>, and <code>date.php</code></p> <p><code>index.php</code> takes an input (a date, in this case)</p> <pre><code>Date: &lt;input type="text" id="date"&gt; &lt;input type="submit" id="date-submit" value="Submit"&gt; &lt;div id="date-data"&gt;&lt;/div&gt; &lt;script src="http://code.jquery.com/jquery-1.8.3.min.js"&gt;&lt;/script&gt; &lt;script src="js/global.js"&gt;&lt;/script&gt; </code></pre> <p><code>global.js</code> listens for a click on the submit button and posts the input (date) to <code>date.php</code></p> <pre><code>$('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); }); }); </code></pre> <p><code>date.php</code> takes the date and queries the mysql database to return an array. This array needs to be passed back to <code>index.php</code> but I can't figure out how to do it.</p> <pre><code>&lt;?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); ?&gt; </code></pre> <p>Ideally, I need to be able to have the <code>$values</code> variable from <code>date.php</code> passed to a <code>$values</code> variable in <code>index.php</code></p> <p>There must be an easier way to do this...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload