Jump to content

Problems Refreshing With Php/ajax


rmpotter

Recommended Posts

My site has a sidebar in it that is supposed to be refreshed everytime I submit a form with information pertaining to said form. At first I had a submit button on the form that also used some ajax to update some session info, the page would reload as it was a submit. The problem with this is it seems to miss my PHP sometimes, not always and there is no particualr pattern to it. I turned the submit into a button and it works everytime, albeit without refreshing my side bar. Does anyone have an suggestions what could be wrong or how I may get around this problem?

Link to comment
Share on other sites

You might want to post all your code. There are a lot of ways things can go wrong.Programmers often make a simple mistake when doing AJAX. They gather most of the AJAX data through a form, and then use a submit button to trigger the AJAX process -- but they forget to capture the submit event. This can lead to strange behavior, where your AJAX process occurs, but the whole page also gets refreshed. This is especially true when the form has a submit button, but the AJAX process is triggered by a click handler. If it's not captured correctly, the submit event will still fire. You seem to have discovered this. One way to handle the problem is to attach a submit handler to the form itself, and make sure the handler function returns false. Something like:

my_form.onsubmit = function () {	// do some AJAX stuff	// other statements	return false;}

You talk about your sidebar getting "refreshed." Is this something your readystatechange handler does? (Do you have a readystatechange handler? Is it functioning correctly?) Or was the sidebar only getting "refreshed" because the form was getting submitted and the whole page was refreshing? The word "refresh" is setting off all kinds of alarm bells in my head. Maybe it's just your word choice, but when people talk about AJAX they usually don't discuss things getting "refreshed." That word usually describes the process of submitting a form the traditional way. So I'm wondering how you've got things set up.

Link to comment
Share on other sites

I am fairly new to ajax so bear with me here. My intentions were to allow the ajax do it's thing and then refresh the side bar through traditional post method. The sidebar does not actually use any ajax so just realoading the entire page would suffice after the PHP code does it's thing. Is this possible?

Link to comment
Share on other sites

You need to trigger the refresh after the ajax comes back. It seems to defeat the purpose of using ajax though, if you're going to refresh the page anyway, why not send all of the data (including what you send through ajax) through a normal form post?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...