I have a page that allows for used input, when the user inputs his/her specifications and selects submit the algorithm will run. This can take a few minutes depending on input. The user will also be directed to a page which will show their results. I want to show a block of php code that allows for the user to input their email and an email containing the url for results will be sent to them automatically when the results are ready. However, if the results are ready I want the block of code (acting like loading page) to 'disappear' and the results visualisation code to run.i.e. show the results. is this possible? I'm fairly new to programming, so not sure the way to go about this. Thanks for the help.
The thing about PHP script is that, it will keep running until -
1) The server kills the script.
2) Script kills itself.
So whatever you are doing, it will keep running in the PHP script untill it is finished and t the end you could place the algorithm to mail the user. To have the results placed back when they are ready you could use Ajax. You could see the following tutorial for Ajax with jQuery: http://www.devirtuoso.com/2009/07/beginners-guide-to-using-ajax-with-jquery/
Related
I'm trying to force to reload a (second) page if a criteria is met in php
But if the criteria is met, i want the page to force reload everywhere, even if 10 people have it open at once for example.
for simplicty lets say the code is like this:
in /filelocation/script.php:
if {$data == "ok"}{
reload/refresh "reload.php" if it's open somewhere;
}
I came across a software that basicly does this, and i want to understand how this is done.
(it works cross device somehow, so i asume its done through php somehow)
Well, in your PHP code, you cannot simply reload/refresh something for all the users connected. This is simply because the PHP code is only executed when your browser requests a page on the server so it's only executed to build the HTML response and then it stops executing. Once the browser has the HTML response it will render the page and then it waits for an action from the user to do something else (such as clicking on a link or posting a form).
I imagine that you would like that when a specific user does something, like posting a comment or buying a product, you would like all the other visitors to be notified that a new comment has been posted or that the number of products available has been reduced.
To do that, you need to implement some JavaScript which is executed in the browser of each visitor. The idea is to keep a connection with the server with the help of web sockets. This way, you can inform the browser that something has changed.
You could google to find some examples of PHP apps using web sockets. The first example I found:
https://www.twilio.com/blog/create-php-websocket-server-build-real-time-even-driven-application
Another solution could be to have some JavaScript doing some pooling, meaning that every N seconds, it executes an Ajax request to the server to ask if something has changed. This can be done with the help of setTimeout(yourFunction, 10000) to call a JavaScript function every 10 seconds. This function will do the Ajax request and then update the part of your page that needs to change. Just be carefull that if you get a lot of users on your site then you'll produce quite a lot of load on your server. So this wouldn't be a good solution, but it could be an alternative to the web sockets.
So I have a PHP script process.php that basically pings either a single host by name or IP and or a range of IPs. The IP(s) are passed via a form.php and I'm happy to report that it works well.
What I'm trying to achieve is the following:
1. Output to the form each IP as it is pinged, currently the script completes before it outputs. This isn't an issue for the single IP or hostname but for the IP range it leaves the user hanging.
My question is can I do this with JQuery/Ajax keeping the logic within PHP and process.php or do I need to move the logic to form.php within JQuery/Ajax?
I know I haven't included the actual code but I just need a check on the logic.
for ($ip=$formipstart;$ip<=formipend;$ip++) {
echo "$ip";
}
You can leave it as is, but I would move your logic into web page, specially if you are good with jQuery and AJAX. For me it is always nicer to present visitor that something happens in background, to show an overlay and spinner somewhere, no matter if that takes 2 seconds or 2 minutes. It will always look better and it may save your hosting resources. For example, you can limit ping-check to 1 per second, or you can stop pinging any time, which is not possible with PHP.
So my question was kind of general on where I should put the logic to achieve a ping response for each request as it happens. My first attempt was a simple form that posted to a process.php page. All the logic was contained within the process.php. The ping tool worked great but for one thing. I couldn't get ping to output as they happened. It would only output the results after the entire logic/page had been processed. The output was correct but it did leave the user hanging.
After creating this post my second attempt moved the logic to the form.php page and was rewritten in a mix of jquery and php. The form.php called the process.php which had been reduced to just the actual ping and result. With jquery and ajax I was able to show each ping response as it happened.
Annoyingly after read many more posts it would seem that if the logic was kept in process.php as pure php i would have to wait until the page has finished processing. I'm yet to see evidence showing me otherwise. It's also a pain that so much logic/code is now visible to the end user.
I searched a lot of places, but couldn't find solution.
What I want to do is:
submit a form on PHP server to another server, the request is to search some results back;
The search will take several minutes, so the other server will first return a progress html page, the will page will call back to ping the other server when the final result will be returned;
Get the final result page
The function is easy if form is submitted from browser. After viewing progress page, the final search result page will be returned and shown in browser.
But I don't want to show those on my client. I want to process that on my server, process data and show something else on my client.
Thanks a lot!!
You'll need some way of tracking whether a form has been processed or not - I recommend using a database entry. So here are the basics of your algorithm:
When the user submits the form to your client, you add a new record to the DB.
The required data from this form is sent to the other server.
Any client who accesses your page will be shown the 'Waiting' layout (because the complete DB column is set to '0')
When the processing is complete, the other server hits a listener script on your server, providing it the form ID. You then update the complete column in the DB to '1'
When the user visits the page now, they see its been completed.
I'm not going to write the code for you, but this functional overview should set you on your way.
Here is the scenario:
I have a page that is logging data to MYSQL. I have another page that reads that data and allows it to be viewed. When a new piece of data is logged I would like to have the first script check and see if the viewing page is open in the browser, and if so append the newest data to the end of the view. Also - could anyone point to some info giving an overview of how PHP and the browser interact? I think I have the concept of the DOM down for javascript...but as far as PHP it just appears that once the page is sent, that's it...
You're correct in that once the PHP is sent, that's it.
There is no way to send data to a PHP page once the page is loaded. There is another slightly nastier method, but the easiest way of doing this is going to be polling the page via Ajax.
So, have a script that every 20 seconds, sends a message to another PHP script that contains the timestamp of the last MySQL log you received, then get the script to return all the data that has been set by that time.
I'm unsure how new you are to JavaScript, but the easiest way of doing that is probably using JQuery's $.ajax and encoding the new MySQL records as JSON.
No this isn't possible as you describe. The viewing page will have to poll the server for changes, either by periodically reloading itself, or by javascript / AJAX.
You are right that once the page is sent by PHP it can have no further influence. In fact the PHP execution thread on the server is killed as soon as output is complete, so the thing that generated the page no longer even exists.
To expand on Dolondro's suggestion, rather than periodically polling the server for updates, you could use Server-Sent-Events (newly supported in modern browsers).
With these, you basically just send 1 ajax request to the server, and the connection is held open. Then, the server can send updates whenever it wants. When the browser receives an event, it can add the data to the screen. Even still, the connection is held open, and the server can send additional events/updates as they occur.
W3C page:
http://dev.w3.org/html5/eventsource/
Wikipedia:
http://en.wikipedia.org/wiki/Server-sent_events
More Info:
https://www.google.com/search?ix=hcb&sourceid=chrome&ie=UTF-8&q=server+sent+events
I have a page where users enter their email address, click "Send", and the webserver will email them a ~10mb attachment. Right now, I have the page just display "Sending..." and the user waits about 20 seconds on this page.
Instead, I want to have it say "Your attachment will be emailed in a few minutes," sending that process somewhere else and allowing the user to continue browsing without having to open up a new tab.
I really just need someone to point me in the right direction because I don't even know what to Google at this point.
You could call another php file that will process the email sending and make sure to put in this call:
ignore_user_abort(true);
What this does is allows the php process to finish, even though the browser leaves. So you could initiate the process via ajax and then go to another page saying your attachment has been sent.
For more info:
http://www.php.net/manual/en/function.ignore-user-abort.php
I recommend checking out this question I posted a while ago.
How can I run a PHP script in the background after a form is submitted?
The answer explains how you can run a PHP script in the background after a form is submitted. This, of course, is just one way to accomplish this. Another would be to build a list of addresses and set up a cron to run a script on a timed interval. Both can accomplish the same thing, it just depends on how you wish to tackle the issue and what you can do on your server.