Conditional onsubmit events

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

    Conditional onsubmit events

    Hi,

    I have a form that has 2 submit buttons. The form action is a php
    script. What I'm trying to do is to execute a different javascript
    function depending on which submit button is pressed. So far I've tried
    the following with no success. In the code below, a different function
    would execute based on the value of the "flag" variable.

    <HTML>
    <HEAD>
    <SCRIPT TYPE="text/javascript">
    function some_function() {
    alert(flag);
    }
    </SCRIPT>
    </HEAD>
    <BODY>
    <form name="form1" action="./submit.php" method="POST"
    onsubmit="javas cript:some_func tion()">
    <input type="text" name="name" size=50>
    <input type="submit" name="submit1" value="Submit 1"
    onclick="javasc ript:var flag=1;">
    <input type="submit" name="submit2" value="Submit 2"
    onclick="javasc ript:var flag=2;">
    </form>
    </BODY>
    </HTML>


    <?php
    if ($_POST[submit1]) {
    echo "submit1";
    }

    if ($_POST[submit2]) {
    echo "submit2";
    }
    ?>

  • tom

    #2
    Re: Conditional onsubmit events

    Hello,
    you sholud try this
    first write this function into head section
    function checkpressedbtn (obj)
    {
    if (obj.value == "Submit 1")
    ------- go to your function -----------
    else
    ------- go to another function -----------
    }

    then change your buttons like this
    input type="submit" name="submit1" value="Submit 1"
    onclick="javasc ript: checkpressedbtn (this)">


    <input type="submit" name="submit2" value="Submit 2"
    onclick="javasc ript: checkpressedbtn (this)">


    Raffi wrote:[color=blue]
    > Hi,
    >
    > I have a form that has 2 submit buttons. The form action is a php
    > script. What I'm trying to do is to execute a different javascript
    > function depending on which submit button is pressed. So far I've tried
    > the following with no success. In the code below, a different function
    > would execute based on the value of the "flag" variable.
    >
    > <HTML>
    > <HEAD>
    > <SCRIPT TYPE="text/javascript">
    > function some_function() {
    > alert(flag);
    > }
    > </SCRIPT>
    > </HEAD>
    > <BODY>
    > <form name="form1" action="./submit.php" method="POST"
    > onsubmit="javas cript:some_func tion()">
    > <input type="text" name="name" size=50>
    > <input type="submit" name="submit1" value="Submit 1"
    > onclick="javasc ript:var flag=1;">
    > <input type="submit" name="submit2" value="Submit 2"
    > onclick="javasc ript:var flag=2;">
    > </form>
    > </BODY>
    > </HTML>
    >
    >
    > <?php
    > if ($_POST[submit1]) {
    > echo "submit1";
    > }
    >
    > if ($_POST[submit2]) {
    > echo "submit2";
    > }
    > ?>[/color]

    Comment

    • Lee

      #3
      Re: Conditional onsubmit events

      Raffi said:[color=blue]
      >
      >Hi,
      >
      >I have a form that has 2 submit buttons. The form action is a php
      >script. What I'm trying to do is to execute a different javascript
      >function depending on which submit button is pressed. So far I've tried
      >the following with no success. In the code below, a different function
      >would execute based on the value of the "flag" variable.[/color]

      Get rid of the "javascript :" labels and the "var" keyword, which
      makes the flag variable local. You want it to be global.

      <HTML>
      <HEAD>
      <SCRIPT TYPE="text/javascript">
      function some_function() {
      alert(flag);
      }
      </SCRIPT>
      </HEAD>
      <BODY>
      <form name="form1" action="./submit.php" method="POST"
      onsubmit="some_ function()">
      <input type="text" name="name" size=50>
      <input type="submit" name="submit1"
      value="Submit 1" onclick="flag=1 ;">
      <input type="submit" name="submit2"
      value="Submit 2" onclick="flag=2 ;">
      </form>
      </BODY>
      </HTML>

      Comment

      • Raffi

        #4
        Re: Conditional onsubmit events


        Lee wrote:[color=blue]
        > Raffi said:[color=green]
        > >
        > >Hi,
        > >
        > >I have a form that has 2 submit buttons. The form action is a php
        > >script. What I'm trying to do is to execute a different javascript
        > >function depending on which submit button is pressed. So far I've tried
        > >the following with no success. In the code below, a different function
        > >would execute based on the value of the "flag" variable.[/color]
        >
        > Get rid of the "javascript :" labels and the "var" keyword, which
        > makes the flag variable local. You want it to be global.
        >
        > <HTML>
        > <HEAD>
        > <SCRIPT TYPE="text/javascript">
        > function some_function() {
        > alert(flag);
        > }
        > </SCRIPT>
        > </HEAD>
        > <BODY>
        > <form name="form1" action="./submit.php" method="POST"
        > onsubmit="some_ function()">
        > <input type="text" name="name" size=50>
        > <input type="submit" name="submit1"
        > value="Submit 1" onclick="flag=1 ;">
        > <input type="submit" name="submit2"
        > value="Submit 2" onclick="flag=2 ;">
        > </form>
        > </BODY>
        > </HTML>[/color]

        Thanks, I will give these a try.

        Raffi

        Comment

        • RobG

          #5
          Re: Conditional onsubmit events

          Lee wrote:[color=blue]
          > Raffi said:
          >[color=green]
          >>Hi,
          >>
          >>I have a form that has 2 submit buttons. The form action is a php
          >>script. What I'm trying to do is to execute a different javascript
          >>function depending on which submit button is pressed. So far I've tried
          >>the following with no success. In the code below, a different function
          >>would execute based on the value of the "flag" variable.[/color]
          >
          >
          > Get rid of the "javascript :" labels and the "var" keyword, which
          > makes the flag variable local. You want it to be global.
          >
          > <HTML>
          > <HEAD>
          > <SCRIPT TYPE="text/javascript">
          > function some_function() {
          > alert(flag);
          > }
          > </SCRIPT>
          > </HEAD>
          > <BODY>
          > <form name="form1" action="./submit.php" method="POST"
          > onsubmit="some_ function()">
          > <input type="text" name="name" size=50>
          > <input type="submit" name="submit1"
          > value="Submit 1" onclick="flag=1 ;">
          > <input type="submit" name="submit2"
          > value="Submit 2" onclick="flag=2 ;">
          > </form>
          > </BODY>
          > </HTML>
          >[/color]

          The form might be submitted without either onclick function running - it
          may be submitted by pressing the enter key or javascript may be disabled
          or unavailable.

          --
          Rob

          Comment

          • Raffi

            #6
            Re: Conditional onsubmit events


            RobG wrote:[color=blue]
            > Lee wrote:[color=green]
            > > Raffi said:
            > >[color=darkred]
            > >>Hi,
            > >>
            > >>I have a form that has 2 submit buttons. The form action is a php
            > >>script. What I'm trying to do is to execute a different javascript
            > >>function depending on which submit button is pressed. So far I've tried
            > >>the following with no success. In the code below, a different function
            > >>would execute based on the value of the "flag" variable.[/color]
            > >
            > >
            > > Get rid of the "javascript :" labels and the "var" keyword, which
            > > makes the flag variable local. You want it to be global.
            > >
            > > <HTML>
            > > <HEAD>
            > > <SCRIPT TYPE="text/javascript">
            > > function some_function() {
            > > alert(flag);
            > > }
            > > </SCRIPT>
            > > </HEAD>
            > > <BODY>
            > > <form name="form1" action="./submit.php" method="POST"
            > > onsubmit="some_ function()">
            > > <input type="text" name="name" size=50>
            > > <input type="submit" name="submit1"
            > > value="Submit 1" onclick="flag=1 ;">
            > > <input type="submit" name="submit2"
            > > value="Submit 2" onclick="flag=2 ;">
            > > </form>
            > > </BODY>
            > > </HTML>
            > >[/color]
            >
            > The form might be submitted without either onclick function running - it
            > may be submitted by pressing the enter key or javascript may be disabled
            > or unavailable.
            >
            > --
            > Rob[/color]

            Thanks for all the suggestions. I got it working through some additions
            and changes to my original script. I prefer not to put the code online
            since the objective was to partially work around popup blockers while
            conditionally redirecting the application via a popup window, and at
            the same time posting the data to a server side php script. I'd rather
            keep these types of scripts off the newsgroups and out of the hands of
            potential abusers.

            Raffi

            Comment

            Working...