Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > return false from ajax

Reply
Thread Tools

return false from ajax

 
 
Andrew Koptyaev
Guest
Posts: n/a
 
      04-10-2010
Hi

I need to test input data from form by php (some mysql) after submit
form. I use onclick event of submit button and run in onclick function
sync jquery ajax get to server php. I know if I want come back to form
I should return false. But I cant return false from success ajax function.

Is exist solution to come back to form not submit.

Thank you!

Andrew.
 
Reply With Quote
 
 
 
 
Sean Kinsey
Guest
Posts: n/a
 
      04-10-2010
On Apr 10, 7:24*pm, "Andrew Koptyaev" <(E-Mail Removed)> wrote:
> Hi
>
> I need to test input data from form by php (some mysql) after submit
> form. I use onclick event of submit button and run in onclick function
> sync jquery ajax get to server php. I know if I want come back to form
> I should return false. But I cant return false from success ajax function..
>
> Is exist solution to come back to form not submit.
>
> Thank you!
>
> Andrew.


In stead of running the xhr in the onclick event, run a regular
asynchronous call instead and return false to stop the submit.
Then when the xhr finishes you either submit the form using js or show
some feedback about what was wrong.
 
Reply With Quote
 
 
 
 
Andrew Koptyaev
Guest
Posts: n/a
 
      04-10-2010
> In stead of running the xhr in the onclick event, run a regular
> asynchronous call instead and return false to stop the submit.
> Then when the xhr finishes you either submit the form using js or show
> some feedback about what was wrong.


Thank you.
But in my config if do on submit onclick="return false;" then submit button
not working as expected.

If I do like onclick="checkForm();"
where:
function checkForm(){
return false;
}
then my form submit as not expected.

weird.

--
http://luvz.ru
 
Reply With Quote
 
Andrew Koptyaev
Guest
Posts: n/a
 
      04-10-2010
sorry for the link in the footer


 
Reply With Quote
 
Sean Kinsey
Guest
Posts: n/a
 
      04-10-2010
On Apr 10, 10:06*pm, "Andrew Koptyaev" <(E-Mail Removed)> wrote:
> > In stead of running the xhr in the onclick event, run a regular
> > asynchronous call instead and return false to stop the submit.
> > Then when the xhr finishes you either submit the form using js or show
> > some feedback about what was wrong.

>
> Thank you.
> But in my config if do on submit onclick="return false;" then submit button
> not working as expected.
>
> If I do like onclick="checkForm();"
> where:
> function checkForm(){
> return false;}
>
> then my form submit as not expected.
>
> weird.


Not weird at all.

its either
onclick="return false;"
or
onclick="return checkForm();"
where checkform = function(){return false;}

 
Reply With Quote
 
Andrew Koptyaev
Guest
Posts: n/a
 
      04-11-2010

> its either
> onclick="return false;"
> or
> onclick="return checkForm();"
> where checkform = function(){return false;}
>

That is true.
But question regarding submit from JavaScript.

If I do document.myForm.submit() then I got php script in my URL
and no anything on screen . This php script is form "myForm" action.

If I try to document.getElementById("myInput").submit() where
myInput is input with submit button
then got document.getElementById("myImput").submit is not a function

what is the solution?
 
Reply With Quote
 
Andrew Koptyaev
Guest
Posts: n/a
 
      04-11-2010
> If I try to document.getElementById("myInput").submit() where
> myInput is input with submit button
> then got document.getElementById("myImput").submit is not a function
>
> what is the solution?


If I try to document.getElementById("myInput").submit() where
myInput is input with submit button
then got document.getElementById("myInput").submit is not a function

fix error but still not working
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      04-11-2010
On Apr 11, 1:10*pm, "Andrew Koptyaev" <(E-Mail Removed)> wrote:
> If I try to document.getElementById("myInput").submit() where
> myInput is input with submit button
> then got document.getElementById("myInput").submit is not a function


That is because you named or id'ed your submit button "Submit" or
"submit". NEVER ever do it. The best of all: forget that input[submit]
has name or id attribute: unless forced to deal with multiple submit
buttons.

<form action="your.php" onsubmit="validate(this); return false;">
....
<input type="submit">
</form>

function validate(frm) {
// if frm input server-side sync check OK then:
// frm.submit();
//
// On programmatic submit() form onsubmit handler
// is not called
}

Of course AJAX call has to be synced and of course it is very bad is
server-side check may tack a noticeable amount of time: for user it
will be like the browser became frozen on submit click.

To make it really usable you need to break it on blocks and allow
screen refresh, something like:

<form action="your.php" onsubmit="return validate(this);">
....
<input type="submit">
</form>

