[php][ajax][javascript]run periodically php with ajax

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hendriktpl07
    New Member
    • Jan 2015
    • 1

    #1

    [php][ajax][javascript]run periodically php with ajax

    hi there..
    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

    Code:
    setInterval(function() {
        $.get("file.php", function (data) {
           console.log('Good to go, data was loaded');
           console.log(data);
        });      
    }, 5000);
    Now, i have a tons of question that make me confused

    well, i have a form that contain 4 field
    Code:
    <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>
    and here is php file
    Code:
    //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));
    
        }
    Question
    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
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    1a) AJAX does not care about forms. how you get the data into the process is your business (although jQuery makes it quite easy to transform a form into AJAX compatible data, just check out their documentation). eventually, what matters for PHP (in terms of variables) is the request itself, nothing you do on the client side.

    1b) calling PHP periodically means periodically sending (AJAX) requests.

    2) see 1)

    3) you don’t. if you have a PHP file running indefinitely (as you do in your code) the AJAX request will time out somewhen and the PHP executable will terminate the script once the max runtime is exceeded (or the process eats up all memory, etc.). in any case, you don’t get a response. TL;DR you cannot hook into an already running PHP script.

    your misconception here is that the AJAX calls communicate to an already running script. that’s plain wrong, period.

    the workflow is as follows:
    - you send a HTTP request (via AJAX) to the server
    - server takes the request and forwards it to the PHP process (while the client waits)
    - PHP executes the desired script, returning the results back to the server (while the client waits)
    - server sends the data from the PHP as HTTP response back to the request’s originator (and usually closes the connection)
    - client receives the HTTP response and does something with it

    Comment

    Working...