I was on effort to code ajax to run a php script with given interval time, let's say every 5 minutes..
i have tried many tutorial but i'm still not satisfy on it, many tutorial set interval time default such as
Expand|Select|Wrap|Line Numbers
- setInterval(function() {
- $.get("file.php", function (data) {
- console.log('Good to go, data was loaded');
- console.log(data);
- });
- }, 5000);
well, i have a form that contain 4 field
Expand|Select|Wrap|Line Numbers
- <form name="catcher_form" action="display.html" onsubmit="testscript();" method="POST">
- <fieldset class="fieldset" align="center">
- <legend>API Endpoint Catcher</legend>
- <br>
- <table>
- <tr>
- <td>URI</td><td>: <input type="text" id="input_uri" name="input_uri"></td>
- </tr>
- <tr>
- <td>Filename</td><td>: <input type="text" id="input_file_name" name="input_file_name"></td>
- </tr>
- <tr>
- <td>Finish</td> <td>: <input type="date" id="input_finish" name="input_finish" /></td>
- </tr>
- <tr>
- <td>Interval</td> <td>: <input type="text" id="input_interval" name="input_interval"></td>
- </tr>
- </table>
- <br />
- <div>
- <input type="submit" value="Process" name="button_process" class="fbtab">
- <input type="submit" value="terminate" name="button_terminate" class="fbtab">
- </div>
- </fieldset>
- </form>
Expand|Select|Wrap|Line Numbers
- //Time interval
- $interval= $_POST['input_interval'];
- while (true){
- $now=time();
- //include("the_script.php");
- //Date Format
- $date=date("M-d-Y, \a\\t g.i a", time());
- //URI API Endpoint
- //$uri="https://data.cityofchicago.org/resource/t2qc-9pjd.json";
- $uri= $_POST['input_uri'];
- //Named File
- $file_name= $_POST['input_file_name'];
- $finish= $_POST['input_finish'];
- //Get File JSON
- $json=file_get_contents($uri);
- $filejson = fopen("dataset/json/dataset-".$file_name."-".$date.".json", "w") or die("Unable to open file!");
- fwrite($filejson, $json);
- fclose($filejson);
- $filecsv = fopen("dataset/csv/dataset-".$file_name."-".$date.".csv", "w") or die("Unable to open file!");
- $array = json_decode($json, true);
- $firstLineKeys = false;
- foreach ($array as $line)
- {
- if (empty($firstLineKeys))
- {
- $firstLineKeys = array_keys($line);
- fputcsv($filecsv, $firstLineKeys);
- $firstLineKeys = array_flip($firstLineKeys);
- }
- fputcsv($filecsv, array_merge($firstLineKeys, $line));
- }
1. is there anyway to pass variable given in the form to php script using ajax or jquery and call php periodically
2. how to keep variable in 2nd running, 3th , 4th and so on
3. how do i terminate, let's say i have a button to call terminate function that stop running php file
i would love to say thank you very much to those of you could help me to solve this issue and i need guidance
best regards