4

I have a chunk of code that defines divs and some of the callbacks on them when clicked. To avoid code redundancy, I want to reuse it. The callbacks use $.post calls to communicate with the server whereas, for this specific page, I want a page refresh the way a form submit does.

Is there any difference in using a form submit to submitPage.php and using the following?

$.post('submitPage',  dataParams, null, 'json').
      success(function(resp, status, req) {
        window.location = 'submitPage.php';  // redirect
      });

I am curious about how this might intrinsically effect the processing of entries in dataParams by submitPage.php.

1
  • the only difference i see it there will not be a post back event Commented Nov 29, 2011 at 20:12

1 Answer 1

1

The difference is that when you submit a form normally, the page request also has the data serialized along with it. You are submitting the form as an AJAX request and then redirecting the user to a page without the serialized form data attached.

So if you need the $_POST variable to be accessible on the submitPage.php then I would let the form submit normally rather than hi-jacking the submit with an AJAX request and then redirecting the user.

You could add the $_POST data to the $_SESSION and then use that when you redirect the user but unless you have a reason to do that it seems like an unnecessary bit of code.

Sign up to request clarification or add additional context in comments.

3 Comments

So, the processing on the data happens when the server processes the post request. After that, I don't need reprocessing. So not sending data the second time might be ok right? Would this work out?
@Navneet The issue I could see happening is that submitPage.php uses $_POST variables to populate itself with data, if that's the case then when you redirect the user these variables will not be there for the submitPage.php page.
I see that problem. Was using isset() to check but I am starting to get the feeling that might be too hacky a solution. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.