How to stop form submit when onClick event returns false

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Randell D.

    #1

    How to stop form submit when onClick event returns false


    Folks

    I have the following code for my submit button

    <input type="submit" name="action" value="Save Article"
    onClick="this.v alue='Saving... '; performPrePostC hecks();">

    I have tested performPrePostC hecks() - Its a long script, but I have proven
    its success by having an alert box echo the success with a simple true or
    false message like so:

    <script type="text/javascript">
    function performPrePostC hecks()
    {
    if(checkRequire dFormFields('fo rm1','headline' ,'Headline')==f alse)
    { alert("false");
    return false;
    }

    alert("true");
    return true;
    }
    </script>


    How come that irrespective of the function performPrePostC hecks returning
    true, or false, that the form is still submitted?

    Can someone point me in the right direction?

    Thanks,
    Randell D.


  • Eric Bohlman

    #2
    Re: How to stop form submit when onClick event returns false

    "Randell D." <reply.to.news. group.only@and. share.com> wrote in
    news:g%Wwb.4974 70$pl3.300911@p d7tw3no:
    [color=blue]
    > I have the following code for my submit button
    >
    > <input type="submit" name="action" value="Save Article"
    > onClick="this.v alue='Saving... '; performPrePostC hecks();">[/color]

    [snip]
    [color=blue]
    > How come that irrespective of the function performPrePostC hecks
    > returning true, or false, that the form is still submitted?[/color]

    Because that's what you asked it to do. To be more specific, an action
    handler is treated as if it's a function, with the return value determining
    whether or not to go ahead. You don't return anything, so the default is
    "yes, go ahead." Try returning the result of your checking function.

    Is it possible that you're used to Perl, where a function (aka sub) by
    default returns the value of the last statement executed? Javascript
    doesn't use that convention; if you want to return a value, you have to do
    so explicitly.

    Comment

    • RR

      #3
      Re: How to stop form submit when onClick event returns false

      > onClick="this.v alue='Saving... '; performPrePostC hecks();">

      To clarify Eric's correct comments, you want:
      onClick="this.v alue='Saving... '; return performPrePostC hecks();"

      regards,
      RR


      Comment

      • Stuart Palmer

        #4
        Re: How to stop form submit when onClick event returns false

        put the funtion that returns fale in inte forms onsubmit handler intead of
        the submit button.

        Stu

        "Randell D." <reply.to.news. group.only@and. share.com> wrote in message
        news:g%Wwb.4974 70$pl3.300911@p d7tw3no...[color=blue]
        >
        > Folks
        >
        > I have the following code for my submit button
        >
        > <input type="submit" name="action" value="Save Article"
        > onClick="this.v alue='Saving... '; performPrePostC hecks();">
        >
        > I have tested performPrePostC hecks() - Its a long script, but I have[/color]
        proven[color=blue]
        > its success by having an alert box echo the success with a simple true or
        > false message like so:
        >
        > <script type="text/javascript">
        > function performPrePostC hecks()
        > {
        > if(checkRequire dFormFields('fo rm1','headline' ,'Headline')==f alse)
        > { alert("false");
        > return false;
        > }
        >
        > alert("true");
        > return true;
        > }
        > </script>
        >
        >
        > How come that irrespective of the function performPrePostC hecks returning
        > true, or false, that the form is still submitted?
        >
        > Can someone point me in the right direction?
        >
        > Thanks,
        > Randell D.
        >
        >[/color]


        Comment

        • Randell D.

          #5
          Re: How to stop form submit when onClick event returns false


          "RR" <newspaper.20.b room@spamgourme t.com> wrote in message
          news:KeYwb.2757 8$aT.4901@news-server.bigpond. net.au...[color=blue][color=green]
          > > onClick="this.v alue='Saving... '; performPrePostC hecks();">[/color]
          >
          > To clarify Eric's correct comments, you want:
          > onClick="this.v alue='Saving... '; return performPrePostC hecks();"
          >
          > regards,
          > RR
          >
          >[/color]

          Luvely Juvely... thanks to both of you - and the nice thing is, I actually
          understand where I went wrong and why it should be the way you guys
          suggested...

          Thanks for the help,
          randell d.


          Comment

          Working...