Forum Moderators: open

Message Too Old, No Replies

many submit button for one form

form, php, javascript, html

         

hanyaz

5:03 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



hello,
i want to have one form with three submit buttons performing different actions.
How could i perform that ?
thanks for the help
hanyaz

Fotiman

5:07 pm on Apr 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




<form action="processForm.php">
<div>
...
<input type="submit" name="action1" value="Do Action 1">
<input type="submit" name="action2" value="Do Action 2">
<input type="submit" name="action3" value="Do Action 3">
</div>
</form>

Then in your processForm.php (or whatever you use to process your form), check to see whether action1, action2, or action3 was submitted and perform whatever action based on that.

rocknbil

6:26 pm on Apr 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could also name them all the same and check the value:

<input type="submit" name="submitButton" id="action1" value="Do Action 1">
<input type="submit" name="submitButton" id="action2" value="Do Action 2">
<input type="submit" name="submitButton" id="action3" value="Do Action 3">

...........

if ($data{'submitButton'} == 'Do Action 1') { &action1; }
elsif ($data{'submitButton'} == 'Do Action 2') { &action2; }
elsif ($data{'submitButton'} == 'Do Action 3') { &action3; }
else { &default_action; }

"elsif" is strictly perl, most languages use "else if"

hanyaz

7:49 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



Hello,
I have tried the following :

<script language="JavaScript">
function publish()
{
document.action="publish";

}

function unpublish()
{
document.action="unpublish";

}
</script>

and in my form i got 2 buttons :

<input name='publish' type='image' src='img/add2.png' value='publish' onClick="publish()">

and

<input name="unpublish" type="image" src="img/delete2.png" value="unpublish" onClick="unpublish()">

It does not work
thanks for the help
hanyaz

hanyaz

8:32 pm on Apr 10, 2008 (gmt 0)

10+ Year Member



hello,
Finally found something very usefull

thanks for your time
hanyaz

[edited by: jatar_k at 10:59 pm (utc) on April 10, 2008]
[edit reason] no urls thanks [/edit]

Fotiman

2:33 pm on Apr 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, you should not be using JavaScript to change the action of your form, as people with JavaScript disabled will get incorrect results. Treat JavaScript as an enhancement, never as a dependency.

Fotiman

3:10 pm on Apr 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




You could also name them all the same and check the value:

The problem with this approach, though, is that the "value" of the input is what gets displayed as the button text. If you ever decide to internationalize your form, then the value would become the text for those other languages, and your form processing would fail. While most web forms are probably not internationalized, I still consider it a best practice to avoid that approach. Just my 2 cents.