Weird $_SESSION behaviour in PHP [duplicate] - php

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to make php scripts run in parallel?
I'm currently having a (in my opinion) weird problem with the $_SESSION superglobal in PHP.
On page unload ($(window).unload) jQuery sends a synchronous post request to save.php.
It contains some data that should be saved in $_SESSION["data"]. I can fetch the jqXHR object and display some test strings contained in responseText with alert() when the current page is still shown. But on the next page those data is not available, yet. I just have to reload and everything is fine.
It seems to me that my browser (Firefox/Chrome) load the next page in background while the post request is not finished yet. Or is it a problem with $_SESSION?

May be attach event to window.unload is not a good idea. Browser may fetch the next page first then call the event, In that case you seesion is not change (yet). So instead of attach to unload event, You may attach to the event that cause navigation
For example:
Attach to a link click, cancel default behavior, call your ajax save.php script then manual do navigation using window.location.href
Attach to form post, cancel the post, call ajax then post form....
Hope that help

Related

Refreshing page with submitted form alert window [duplicate]

This question already has answers here:
Stop browsers asking to resend form data on refresh
(4 answers)
Closed 7 years ago.
I am using forms to retrieve data using post method. Once I do that, and retrieve data from database, if I want to refresh the page, my browser will give me alert window telling me it will have to resend data again. How would I go about making that window not appear?
On your form, you need to change the method="POST" to method="GET".
Also, you should alter the way you get the requests to "$_GET" and not "$_POST".
You will notice that the parameters are going to appear on the URL after submiting.
Note that this isn't recomended for forms, it's often used for search boxes.
It should be form action another page and completing inserting data and than redirect to form page
or after completing data send redirect with jsvascript code
like
window.location.href = window.location.pathname;

Submit an action without refreshing or redirecting the page [duplicate]

This question already has answers here:
jQuery Ajax POST example with PHP
(17 answers)
Closed 7 years ago.
So, I have a contact form on one page (let's call it send_now.php or example.com/send)
Once the form is filled out and submitted, then an email is sent to a certain user while the page is directed to example.com/it_is_sent page which contains a sent confirmation based on confirmation.php.
I would like to know how to change it so that everything is done on example.com/send/ page without refreshing or redirecting the user to the next page.
Here is what I mean.
So, in /send/ page, an user fills out the form and click send. Then without redirect the user to /confirmation/ page, the confirmation is shown on /send/ page without redirecting the user, so everything happens within the same page.
Is there a way to do that? what is the general concept of doing things like that?
or, can the form be submitted within the same page without refreshing the page?
Thanks!
Take a quick search around the net for "jquery ajax form submit". The term I think you're looking for is Ajax. It is what allows you to have JavaScript send off data to a PHP script without refreshing the page.
You build your form like normal, and attach a jQuery click event to the form or submit button. The jQuery/Ajax function takes the data from the form and sends it over GET or POST to your PHP form.
Whatever your PHP script outputs is received by your jQuery/Ajax function. I like to use json_encode on a PHP Array for the PHP script output. In JavaScript I can then easily work with the results as an array of values.
Depending on what's in the Array or output depends on how your JavaScript should react. Output could be as simple as a 0 or 1, true or false, or a json Array or values like I usually do. I'll usually include at least error=true/false.
You could have the PHP script output be displayed in a Div once the Ajax success function fires.
You could also use jQuery load() to load another page into a Div upon success. The possibilities are endless when you combine it all.
You can easily find code samples for this all over StackOverflow and tutorials on the rest of the Internet. You're looking for "jQuery Ajax Form Submit to PHP", maybe even with MySQL?
This technique makes buttons that make instant changes possible. Once you're done with this project, look into websockets if you really want to see how instant the web can be.

Is my logic correct? Ajax and/or PHP with Mysql

I have a page which shows a list of items. Page coded with html, css, php and using mysql db.
On that page a user can request to add one of the items to their special list.
I want to do this within the page without having to do a complete page refresh. So user clicks button to add, item is added to their list and button changed so they can't add it again.
Do I use ajax calls to run code behind the page and then refresh the div?
Or is there a better more efficient way to do it.
I'd prefer a php option of possible in case user has js turned off, but don't know if it can be done with using js.
Any help appreciated.
If you want dynamic content (changing the page without refreshing) you are going to have to use Javascript. To do what you are asking, you could call a PHP script via Ajax that outputs the contents of the div with the new item, and then change the div based on that response.
Dagon is exactly right. Create a form which handles the request and set the action of the form to the PHP script you want to handle the request. Note that although this can be the same php script that you use to process your ajax request, it does not necessarily have to be.
Many times when I implement such functionality, I'll set the PHP to send variables as POST (in the event of JS disabled) and have my ajax request as a GET so I can use a single PHP page to handle the 'same' request. When using AJAX, I'll have the script echo a specific code then have the ajax response handle that return.
if(val == 'OK') {
//in event of success, perhaps you want to hide the original form and show a success message
} else {
//do something like unhide a hidden div to display an error
}
If JavaScript is turned off, the page has to be reloaded. In your case jQuery could be very handy and simply rewrite the element you need to rewrite. The server send's a simple json. Using a PHP Framework might also be a good idea, since the way you ask it seems (with respect, and not wanting to offend), that you are not using any framework and might run into falls making your script vulnerable (sql injections for example)
If your visitor doesn't have JavaScript enabled and you want to serve anyways, then you have to do a page reload. First check if that is worth to do, who is your client/visitor, what browser do they use, ... questions like that help you to design your page/app.

use jquery ajax to refresh PHP data on same page

I have a PHP page with a mysql connection, a select query and then i'm building a table using PHP. If i wanted to use jQuery AJAX to refresh the data on a setInterval, on the same page, how would i go about doing that? (by the way i can do it to another PHP page but i've never done it if the PHP stuff is on the same page)
If you want to keep making ajax calls i suggest you do long polling, which basically means you have a script that is requesting content via ajax every given time, and it will verify every time if the content has been modified, if not it will wait again and make another call.
I used jQuery Periodical updater for a chat box and it worked perfectly.
If you wnat to learn more about how jQuery and AJAX work, check out this Nettus article
You set the param in request, for instance file.php?ajax=1
The, depending on its value, you render full html or just the necessary elements for ajax
in php:
if($_GET['ajax']) renderAjax();
else renderFullHTML();
in js:
$.get('file.php?ajax=1', function(data) {
$('.result').html(data);
});

passing values from java script to php

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.

Categories