Ajax Talk - Asynchronous javascript and xml discussion board
 FAQ   Search    Usergroups      Register   Profile   Check your messages   Log in 
Signup now to enjoy a range of free membership advantages !!
Ajax Talk - Moderators Wanted
Preventing Form submission

 
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Help
View previous topic :: View next topic  
Author Message
Snyke
Forum Assistant
Forum Assistant


Joined: 20 Jun 2006
Posts: 87
Location: Zurich

PostPosted: Tue Aug 22, 2006 8:56 pm    Post subject: Preventing Form submission Reply with quote

Ok this is my problem: I have a Form that should trigger some action on submit and I don't want the form to be submitted the traditional way. How do I capture the event (onsubmit in the form tag I assume) and how do I prevent the event from being propagated, so that the form is not submitted?
_________________
[::[ Snyke.net ]::]
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
deejay
Forum Assistant
Forum Assistant


Joined: 17 May 2006
Posts: 78
Location: Netherlands

PostPosted: Fri Aug 25, 2006 7:35 am    Post subject: a simple javascript Reply with quote

Hi Snyke,

If i understand you correctly a simple javascript should do the trick.

Code:
<SCRIPT LANGUAGE="JavaScript"><!--
function myFunction() {
    var failed = false;
    // stick some form field validation in here
    if (failed)
        return false;
    else
        return true;
}   
//--></SCRIPT>

<FORM NAME="myForm" onSubmit="return myFunction()">
<!-- put some form fields in here -->
<INPUT NAME="myButton" TYPE="SUBMIT">
</FORM>

this example comes from irt.org ; this is a very handy resource, especially the javascript faq section;

http://irt.org/script/script.htm

(scroll down)

for some reason the syntax of javascript doesn't want to stay in the head, maybe because it's so damn interpunctual...
_________________
please correct me if i am wrong
seo-expertglobalwarming
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
iporter
New one
New one


Joined: 24 Sep 2007
Posts: 2

PostPosted: Mon Sep 24, 2007 2:29 pm    Post subject: ...but after sending an request,this doesn't work in Firefox Reply with quote

I have a problem related to this. I expected to use the solution here, but I found that Firefox doesn't execute the return statement after perfoming an xmlHTTP request. In the code below, the form submits after the alert pops up:

Code:

<form onSubmit="return !loginUser();">

function loginUser() {
   if (request = createHTTPRequest()) {
      sendRequest(request, "POST", loginHandler, url, parameters);
      return true;
   }
   return false;
}

function sendRequest (request, method, loginHandler, url, parameters) {
   try{
      request.onreadystatechange = function () {
         if (request.readyState == 4) {
            if (request.status == 200) {
               handler(request);
            } else {
               // handle error
            }
         }
      };
      request.open(method, url, true);
      request.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
      request.send(parameters);
   } catch (e) {
      // handle error
   }
}

function handler(request) {
   alert("The request is complete, but Firefox will now submit the form");
}


IE goes back to the loginUser() function and returns true to the onSubmit (note the ! in the onSubmit to prevent the form submission), but Firefox doesn't. Any ideas why?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Ajaxtalk.com Forum Index -> AJAX Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
 Advertisements
 XML & Copyright Notice 
    


Powered by phpBB © 2001, 2005 phpBB Group

What is according to Wikipedia: Ajax Web2.0 XML E4X


Link to our site:

  


del.icio.us digg spurl wists simpy newsvine blinklist furl blogmarks yahoo! myweb smarking ma.gnolia segnalo reddit fark technorati cosmos


Normal Bookmark


Partner Button Links:
Codecrunch.com: Webmaster Tutorials    One Ajax


Partner Text Links:
quomon | web20log | ajaxmatters | tableless | 456bereastreet | music charts | gift ideas | competitions | free xbox 360 | link trade ?


Credits:
it consultancy & project management lead generation zoekmachine optimalisatie steal these buttons seo expert