Using PHP and Javascript to Reload Data [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Loading Content using Ajax with PHP Include
So, I have a bit of a problem. I have a page, and in the page is a div, and in the div is an include for php. It includes a page that actually populates the div with content. Now, my issue is I want to make it so the div will refresh with more content loaded up from a MySQL server. My problem is going from the anchor link in the page, and using a (what, probably ajax I think?) call to a php function to load more content from the database. Thanks to any help anyone can give!

render a javascript snippet which will do a ajax post on click of that anchor. in that php, replace the div's innerHTML with the content you loaded from Mysql.
`jQuery(div_id).html(content)`
will replace the div's html with content.

Related

Anyone know how I would send form data without page refresh? [duplicate]

This question already has answers here:
How to send form data without page refresh?
(6 answers)
Closed 4 years ago.
I have a form sending chat messages to database and a php file getting them on a page as styled elements I use ajax to get messages from my php file and display them every 2 seconds.
The only problem is that when the form is submitted, it refreshes the page.
Which is quite annoying.
Anyone know how I could get data to the database without the page refreshing?
Submit your form with a button that is not an input of type submit and call a javascript function in the onclick event that makes an ajax call. That will prevent the page from refreshing.

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 it possible to call a PHP function from js file? [duplicate]

This question already has answers here:
Call php function from JavaScript
(5 answers)
Closed 5 years ago.
Is there any way I can call a PHP function from jQuery? I have a .js file and a .php file.
I have a jQuery event applied on a button click. Now I want to set a condition on what happens next when the button is clicked depending on whether a user is logged in or not. Is there any way I can do this from my .js file ?
You can call any php file by the common HTTP methods, e.g. GET and POST.
What you might be looking for is AJAX. Google the term, and also look into jQuery for a simplified approach.
You can send it to a php file with ajax and use your php file to check if the user's logged in. Then you can send a response back to your js file to see which event you should trigger
Technically, yes. But why would you want to in this case? My suggestion is to look into storing values in cookies, since both PHP and JS will have access to them.

Reload previous link on click-event of back button of browser? [duplicate]

This question already has answers here:
How to detect the back button
(2 answers)
Closed 9 years ago.
i am developing one website in which main content is getting load by jquery ajax according to selected menu item.
on selecting any menu item, url get changed according to this pattern:
http://host/domain/index.php?pageid=page
here page refers to the page that i want to load into main content using ajax.
now in this case, i want to reload the previous page if user clicks on back button of browser.
can anyone help me out how could i achieve this?
If you do not change a url, then I'm afraid that is not possible.
Suppose you should better try to use window.history which provides onpopstate event when user clicks back button. But you will need to modify url in browser with history.pushState/history.replaceState functions. Possibly, you can add same url to history, so it will not be changed visually. And then take previous URL from your custom history array. But not sure if popstate will work if you place same url with pushState
But that will work in modern browsers only. To make it work in all browsers, you should better use some history plugin (for example this) which will also handle IE using hashtags

Weird $_SESSION behaviour in PHP [duplicate]

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

Categories