I am developing a UI framework in PHP. The idea is very similar to .NET platform. Different php class are developed to render DOJO controls. In short the framework generates Javascript code to render DOJO UI controls on client side. The process is as follows:
First Request:
URL GET request come to server.
Server executes the relevant scripts which load the FORM.
Call render function of each child control (this generates the
javascript code)
serialize the whole FORM object and save it to a SESSION file.
Output the JS Code to client.
Subsequent Requests:
FORM postback request comes to server.
Server loads previous state from SESSION file (using unserialize).
Update the state object with user submitted values.
Call event handlers.
Save the current state in SESSION file.
Render response to client.
The above process works perfect for a single request at a time from same session. I need to handle multiple requests from the same session at a time.
Issue:
Let say there are two requests from the same session R1 and R2
respectively. R1 takes 10 secs to complete while R2 takes 3 secs. While
R1 was at 5 secs R2 comes and update the state file when R1 is at 8
Sec. After that R1 updates the state file again after 2 secs.
The problem is that the changes made by request R2 to state file is lost.
How the above scenario could be handled in PHP? Is there any design pattern I can follow for state management in UI framework?
Generate a unique key each time the form is requested (GET) and put it into the form as a hidden input field. This is a common practice to mitigate CSRF attacks, so you can also use it to see which form post relates to each GET request. Maybe it'll be the key to your forms array stored in your session.
Side note: it seems excessive to store some serialized form data with the session on every GET request. In most cases your form would be identical across all users. And on the POST you would presumably know what the form should "look" like anyway (although I imagine you're doing this to generalize the form handler).
Related
My Yii2 based application has a process where I need to fetch data from an external SOAP API and display the results after processing. I wish to display some static data and a placeholder for results until the background process of API data fetch and processing is completed.
Is there a way to display intermediate data in Yii2 views without using Ajax? Ajax is not a suitable option in this case due to amount of data which needs to be exchanged with API, and processed before rendering the results. Plus, results format is variable, and hence different templates may need to be applied before rendering them.
EDIT: to give a clearer picture, the flow is as below:
User Submits data (UD)
Data is processed onto the server resulting in information (SI) and data (SD)
SD is submitting to external SOAP API which returns information (AI)
User shall be given the two pieces of information SI and AI, however user shouldn't be able to see the data SD at any point
Server side processing takes about 1 second, however API data interchange takes about 15 seconds
So, SI is available at t=1 sec, but AI is only available after t=16 secs. The purpose here is to not make the user wait for AI availability, and give them time to consume SI, while AI is provided when it becomes available.
So the question is a little complicated, let me explain. My page code is running like this:
User enters query in the search field and clicks submit.
1.1 jQuery loads a new body to display progress data.
1.2 jQuery calls process.php via AJAX and supplies query as the argument.
1.3 jQuery starts setInterval periodic update to grab progress data, stored inside $_SESSION['prog'], and displays it.
When process.php finishes, jQuery stops periodic update, displays final information and calls AJAX to clear the $_SESSION['prog'] variable.
At the moment progress data is stored inside one variable, which is fine as far as different users are concerned (because of the different sessions), but if the same user were to make multiple requests at the same time, the $_SESSION['prog'] variable would be cross-overwritten.
So far I have thought of two possiblities to distinguish data for each request from the same user (same session)
Have jQuery generate some random string and send it together with query (and hope to avoid colission, although that would be unlikely)
Make 2 AJAX calls, first one requesting new_request_id, the second one sending query and new_request_id as parameters.
Have AJAX return something from PHP before is finishes(completes).
I need to connect each browser window (each request) with each running process, so I cannot send back new request ID after the request has been submitted, because I wont know which data to pick up with jQuery in the browser window. Btw, I will change $_SESSION['prog'] to $_SESSION[request_id] -> request_id is what I'm looking for.
It (request_id) could be last_insert_id(), because im creating new DB entries for each valid query, but I don't know how to get it back to each different user window.
I need advice here. Only just begun to code in PHP, AJAX and jQuery, don't really know much about sessions. How should I solve this problem?
Sorry for the lack of code, I will paste is at request.
You could add a unique ID to each request in addition to the session ID. eg. uniqid() in javascript/jquery?
You need to differentiate them somehow. For example use a unique ID autonumber field. MySQL has last_insert_id() which is very useful and handles concurrent requests correctly.
Avoid using Session variables in Ajax requests. Send them with GET (or POST) instead. Even if calling Session_start(); in the Ajax request and getting $_SESSION['prog'] from there, results can be unexpected.
I m trying to make messenger kind of service. In which I am facing a problem. I am supposed to reload the page manually. But the problem in running it automatically is that it gets reloaded for every second. I had used a javascript for reloading the page for every 1 sec. Inside which i m calling a php script. But the problem with that code is that complete page gets refreshed. Is there a way to reload only a particular part of the whole page and also i should be able run the php script in the background.
Use Ajax. Basic w3schools tutorial
Ajax is a group of interrelated web development techniques used on the client-side to create asynchronous web applications. With Ajax, web applications can send data to, and retrieve data from, a server asynchronously (in the background) without interfering with the display and behavior of the existing page. Data can be retrieved using the XMLHttpRequest object. Despite the name, the use of XML is not required (JSON is often used instead), and the requests do not need to be asynchronous.
[Wikipedia]
I've been designing a site that is used to collect data, but the person I'm designing for wants some form of redundancy just in case the window is closed or the system shuts down. Is there any way to take data that's been collected and write it to a MYSQL database if the user is disconnected for a certain amount of time, or if they shut the browser window/shut the system down without submitting the data?
The web is stateless and disconnected - so all data will (or rather: should be) persisted between page requests.
I assume you have a web-page generated by PHP that contains a lengthy data-entry form, and you want to save the data in that form in the event the user closes their browser window - the solution is to use a client-script that polls the server with the current data in the form, or at the very least hooks on to the window close event.
Actual implementation is a task left up to the OP.
This can't be done just with a pure HTML page - if the user doesn't submit the form, your server doesn't know what they've typed.
However, you could put some Javascript on the page that made an AJAX call every few seconds (or every few key-stokes or clicks). The idea would be for the JavaScript to invisibly submit the whole form to a PHP page which saved it into a sort of "holding area".
If the user then submitted the form, the holding area could be cleared out, but if they never did, then the data in the holding area would show you where they got to.
The most common techniques to partially prevent this szenario is that web apps work with a heartbeat-function which fires via javascript in a constant interval and sends a request to the server, p.e. to show that the user is still logged on - or, in your case, maybe to submit data already typed into form fields, too.
Think of it as an ajax-powered auto-save-function!
You have to add some javascript to your code for this, but the commonly used javascript libraries, like jquery or mootools, are well documented and offer alot of examples how to do something like this.
I am using php to fill up a form. Now, it so happens that form is using ajax for many of its fields.
e.g.
select [country]
(ajax will show drop-down filled with states for that country)
select [states]
(ajax will show drop-down filled with cities)
select [city]
(ajax will enable a submit button)
If it is a simple html based form, it can be easily filled with cURL. But what if the form is using ajax to populate the drop-down fields.
Thanks
When you are "populating" the form with curl, you are actually POSTing the data that would have been typed/entered into the form.
There is no need for the Ajax requests to be made, as long as you know what data you have to use.
So, the solution would be to :
Begin by getting the lists of data (by using the form in a browser, for instance)
Know how those data have to be used (by observing the "normal" way the form works)
POSTing the correct values, with your curl request.
In the end, your curl request should be the same than the one made by the browser when the form is submitted -- independantly of the Ajax requests that are sent before (those are only useful for getting data)
Well, that is unless the Ajax requests are actually "writting" something on the server -- but that's pretty rare for this kind of situation.
If it is a simple html based form, it can be easily filled with cURL
I'm not clear what you mean here. As far as I know, cURL is a tool for making HTTP requests. It can't "fill forms" (unlike, for example, WWW::Mechanize). Am I wrong about this?
I think you mean: "If it is a simple HTML based form, I can easily construct an HTTP request using cURL that submits the same query string or POST data as using a browser would." I'm going to proceed on that assumption.
The use of Ajax (in of itself) doesn't stop you constructing a the form data manually and submitting it as normal. It just makes it a little more difficult to work out what data you need to submit.
The remote system might be implemented in such a way that it falls over if you don't request all the bits of data in the right sequence (e.g. it will barf if you submit the complete data at the end without requesting the list of cities for a country). Emphasis on 'might', this wouldn't be a sane way to implement the system.
You might also want to make multiple requests with cURL anyway so that you can fetch the list of cities (and any ids that might be associated with them) and access them programatically.