Two actions on a form

E

elia

Hello,

I can't do two actions wich must be done on a form having two buttons.
(payment by invoice and payment by Credit card)

The first action establishes a link on two differents pages according
to whether one wants to pay by invoice or credit card. (2 buttons send)


The second action is for check if all the fields were filled.
The two codes are ok by themselves but do not work together!

For the first action (links) I have:

<script language='Javascript'>
function setAction(name) {
if (name=="submit1")
document.forms[0].action= "invoice.php"
else if (name=="submit2")
document.forms[0].action= "credit_card.php"
}
</script>

and on the form:
<input type="submit" id="submit1" name="submit1"
onClick="setAction(this.name)"
<input type="submit" id="submit2" name="submit2"
onClick="setAction(this.name)"
....

and for the second action (check fields) :

<script type="text/javascript">
function verif_fields()
{
if(document.form1.name.value == "")
{
alert("Write your name");
document.forml.name.focus();
return false;
}
if(document.form.email.value == "")
{
alert("Write your email.......
....
return confirm("ok");
}
</script>

and in the form:
<form name="form1" method="post" onSubmit="return verif_fields()" >

I try to do like this but it doesn't work! :

<script type="text/javascript">
function verif_fields()
{
if(document.forml.name.value == "")
{
alert("Write your name");
document.forml.name.focus();
return false;
}
if(document.forml.email.value == "")
{
alert("Write your email ...
....
document.form1.action = button==1? "invoice.php" : "credit_card.php";
return confirm("Ok");
}
</script>

and in the form :

<form name="forml" method="post" onSubmit="return verif_fields()">

<input type="submit" id="submit1" name="submit1" onClick="button=1"
value="Invoice">
<input type="submit" id="submit2" name="submit2" onClick="button=2"
value="Credit card">

Is somebody can help me? Thanks, pascal
 
M

Michael Winter

elia said:
I can't do two actions wich must be done on a form having two buttons.
(payment by invoice and payment by Credit card)

No, it mustn't. Give the buttons a control name (typically the same) and
the one used to submit the form will be successful. That is, its control
name and value will be added to the data set sent to the server. You can
then determine the necessary processing server-side.

It may not be a good idea to have two submit buttons in this case.
Instead, consider using radio buttons or a select element to allow the
user to choose between methods.

Relying on scripting is not a good idea at the best of times. Doing it
with e-commerce solutions is utterly stupid: the idea is to get money
from people, and one cannot do that if the site doesn't work.

[snip]

Mike
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top