function validate(frm) {
// show "checking" message
// or any visual signal that something
// is going on but may take time

// release context so the page could be updated:
window.setTimeout(validate2(frm), 10);

return false;
}

function validate2(frm) {
// do AJAX data check
// if frm input server-side sync check OK then:
// frm.submit();
//
// On programmatic submit() form onsubmit handler
// is not called
}

etc.
 
Reply With Quote
 
Sean Kinsey
Guest
Posts: n/a
 
      04-11-2010
On Apr 11, 3:15*pm, VK <(E-Mail Removed)> wrote:
> On Apr 11, 1:10*pm, "Andrew Koptyaev" <(E-Mail Removed)> wrote:
>
> > If I try to document.getElementById("myInput").submit() where
> > myInput is input with submit button
> > then got document.getElementById("myInput").submit is not a function

>
> That is because you named or id'ed your submit button "Submit" or
> "submit".


I can see nothing to support that statement.
The reason is that the input element has not submit method, only the
form.

> NEVER ever do it. The best of all: forget that input[submit]
> has name or id attribute: unless forced to deal with multiple submit
> buttons.
>
> <form action="your.php" onsubmit="validate(this); return false;">
> ...
> *<input type="submit">
> </form>
>
> function validate(frm) {
> *// if frm input server-side sync check OK then:
> *// frm.submit();
> *//
> *// On programmatic submit() form onsubmit handler
> *// is not called
>
> }
>
> Of course AJAX call has to be synced and of course it is very bad is
> server-side check may tack a noticeable amount of time: for user it
> will be like the browser became frozen on submit click.


There is no reason to use a synchronous call here.

>
> To make it really usable you need to break it on blocks and allow
> screen refresh, something like:
>
> <form action="your.php" onsubmit="return validate(this);">
> ...
> *<input type="submit">
> </form>
>
> function validate(frm) {
> *// show "checking" message
> *// or any visual signal that something
> *// is going on but may take time
>
> *// release context so the page could be updated:
> *window.setTimeout(validate2(frm), 10);


This is an error, its either 'setTimeout("validate2(frm);",10);' which
is bad as it uses eval and thus runs only in the global scope,
or 'setTimeout(function(){validate2(frm);},10)'
Either way, its not needed if you just start an async operation

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      04-11-2010
> > That is because you named or id'ed your submit button "Submit" or
> > "submit".

>
> I can see nothing to support that statement.
> The reason is that the input element has not submit method, only the
> form.


Very true? and that's the point Now try this:

<form action="">
<input type="submit" name="submit" value="Submit">
</form>

and now document.forms[0].submit() or any variants. A very old DHTML
oops, never fixed.


> > NEVER ever do it. The best of all: forget that input[submit]
> > has name or id attribute: unless forced to deal with multiple submit
> > buttons.

>
> > <form action="your.php" onsubmit="validate(this); return false;">
> > ...
> > *<input type="submit">
> > </form>

>
> > function validate(frm) {
> > *// if frm input server-side sync check OK then:
> > *// frm.submit();
> > *//
> > *// On programmatic submit() form onsubmit handler
> > *// is not called

>
> > }

>
> > Of course AJAX call has to be synced and of course it is very bad is
> > server-side check may tack a noticeable amount of time: for user it
> > will be like the browser became frozen on submit click.

>
> There is no reason to use a synchronous call here.


There is a very good one. Otherwise the AJAX check request will be
sent and - right after - the form submitted without waiting check
results.

> > To make it really usable you need to break it on blocks and allow
> > screen refresh, something like:

>
> > <form action="your.php" onsubmit="return validate(this);">
> > ...
> > *<input type="submit">
> > </form>

>
> > function validate(frm) {
> > *// show "checking" message
> > *// or any visual signal that something
> > *// is going on but may take time

>
> > *// release context so the page could be updated:
> > *window.setTimeout(validate2(frm), 10);

>
> This is an error, its either 'setTimeout("validate2(frm);",10);' which
> is bad as it uses eval and thus runs only in the global scope,
> or 'setTimeout(function(){validate2(frm);},10)'
> Either way, its not needed if you just start an async operation


The latter one, right. My typo? thank you for correcting.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
AJAX IDE and AJAX TOOL--The Release of JoyiStar AJAX WebShop 3 Beta minnie Java 1 12-13-2006 06:29 AM
False positive, false intrusion, false alarm Nick Computer Security 3 04-26-2006 07:40 PM
"return false" gives a blank page with the word "false" ? kurtj@ev1.net Javascript 10 02-16-2006 04:13 PM
onselectstart="return false" ondragstart="return yingjian.ma1955@gmail.com Javascript 1 10-06-2005 10:18 PM
difference between return true; and return false; w i l l Javascript 4 07-04-2003 10:05 PM



Advertisments