Click to See Complete Forum and Search --> : onsubmit trigger


aboatwri
06-11-2003, 11:49 AM
I have a number of javascript books which are good to start with but none seem to cater for the following. Bascially I have a for which has an ACTION defined. At the bottom of the form I have three buttons 'Submit', 'Cancel' and 'Delete'. Bascially I would like to be able to decide which action I call dynamically when the submit happens. I would not have thought that this was difficult but any help would be gratefully received.

Thank you in advance.

Regards
Antoine

AdamGundry
06-11-2003, 12:03 PM
It's probably better if you keep one action and use three different named submit buttons, checking which was pressed server-side - that way your page will work if JS is disabled.

If you must change the action dynamically, however, use
document.forms[0].action = 'example.php';

Adam

aboatwri
06-12-2003, 03:15 AM
Thank you adam, but I have created the following piece of test script which does not work. I can see the value of the input field but not of the button whuch says 'undefined'?

any thoughts.

--------HTML------------
<head>
<title>Add/Update Menus</title>
<script LANGAUGE="JavaScript" type="text/javascript" >

function subcheck()
{

alert("button:" + document.update_form.button.value);
alert("field:" + document.update_form.field.value);
}

</script>
</head>
<body>
ccccc
<form name="update_form" action="xxxx.htm" method="post" onsubmit="subcheck();">

<input name=field type=text>

<input name=button type="submit" value="submit">
<input name=button type="submit" value="delete">

</form>
</body>

JHL
06-12-2003, 03:35 AM
You should change the name of your button.

try this:

<head>
<title>Add/Update Menus</title>
<script LANGAUGE="JavaScript" type="text/javascript" >

function subcheck()
{

alert("button:" + document.update_form.button1.value);
alert("field:" + document.update_form.field.value);
}

</script>
</head>
<body>
ccccc
<form name="update_form" action="xxxx.htm" method="post" onsubmit="subcheck();">

<input name=field type=text>

<input name='button1' type="submit" value="submit">
<input name='button2' type="submit" value="delete">

</form>
</body>

aboatwri
06-12-2003, 08:23 AM
Thanks but the problem then is that I do not know which button has been pressed. Ideally I want to be able to press either the "submit" button and know it or "delete" and know it. Thia solution on allows me to know what the individual button values are. Maybe there is another way of knowing which of the buttons triggered the SUBMIT.

Thank you

AdamGundry
06-12-2003, 08:30 AM
How about this?

<head>
<title>Add/Update Menus</title>
<script LANGAUGE="JavaScript" type="text/javascript" >

function subcheck()
{

alert("button:" + buttonpressed);
alert("field:" + document.update_form.field.value);
}

</script>
</head>
<body>
ccccc
<form name="update_form" action="xxxx.htm" method="post" onsubmit="subcheck();">

<input name="field" type="text">

<input name="button" type="submit" value="submit" onclick="buttonpressed = 'submit'">
<input name="button" type="submit" value="delete" onclick="buttonpressed = 'delete'">

</form>
</body>

Khalid Ali
06-12-2003, 08:36 AM
Without knowing why you are trying to do what you are..

here is something I put together...hope this helps


<head>
<title>Add/Update Menus</title>
<script LANGAUGE="JavaScript" type="text/javascript" >
var whichBtn;
function subcheck(){
alert("button:" + whichBtn.value);
alert("field:" + document.update_form.field.value);
}

function SetWhichButton(btn){
whichBtn = btn;
}
</script>
</head>
<body>
ccccc
<form name="update_form" action="xxxx.htm" method="post" onsubmit="subcheck();">
<input name=field type=text>
<input type="submit" value="submit" onclick="SetWhichButton(this);"">
<input type="submit" value="delete" onclick="SetWhichButton(this);">
</form>
</body>