PDA

View Full Version : form.action works different for form.submit() call and submit event. why?


jayapalchandran
05-06-2009, 03:46 PM
Hi,

i have a form with its action set to something.php . and i have a hidden button with name action. so within this form there are two properties with the same name
like fom.action = 'a.php' and <input type='hidden' name='action' value='login'>

like the following

CODE ONE

<form name='aform' action='a.php' method='post' >
<input type='hidden' name='action' value='login'>
<input type='text' name='email'>
<input type='button' value='Send' onclick='validate()'>
</form>

and the script is as follows

<script>
function oh()
{
if(true) // if validatio is true then the form should submit
document.aform.submit()
}
</script>

the above dosen't work because the form.action is getting overridden by hidden type action. this is my assumption.

where as look at the following

CODE TWO

<form name='aform' action='a.php' method='post' onsubmit='return oh()' >
<input type='hidden' name='action' value='login'>
<input type='text' name='email'>
<input type='submit' value='Send' onclick='validate()'>
</form>

and the script is as follows

<script>
function oh()
{
if(true) // if validatio is true then the form should submit
return true
else return false
}
</script>

the above set of code (CODE TWO) works even though there are two properties with the name action... where as the (CODE ONE) will not submit properly...

can any one tell me why? anyway the form is working...
and please dont tell the better give a different name to the input field because some frameworks of php expects an action key value pair ...

so when document.fomname.submit() is called then the form doesn't submit properly where as if we let the default handlers to submit then it works... why this differente

anyway i use my own style of submitting the forms but i want to know the difference ...

...

adios
05-06-2009, 08:55 PM
I hope you're not obsessed with this. Probably just something irregular.
btw Form.onsubmit is not called by Form.submit() - the logic probably being: you're already scripting the submit, so you don't need to trap the event independently.

You're right about the overwriting - a staple of these forums (fora?) is the "Help! I tried to submit my form with document.myform.submit() and I keep getting the error 'document.myform.submit is null or not an object' - help!" Guaranteed that they've named their submit button ... "submit". Aargh.

Use a submit widget (button, image) and a validator call returned to the onsubmit handler, unless you have a reason for not doing that. Degrades better, too.

jayapalchandran
05-09-2009, 08:32 AM
anyway... i was able to handle the situation by using onsubmit... and i suggest people not to use same names for objects(elements). a php frame work was expecting an input of name action and you know most people send the request type in that variable to mean like the following... delete, edit, add ... in the server side... so in this case i suggested using onsubmit ... so in this case i think this is one way of submitting the form.