return value from a function calles inside submit()

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

    return value from a function calles inside submit()

    Hi,

    Some place I use links to submit forms instead of a submit button. The
    way I have done this is: <a
    href="javascrip t:document.getE lementById('<?p hp print "delete$i"
    ;?>').submit()> Delete?</a>.
    This method worked perfectly fine. But then I also wanted to add a confirm
    box to the user where. A confirm("do you really want to delete?").
    So I made the function:
    function controll()
    {
    if(confirm("Do you really want to delete?"))
    {
    return (true);
    }
    else
    {
    return (false);
    }
    }
    And tried to call the function from inside the submit() like
    <a href="javascrip t:document.getE lementById('<?p hp print "delete$i"
    ;?>').submit(co ntroll())>Delet e?</a>.
    This also works fine, but then I have to return the return value. So i
    tried
    <a href="javascrip t:document.getE lementById('<?p hp print "delete$i"
    ;?>').submit(re turn controll())>Del ete?</a>.
    And that does not work.
    It does work inside the onSubmit="..." then.
    So My question is: How could I both use a link inside a form and give the
    user the opportunity to regret the choice?


    Sindre
  • Michael

    #2
    Re: return value from a function calles inside submit()

    sindre <sindrehi@c2i.n et> wrote in message news:<opsaf5i9r cxg0aby@xn--sindres_brbare-8ib>...[color=blue]
    > Hi,
    >
    > Some place I use links to submit forms instead of a submit button. The
    > way I have done this is: <a
    > href="javascrip t:document.getE lementById('<?p hp print "delete$i"
    > ;?>').submit()> Delete?</a>.
    > This method worked perfectly fine. But then I also wanted to add a confirm
    > box to the user where. A confirm("do you really want to delete?").
    > So I made the function:
    > function controll()
    > {
    > if(confirm("Do you really want to delete?"))
    > {
    > return (true);
    > }
    > else
    > {
    > return (false);
    > }
    > }
    > And tried to call the function from inside the submit() like
    > <a href="javascrip t:document.getE lementById('<?p hp print "delete$i"
    > ;?>').submit(co ntroll())>Delet e?</a>.
    > This also works fine, but then I have to return the return value. So i
    > tried
    > <a href="javascrip t:document.getE lementById('<?p hp print "delete$i"
    > ;?>').submit(re turn controll())>Del ete?</a>.
    > And that does not work.
    > It does work inside the onSubmit="..." then.
    > So My question is: How could I both use a link inside a form and give the
    > user the opportunity to regret the choice?
    >
    >
    > Sindre[/color]

    Hi Sindre,

    Why don't you just use a <p> or <span> or <div> for your link?
    I'd do it this way:

    <script type="text/javascript">
    function conditional_sub mit (myForm) {
    if(confirm("Do you really want to delete?")) {
    document.myForm .submit();
    } else {
    //nothing to do
    }
    }
    </script>

    <p onclick="condit ional_submit(do cument.getEleme ntById('<?php print
    "delete$i";?>') ">Delete?</p>

    I didn't test it, but I hope it works. You could combine it with an
    image too.
    HTH

    cu, Michael

    Comment

    • mortb

      #3
      Re: return value from a function calles inside submit()

      sindre <sindrehi@c2i.n et> wrote in message news:<opsaf5i9r cxg0aby@xn--sindres_brbare-8ib>...[color=blue]
      > Hi,
      >
      > Some place I use links to submit forms instead of a submit button. The
      > way I have done this is: <a
      > href="javascrip t:document.getE lementById('<?p hp print "delete$i"
      > ;?>').submit()> Delete?</a>.
      >[/color]
      ....

      you can try:

      href="javascrip t:if(confirm('D o you really want to
      delete?'))docum ent.getElementB yId('<?php print "delete$i"[color=blue]
      > ;?>').submit()" >Delete</a>[/color]

      cheers,
      mortb

      Comment

      • sindre

        #4
        Re: return value from a function calles inside submit()

        [color=blue]
        > Why don't you just use a <p> or <span> or <div> for your link?
        > I'd do it this way:
        >
        > <script type="text/javascript">
        > function conditional_sub mit (myForm) {
        > if(confirm("Do you really want to delete?")) {
        > document.myForm .submit();
        > } else {
        > //nothing to do
        > }
        > }
        > </script>
        >
        > <p onclick="condit ional_submit(do cument.getEleme ntById('<?php print
        > "delete$i";?>') ">Delete?</p>
        >[/color]

        Actually it dont. Firstly onclick in div, p og span is not supported by
        netscape 6.x. But it wont work either in opera or explorer. Nothing
        happens when i move the mouse over the "link" and nothing happens when i
        click the word.

        Sindre

        Comment

        • Michael

          #5
          Re: return value from a function calles inside submit()

          sindre <sindrehi@c2i.n et> wrote in message news:<opsahzcwn bxg0aby@xn--sindres_brbare-8ib>...[color=blue][color=green]
          > > Why don't you just use a <p> or <span> or <div> for your link?
          > > I'd do it this way:
          > >
          > > <script type="text/javascript">
          > > function conditional_sub mit (myForm) {
          > > if(confirm("Do you really want to delete?")) {
          > > document.myForm .submit();
          > > } else {[/color]
          > //nothing to do[color=green]
          > > }
          > > }
          > > </script>
          > >
          > > <p onclick="condit ional_submit(do cument.getEleme ntById('<?php print
          > > "delete$i";?>') ">Delete?</p>
          > >[/color]
          >
          > Actually it dont. Firstly onclick in div, p og span is not supported by
          > netscape 6.x. But it wont work either in opera or explorer. Nothing
          > happens when i move the mouse over the "link" and nothing happens when i
          > click the word.
          >
          > Sindre[/color]

          Sorry, my mistake. I forgot a closing bracket and it should be
          myForm.submit() ; instead of document.myForm .submit().

          Here is an complete example-page, that workes out in Mozilla 1.7,
          Netscape 6.2 and IE 6. It does not work in NS 4.75, because this one
          does not jet support getElementById. I substituted '<?php print
          "delete$i"; ?>' with 'delete1' in my example. As mortb wrote, you can
          place the code of the function in the onclick event directly, depends
          on how many forms you have in your page. You could also just send the
          name(text!) of the form to the function like
          onclick="condit ional_submit('d elete1');", then, depending on the
          browser you could use document.getEle mentsByName,
          document.getEle mentById or even document.all to get your form-object.

          !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
          <html>
          <head>
          <title>tests</title>
          <meta http-equiv="Content-Type" content="text/html;
          charset=iso-8859-15">

          <script type="text/javascript">
          function conditional_sub mit (myForm) { if(confirm("Do you really want
          to delete?")) myForm.submit() }
          </script>

          <body>

          <p onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
          style="cursor:p ointer">Delete? </p>
          <br>
          <div onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
          style="cursor:p ointer">Delete? </div>
          <br>
          <span onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
          style="cursor:p ointer">Delete? </span>
          <br><br>
          <a href="#" onclick="condit ional_submit(do cument.getEleme ntById('delete1 '));return(fals e);">Delete?</a>
          <br><br>
          <a href=javascript :conditional_su bmit(document.g etElementById(' delete1'));>Del ete?</a>
          <br><br><form name="delete1" id="delete1" action="if1.htm l"
          method="get" target="_self">
          <input type="text" size="20" maxlength="30" name="myText">

          </form>

          </body>
          </html>

          Recommendation: Use CSS to format the links

          cu, Michael

          Comment

          • sindre

            #6
            Re: return value from a function calles inside submit()

            [color=blue]
            > !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
            > <html>
            > <head>
            > <title>tests</title>
            > <meta http-equiv="Content-Type" content="text/html;
            > charset=iso-8859-15">
            >
            > <script type="text/javascript">
            > function conditional_sub mit (myForm) { if(confirm("Do you really want
            > to delete?")) myForm.submit() }
            > </script>
            >
            > <body>
            >
            > <p onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
            > style="cursor:p ointer">Delete? </p>
            > <br>
            > <div onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
            > style="cursor:p ointer">Delete? </div>
            > <br>
            > <span onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
            > style="cursor:p ointer">Delete? </span>
            > <br><br>
            > <a href="#"
            > onclick="condit ional_submit(do cument.getEleme ntById('delete1 '));return(fals e);">Delete?</a>
            > <br><br>
            > <a
            > href=javascript :conditional_su bmit(document.g etElementById(' delete1'));>Del ete?</a>
            > <br><br><form name="delete1" id="delete1" action="if1.htm l"
            > method="get" target="_self">
            > <input type="text" size="20" maxlength="30" name="myText">
            >
            > </form>
            >
            > </body>
            > </html>
            >
            > Recommendation: Use CSS to format the links
            >
            > cu, Michael[/color]

            Sorry,
            But It did not work. I used cut and paste the whole section. None of the
            links worked here:-)

            Sindre

            --
            Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

            Comment

            • Grant Wagner

              #7
              Re: return value from a function calles inside submit()

              sindre wrote:
              [color=blue][color=green]
              > > !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
              > > <html>
              > > <head>
              > > <title>tests</title>
              > > <meta http-equiv="Content-Type" content="text/html;
              > > charset=iso-8859-15">
              > >
              > > <script type="text/javascript">
              > > function conditional_sub mit (myForm) { if(confirm("Do you really want
              > > to delete?")) myForm.submit() }
              > > </script>
              > >
              > > <body>
              > >
              > > <p onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
              > > style="cursor:p ointer">Delete? </p>
              > > <br>
              > > <div onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
              > > style="cursor:p ointer">Delete? </div>
              > > <br>
              > > <span onclick="condit ional_submit(do cument.getEleme ntById('delete1 '))"
              > > style="cursor:p ointer">Delete? </span>
              > > <br><br>
              > > <a href="#"
              > > onclick="condit ional_submit(do cument.getEleme ntById('delete1 '));return(fals e);">Delete?</a>
              > > <br><br>
              > > <a
              > > href=javascript :conditional_su bmit(document.g etElementById(' delete1'));>Del ete?</a>
              > > <br><br><form name="delete1" id="delete1" action="if1.htm l"
              > > method="get" target="_self">
              > > <input type="text" size="20" maxlength="30" name="myText">
              > >
              > > </form>
              > >
              > > </body>
              > > </html>
              > >
              > > Recommendation: Use CSS to format the links
              > >
              > > cu, Michael[/color]
              >
              > Sorry,
              > But It did not work. I used cut and paste the whole section. None of the
              > links worked here:-)
              >
              > Sindre[/color]

              If you copied and pasted it verbatim, then the call to "confirm()" probably wrapped, which would
              have resulted in a syntax error.

              I just tested the code (after cleaning it up a bit) and it works fine:

              <html>
              <head>
              <script type="text/javascript">
              function confirm_delete( theForm) {
              // DO NOT LET THE NEXT LINE WRAP
              if (confirm("Do you really want to delete?")) {
              theForm.submit( );
              }
              }
              </script>
              </head>
              <body>

              &lt;P>:
              <p
              onclick="confir m_delete(docume nt.forms['delete1']);"
              style="cursor:p ointer;"[color=blue]
              >Delete?</p>[/color]

              &lt;DIV>:
              <div
              onclick="confir m_delete(docume nt.forms['delete1']);"
              style="cursor:p ointer;"[color=blue]
              >Delete?</div>[/color]

              <br>

              &lt;SPAN>:
              <span
              onclick="confir m_delete(docume nt.forms['delete1']);"
              style="cursor:p ointer;"[color=blue]
              >Delete?</span>[/color]

              <br>
              <br>

              &lt;A>:
              <a
              href="#"
              onclick="
              confirm_delete( document.forms['delete1']);
              return false;
              "[color=blue]
              >Delete?</a>[/color]

              <br><br>

              <form name="delete1" action="action. cgi" method="get">
              <input type="text" size="20" name="myText">
              </form>
              </body>
              </html>

              Everything is split across multiple lines to try make it clearer and to try to avoid
              inappropriate line breaks caused by your news reader software.

              --
              | Grant Wagner <gwagner@agrico reunited.com>

              * Client-side Javascript and Netscape 4 DOM Reference available at:
              * http://devedge.netscape.com/library/...ce/frames.html
              * Internet Explorer DOM Reference available at:
              * http://msdn.microsoft.com/workshop/a...ence_entry.asp
              * Netscape 6/7 DOM Reference available at:
              * http://www.mozilla.org/docs/dom/domref/
              * Tips for upgrading JavaScript for Netscape 7 / Mozilla
              * http://www.mozilla.org/docs/web-deve...upgrade_2.html


              Comment

              • Michael Schmitt

                #8
                Re: return value from a function calles inside submit()

                Grant Wagner wrote:
                [color=blue]
                > If you copied and pasted it verbatim, then the call to "confirm()" probably wrapped, which would
                > have resulted in a syntax error.
                >
                > | Grant Wagner <gwagner@agrico reunited.com>[/color]

                I used the Web-Interface of google, which is breaking the lines. I
                changed to my mail/news client, so this should not happen anymore.

                Comment

                Working...