By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

Conditional onsubmit events

P: n/a
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";
}
?>

Aug 24 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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)">
Raffi wrote:
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";
}
?>


Aug 24 '05 #2

P: n/a
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>

Aug 24 '05 #3

P: n/a

Lee wrote:
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>


Thanks, I will give these a try.

Raffi

Aug 25 '05 #4

P: n/a
Lee wrote:
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>


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
Aug 25 '05 #5

P: n/a

RobG wrote:
Lee wrote:
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>


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


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

Aug 26 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.