473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
4 4948
b1randon
171 Expert 100+
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
d3vkit
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
171 Expert 100+
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
d3vkit
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

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

Similar topics

3
by: Varun | last post by:
Hi There, I have a form("myRequest.asp") and the values from it are retrieved into the page ("output_Print.asp") on which I have two buttons('Save As Complete' and 'Save As Incomplete'). When the...
2
by: Besta | last post by:
Hello all, I am having trouble creating a windows service with a timer. Everything seems to go ok but the elapsed event does not fire.Can anyone shed any light on this, may be something simple as...
1
by: kamleshsharmadts | last post by:
I am using Ajax with struts in web application. from jsp i am calling a function of ajax.js onclick of a button. code of that call function which calling from jsp given as below:- ...
3
by: leos1981 | last post by:
I am currently working on the asp.net web project. I using the return false method to stop the submit action when the user choose to cancel the action. It is working fine before I am installing the...
5
by: holy moly | last post by:
Hello, I have been trying to get this JS form validation code to work ad nauseam... it won't validate, stop if required and submit. The best I managed was to get it to display an empty...
1
by: prashanth023 | last post by:
Hi, This is prashanth kumar. I am getting error , i am gettign number of records using ajax. Please solve this if n >0 i am putting return false in response function. but form is going to...
4
by: Jean Ceugniet | last post by:
Hi, I just made my very first ajax form submitting. This works perfectly (myAjax01 is a variable external to this function). **************************** Code >> ****************************...
2
by: bips2008 | last post by:
The code seems to work fine in other browser but in IE it throws this error. This is very urgent for me and any help would be greatly appreciated For your convienence i have posted the code for the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.