By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,466 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,466 IT Pros & Developers. It's quick & easy.

Does return false on an Ajax form stop submit values from posting?

P: 35
I have a form on my page, and some javascript which uses ajax to submit the form, and then opens the new page in a div using ajax so there is no refresh. This works fine. But the problem is this: all values seem to pass except what the submit buttons say. I have a preview button and a finish button, and need to check which was pressed. I can add a checkbox for submit, but that's annoying.
I'm using mootools for the ajax call, so it might not look like regular ajax, but...
Expand|Select|Wrap|Line Numbers
  1. submitForm = function(frmName, el)    {
  2.             $(frmName).send({update: $(el), onComplete: function()    {MOOdalBox.init()}});
  3.             return false;
  4.         }
This basically is posting the form (frmName) to the div (el).
So question is, since I'm using return false to stop the form from submitting, does it not send the submit buttons values in post?

In case it helps, the form...
[html]<form action="content/blog/submit.php" id="addBlogForm" method="post" onSubmit="return submitForm(\'addBlogForm\', \'previewBlogDiv\');">

... form stuff here...

<input type="submit" class="submit" name="preview" value="Preview" />
</form>[/html]

And then my php script (submit.php) ...
[php]if($_POST['preview']) {
include('replace.php');
} else {
...
[/php]

It always runs the else statement though, apparently not catching the $_POST of preview?

I'm just trying to figure out what I did wrong in this, since that's about all that I can't check in my php script when it's submitted.
Dec 14 '06 #1
Share this Question
Share on Google+
4 Replies


b1randon
Expert 100+
P: 171
I have a form on my page, and some javascript which uses ajax to submit the form, and then opens the new page in a div using ajax so there is no refresh. This works fine. But the problem is this: all values seem to pass except what the submit buttons say. I have a preview button and a finish button, and need to check which was pressed. I can add a checkbox for submit, but that's annoying.
I'm using mootools for the ajax call, so it might not look like regular ajax, but...
Expand|Select|Wrap|Line Numbers
  1. submitForm = function(frmName, el)    {
  2.             $(frmName).send({update: $(el), onComplete: function()    {MOOdalBox.init()}});
  3.             return false;
  4.         }
This basically is posting the form (frmName) to the div (el).
So question is, since I'm using return false to stop the form from submitting, does it not send the submit buttons values in post?

In case it helps, the form...
[html]<form action="content/blog/submit.php" id="addBlogForm" method="post" onSubmit="return submitForm(\'addBlogForm\', \'previewBlogDiv\');">

... form stuff here...

<input type="submit" class="submit" name="preview" value="Preview" />
</form>[/html]

And then my php script (submit.php) ...
[php]if($_POST['preview']) {
include('replace.php');
} else {
...
[/php]

It always runs the else statement though, apparently not catching the $_POST of preview?

I'm just trying to figure out what I did wrong in this, since that's about all that I can't check in my php script when it's submitted.
I've never used MooTools, I'm used to doing it the long way. If you were doing that I'd suggest that you just throw a variable called "mode" or something into the query string of your ajax GET call, and plug in "submit" or "preview" dynamically. That way you don't have to go through all of the trouble of creating a control (like the checkbox you were talking about). I don't know if that is easy or hard with MooTools, but worth a shot. I'd recommend dropping the toolkit if you're serious about ajax. They don't really help that much and just end up limiting what you can do in the end. Hope that helps.
Dec 14 '06 #2

P: 35
I've never used MooTools, I'm used to doing it the long way. If you were doing that I'd suggest that you just throw a variable called "mode" or something into the query string of your ajax GET call, and plug in "submit" or "preview" dynamically. That way you don't have to go through all of the trouble of creating a control (like the checkbox you were talking about). I don't know if that is easy or hard with MooTools, but worth a shot. I'd recommend dropping the toolkit if you're serious about ajax. They don't really help that much and just end up limiting what you can do in the end. Hope that helps.
I am new to ajax and javascript so bear with me if I am slow with understanding it all. (And I know it doesn't help to jump from ajax to a library when your new to a language... but the effects are very nice, even if unnecessary).

I since changed my idea to just doing a live preview of text typing, but am still interested in this, so for the sake of understanding, I would like to continue this thread, if you don't mind.

What I thought of after reading your reply was this:
-I add a onClick to a button for preview, which has a parameter for mode set.
-My ajax function uses this variable like so:
Expand|Select|Wrap|Line Numbers
  1. http_request.open('GET', url + parameters, true); http_request.send(null);
where mode is in the params. I found this code on the net, so if parameters isn't what I think, sorry. But from the looks of it, url+params to me means ?var=whatever sort of params. Soooo maybe write it like this:
Expand|Select|Wrap|Line Numbers
  1. http_request.open('GET', url + '?mode='+mode, true);
-Since I am using get, it's passed onto the next page, which can use $_GET['var'] to get the mode.

Is this the right idea? If I come across this problem again, I want to be able to tackle it head on.

And does this mean using post is just out of the question? I suppose it wouldn't actually be showing anything in the url bar anyway right?

Thanks for your time!
Dec 16 '06 #3

b1randon
Expert 100+
P: 171
I am new to ajax and javascript so bear with me if I am slow with understanding it all. (And I know it doesn't help to jump from ajax to a library when your new to a language... but the effects are very nice, even if unnecessary).

I since changed my idea to just doing a live preview of text typing, but am still interested in this, so for the sake of understanding, I would like to continue this thread, if you don't mind.

What I thought of after reading your reply was this:
-I add a onClick to a button for preview, which has a parameter for mode set.
-My ajax function uses this variable like so:
Expand|Select|Wrap|Line Numbers
  1. http_request.open('GET', url + parameters, true); http_request.send(null);
where mode is in the params. I found this code on the net, so if parameters isn't what I think, sorry. But from the looks of it, url+params to me means ?var=whatever sort of params. Soooo maybe write it like this:
Expand|Select|Wrap|Line Numbers
  1. http_request.open('GET', url + '?mode='+mode, true);
-Since I am using get, it's passed onto the next page, which can use $_GET['var'] to get the mode.

Is this the right idea? If I come across this problem again, I want to be able to tackle it head on.

And does this mean using post is just out of the question? I suppose it wouldn't actually be showing anything in the url bar anyway right?

Thanks for your time!
You've got the right idea for sure. I did my first AJAX using this tutorial . It uses PHP (you too, right?) and has the proper code for using the xmlrequest object.
Dec 18 '06 #4

P: 35
You've got the right idea for sure. I did my first AJAX using this tutorial . It uses PHP (you too, right?) and has the proper code for using the xmlrequest object.
Thought I'd let you know (and anyone else using mootools that runs into this thread), that I found out mootools has a REALLY easy way to implement the post variables onto an ajax call. It's simply:
Expand|Select|Wrap|Line Numbers
  1. var ajax_function = new Ajax('dir/page_you_want_to_call_to.php', {
  2.        postBody: 'var='+var+'&var2='+var2,
  3.     onComplete: function(responseText) {
  4.         //do something with responseText
  5.     }
  6. }).request();
You then can get the variables sent to the page with $_POST['var'] (in php). I usually send a variable saying 'called_from=internal' to let the page know how the call was made, and then I send 'success' or 'failure' depending on if what I'm doing worked. This is passed back as responseText, so you could just as easily send back what you want the current page say next.
I know I just sort of took my own thread OT and into mootools area, but I hate coming across threads that take me half way to a solution and then nothing else.

Although the aforementioned solution of straight Ajax and no js framework would work just as well.
Feb 7 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.