PDA

View Full Version : Enter key wont submit form



tanel96
February 3rd, 2008, 11:05 AM
I have a regular form with a few text fields and a submit button. When i fill out the form and press the submit button with a mouse then everything works perfectly, but when i try to submit the form by pressing ENTER key... it just reloads the page and doesn't submit the form.

And that ONLY happens when i'm using IE... in FireFox everything works just fine.

Any ideas why this is happening :hugegrin: ?

rondog
February 4th, 2008, 03:06 PM
hmm is the submit button inside ur form tags?

tanel96
February 5th, 2008, 10:00 AM
here is my html:



<form action="/test/admin/file.php?model=smth" method="post" name="addStuff" class="addStuff" id="addStuff">
<table width="300" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="100" height="40">Mudeli nimi:</td>
<td><input type="text" name="var1" id="var1" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="submitStff" id="submitStff" value="Add stuff" /></td>
</tr>
</table>
</form>

fasterthanlight™
February 5th, 2008, 10:25 AM
Strange...... code looks ok... maybe make your id's and name's slightly different, who knows, maybe IE chokes on that.

gregmax
February 5th, 2008, 11:17 AM
I don't rely on the browser to submit any form... they all handle the form differently. This is what I have been doing and now everything works like a charm:

- in the form tag add this: "onsubmit='return false;"
- then instead of having an input type="submit", have it be: "type='button' onClick='beforeSubmit()"
- the "beforeSubmit" will be a function in javascript which you can have it verify and test that all fields (if you want it to that is) have been filled out properly before submitting.
- in side the "beforeSubmit" function, you would do something like this to submit:
var form = document.forms["myform"];
form.submit();

borrob
February 6th, 2008, 07:14 AM
the problem with ie is that the enter button only submit's when the focus is on the form. So that may cause your form not being submitted.

tanel96
February 6th, 2008, 02:01 PM
I tried something like this... but it didn't work:



<html>
<head>
<title>Untitled Document</title>
<SCRIPT language="JavaScript">
function submitForm()
{
var form = document.forms["form1"];
form.submit();
}
</SCRIPT>
</head>
<body>
<?php
if($_POST["button"]){
echo $_POST["txt"]."<br>";
}
?>
<form name="form1" method="post" onsubmit="return false;" action="<?=$_SERVER['PHP_SELF'];?>">
<p>
<input type="text" name="txt">
</p>
<p>
<input type="button" name="button" onclick="submitForm" value="Submit">
</p>
</form>
</body>
</html>