Conditional onsubmit events

R

Raffi

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="javascript:some_function()">
<input type="text" name="name" size=50>
<input type="submit" name="submit1" value="Submit 1"
onclick="javascript:var flag=1;">
<input type="submit" name="submit2" value="Submit 2"
onclick="javascript:var flag=2;">
</form>
</BODY>
</HTML>


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

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

tom

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="javascript: checkpressedbtn(this)">


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

Lee

Raffi said:
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.

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>
 
R

Raffi

Lee said:
Raffi said:

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>

Thanks, I will give these a try.

Raffi
 
R

RobG

Lee said:
Raffi said:



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>

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.
 
R

Raffi

RobG said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top