I need to run a php script (site crawler) on submit button click without a page reload. After the background script run, A confirmation / alert popup should appear that the site crawler is ended. Any Ideas Please?
Basically the idea goes like this:
you send and XHR call to server and execute script
script checks if there is already a background process
if there is one, return the status code
if on background script running: script forks itself , returns an "OK" response and start to do the work
with a different XHR call you repeatedly check if script has finished
when the server-side script has finished running, you gather the results and diplay in the page
To simplify the interaction with server, you might want to use some of the XHR libraries. I would avoid suggest to avoid slapping on jquery unless you are actually already using it for something else.
As for "how to know when background script has finished" : i think the best way would be for that script to create a file, which contains
when running: -1
when finished: 0
when error: positive number
Jquery + AJAX could make your job easier. Read this tutorial on how to achieve the result:
AJAX+ jQuery Tutorial
Related
What would be the best approach to give the user feedback, that a PHP script is running while they wait?
I've got a PHP script that runs via an AJAX call when a user clicks a button.
The PHP script deals with large heavy images and takes some time before it is completed.
Because it is activated via an AJAX call, the user get's no visual feedback that the PHP script is running, because the user does not leave the page.
How do I go about giving some kind of visual feedback that something is happening via PHP in the background?
A simple .gif that is visible during the loading time of the script would suffice.
The easiest way to do it is to show the image as soon as the click takes place. You could do this by having an image already in place styled with display: none and then use jQuery's show() function to show it.
When you get your success message within your AJAX call, hide the image again.
If you have incremental activity that you want to show textually you can also direct the action at an iframe and have php echo and flush.
echo "Beginning Step 1... <br/>";
flush();
// do step 1
echo "Step 1 Complete!<br/>";
echo "Beginning Step 2...<br/>";
flush();
// etc..
i have a requirement in which user need to upload a file and click on submit after uploading the page. after clicking on submit a php script runs in the background. what i want is that the page should be redirected to another PHP page while script.php script should execute in the background.
<form action=script.php method='POST'>
what i want is that the script.php script should still run and the page should be redirected to some result.html page.. Any idea?
as fare as I understood your question you need a message queue tool.
save the requests in a sql table or as jobs on a file system and
run a script from the console.
otherwise you block some web server instances.
something like rabbitmq
all the best
Can you use Jquery file uploading method? So while ajax uploads the file, you can show something else in background div.
You can do a header("Location: foo.php"); after the script that you want to run. If you script doesn't output any headers, this is fine and it will redirect when the script completes.
If the script is very time-consuming, you will need to do an asynchronous call, probably via ajax, to the PHP script as you submit the form, and then the form target can be where you'd like the redirection to be.
header('location:'sumthng.php');
giv this in ur action page
can any one please help how to get the values from the javascript to php other than while using submit button.
scenario:
i am searching for record, if the record found then, i need confrim alert asking to continue or not, if he click continue how can i say he selected continue
If you want to check without having a page reload, you probably want to execute an AJAX call, then depending on the result returned by the underlying PHP script, take the appropriate action. If you have no knowldege of how to implement this, take a look here
You can never use JavaScript to communicate with the page while it is loading, you can only send a new request to the web server from the JavaScript layer... although you can send that request to the same script that's already running, it will still be a new instance of the PHP script, just like when you open a new browser tab to the same page.
The only way for JavaScript to communicate with PHP at all, is by sending an HTTP request. But you don't have to refresh the page in order to do that if you use AJAX.
AJAX is basically a word to describe JavaScript exchanging information with web pages without refreshing the page. But note that it will still not be able to change variables in the PHP script which is running when the JavaScript code is executed.
In the case of PHP, I've used the open-source library SAJAX which is quite simple. You will find it at http://www.modernmethod.com/sajax/
Hope it helps and good luck!
You can use this as an example using jquery and PHP:
$.post('searchimage.php', { action: 'searchimage', imgreference: $(this).val() },
function(data) {imgsample.html(data);}
);
Basically apply the above function in a document ready function so its run when the page loads.
This can be triggered using $("#prodcode").click() or what ever event handler you want to use.
The php page in my example will get sent the value from imgreference as a post, you can do whatever you want in the php page then return the value which gets added to the imgsample (in this case a td)
hope this helps.
I have a run.php that has a button on it called "Run". Clicking it does a jQuery $.get() AJAX call back to a response.php on the server, and sends the result to a Javascript callback function getResponse(sData) inside run.php's HTML for display back on the browser. I want to have things such that as response.php runs through a list of tasks, it echoes a response back with the echo command and for getResponse to update a DIV with that status as it moves along. So, let's say I have 5 steps inside response.php, and therefore 5 echo statements back to getResponse().
I tried to get this to work, but what happens is that jQuery waits and then sends one single response all at once, rather than sending as it goes along with the 5 responses.
What's the technique?
The reason I ask is that I have a script that does something to a bunch of files. The first thing it does is a file count, so it updates my progress bar. Then, as it runs through files, it needs to increment my progress bar like every 1000 files.
I think there's no way to make that ajax call to have multiple response in just one call... but what I could suggest is you make a session on php... and in every steps on your tasks function, update that session... then make another ajax call that checks that session if any updates happened... if there is update then do what you have to do....
As you can't really get progress with xmlhttprequest, I suggest you can look into other ways of doing AJAX calls. One of them is through iframe. You can create hidden iframe, set it's sources to request.php and then periodically just check it's content. It should be possible since it's all it the same domain and restrictions does not apply.
iframe might work because it's not that different from normal browser window, meaning that it periodically applies data it gets into DOM even if request hasn't been finished yet. There's potentially might be problems with how different browsers do that, i.e. IE shows new content only if it got more than 4K or something. But it is possible to overcome that, I'm sure.
So, create new hidden iframe, add src attibute to your php script, make that script periodically write something to the client and on the client check what have been written and convert it to shiny GUI stuff.
How would I go about running a php script when I pressed say an "update" button, in which then it would run say script x1.php (which has no echo's or or other output, successes or failed) then update the current page (I know the update part can be done with ajax, but im not sure how and how I would get the x1.php script to run in the background and trigger the ajax update when done).
Any pointers in the right direction would be great :)
You can try like this:
<input type="button" onclick="go();" value= "Update" />
function go()
{
$.ajax(
{
type: "POST",
url: "script x1.php",
data: data, // data to send to above script page if any
cache: false,
success: function(response)
{
// update code for your page
}
});
}
This will run your script in the background and then you can also update the contents of the page. You might want to modify the code as per your needs.
which has no echo's or or other
output, successes or failed
There should be some response sent back for the above script to be able to know that it has finished running otherwise i am afraid you can't find out when and how script ended.
Note: Using JQuery here.
Ajax is the 100% correct answer here. You generate a HTML page that makes an Ajax request to x1.php. Depending on what that returns, the Ajax success method updates the page.
To be exact, you make the request using the Ajax method in JavaScript, and update the page using JavaScript.
Here's some examples and documentation:
Using JQuery: jQuery.ajax()
Using Prototype: Introduction to Ajax
AJAX is the "right" direction. You should call the x1.php, and use it's output to update the current page. Take care about your site should work without javascript.
What you're actually asking for is exactly what AJAX is all about. You would just attach an event observer to that button and run your PHP script, returning whatever you need. The exact syntax is just dependent on whether you are using a JavaScript lib (jQuery/Prototype etc)
Have a look at an question I asked. Return value to browser but still process in PHP.
Use ajax to make the request, but get PHP to return a value even though it is still processing in the background.
You can employ a variety of mechanisms here. Judging by your question, i take it the "Update" process is expected to take a bit longer than you would expect from an usual ajax request?
Option 1
Make the update script write the status to a database, then poll for update status via AJAX
Option 2
Make the update script create a lockfile while it's running, and delete the lockfile once it has finished, use AJAX to poll for exitance of the lockfile
Option 3
If the update script is expected to finish updating quickly, simply echo an 'success' or 'error' status message and handle it on successful AJAX request as Sarfraz suggested.
Just include a .php that has as SQL-query something like this:
mysql_query("SELECT * FROM table WHERE date < DATE_SUB(NOW(), INTERVAL 8 DAY)");
It will only be executed if that statement is true. :)
In that .php, make sure not to put any echo's. It will work 'automatically' and the user won't notice a thing.