473,387 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[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

Expand|Select|Wrap|Line Numbers
  1. setInterval(function() {
  2.     $.get("file.php", function (data) {
  3.        console.log('Good to go, data was loaded');
  4.        console.log(data);
  5.     });      
  6. }, 5000);
  7.  
Now, i have a tons of question that make me confused

well, i have a form that contain 4 field
Expand|Select|Wrap|Line Numbers
  1. <form name="catcher_form" action="display.html" onsubmit="testscript();" method="POST">
  2.     <fieldset class="fieldset" align="center">
  3.         <legend>API Endpoint Catcher</legend>
  4.             <br>
  5.             <table>
  6.             <tr>
  7.             <td>URI</td><td>: <input type="text" id="input_uri" name="input_uri"></td>
  8.             </tr>
  9.             <tr>
  10.             <td>Filename</td><td>: <input type="text" id="input_file_name" name="input_file_name"></td>
  11.             </tr>
  12.             <tr>
  13.             <td>Finish</td> <td>: <input type="date" id="input_finish" name="input_finish" /></td>                                            
  14.             </tr>
  15.             <tr>
  16.             <td>Interval</td> <td>: <input type="text" id="input_interval" name="input_interval"></td>
  17.             </tr>
  18.             </table>
  19.  
  20.             <br />
  21.             <div>
  22.             <input type="submit" value="Process" name="button_process" class="fbtab">
  23.             <input type="submit" value="terminate" name="button_terminate" class="fbtab">
  24.             </div>
  25.  
  26.     </fieldset>
  27.     </form>
and here is php file
Expand|Select|Wrap|Line Numbers
  1. //Time interval
  2. $interval= $_POST['input_interval'];
  3.  
  4. while (true){
  5.  
  6.     $now=time();
  7.     //include("the_script.php");
  8.  
  9.     //Date Format
  10.     $date=date("M-d-Y, \a\\t g.i a", time());
  11.     //URI API Endpoint
  12.     //$uri="https://data.cityofchicago.org/resource/t2qc-9pjd.json";
  13.     $uri= $_POST['input_uri'];    
  14.     //Named File
  15.     $file_name= $_POST['input_file_name'];
  16.     $finish= $_POST['input_finish'];
  17.     //Get File JSON
  18.     $json=file_get_contents($uri);
  19.     $filejson = fopen("dataset/json/dataset-".$file_name."-".$date.".json", "w") or die("Unable to open file!");
  20.     fwrite($filejson, $json);
  21.     fclose($filejson);
  22.  
  23.  
  24.     $filecsv = fopen("dataset/csv/dataset-".$file_name."-".$date.".csv", "w") or die("Unable to open file!");
  25.     $array = json_decode($json, true);
  26.     $firstLineKeys = false;
  27.     foreach ($array as $line)
  28.     {
  29.         if (empty($firstLineKeys))
  30.         {
  31.             $firstLineKeys = array_keys($line);
  32.             fputcsv($filecsv, $firstLineKeys);
  33.             $firstLineKeys = array_flip($firstLineKeys);
  34.         }
  35.  
  36.         fputcsv($filecsv, array_merge($firstLineKeys, $line));
  37.  
  38.     }
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
Jan 27 '15 #1
1 2420
Dormilich
8,658 Expert Mod 8TB
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
Jan 28 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: JMB | last post by:
Hello, I was wondering if anyone knew of any projects extending the inline upload progress bar to utilize an inpage image uploader with bar, without having to refresh or go to a seperate page,...
9
by: CeyloR | last post by:
Hello everyone, I have a problem with my Ajax page. I'm jusing prototype-1.4.0.js as framework. I have a function to populate three listboxes. The items in the listboxes are from a MYSQL...
4
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have seen several articles about this subject but I was wondering with ajax is this easer. The articles mentioned client callback and the onbeforeunload event of the browser. Can AJAX be used for...
5
by: Jo | last post by:
hi there: I ve never used ajax before but as a test i wanted to use it for a web presentation i have to do, can you recomend some tutorial or sample code? Is it simple for someone who never...
2
by: willl69 | last post by:
Hi All, Ive been having a problem of late with one of my sites that uses PHP5 / Ajax. The problem is that periodically the ajax functions lock up and it gets stuck in the loading phase of the...
3
by: willl69 | last post by:
Hi All, Ive been having a problem of late with one of my sites that uses PHP5 / Ajax. The problem is that periodically the ajax functions lock up and it gets stuck in the loading phase of the...
5
by: ravisuguna | last post by:
Hi, I am new to ajax.I have a file in php which is a report. I want a certain portion of the report to be refreshed every 3 seconds.How can we refresh a part of page using ajax.I also want to...
9
by: Caper | last post by:
Hello everyone, I can’t decide which Ajax framework(s) I should take in my project since I am a newbie in the Ajax world. The main task of this project relates to online examining in all middle...
1
by: shawnwperkins | last post by:
Hi Guys, I'm new to Javascript and have a couple of questions that I hope someone could quickly answer for me. First, I'm trying to modify an example Ajax script to fit my needs. The script...
2
by: Shao | last post by:
Dear All, I am looking for a nitty-gritty Python Ajax script to fire off a number of processing programmes, periodically checking their operations, sending messages back to an HTML div form by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.