...

View Full Version : submit form with enter key in PHP



harkly
08-27-2010, 08:41 PM
I am trying to get my form which is in PHP to submit when pressing the Enter key

This is what I am using


<script type="text/javascript">

function submitFormWithEnter(myfield,e)
{
var keycode;
if (window.event)
{
keycode = window.event.keyCode;
}
else if (e)
{
keycode = e.which;
}
else
{
return true;
}

if (keycode == 13)
{
myfield.form.submit();
return false;
}
else
{
return true;
}
}
</script>



<div id='aboutForm'>
<span class='headerbox'><b>Your Profile Headline:</b></span>
<span class='textbox'><input type='text' name='headline' class='zip' size='67 ' value='$headline'
onKeyPress=\"return submitFormWithEnter(this,event)\"></span>
</div>

Anyway I can get this to work?

Seems to be an issue with the "text" input

jimhill
08-27-2010, 08:53 PM
Just add this to the code on the form. Warning though text areas will trigger it if they hit enter.


<script language="javascript">
document.onkeypress = function(){
if(event.keyCode == 13)document.form_name.submit();
}
</script>

Fou-Lu
08-27-2010, 10:16 PM
This has nothing to do with PHP.
Moving from PHP to Javascript.

Old Pedant
08-27-2010, 10:30 PM
<script language="javascript">
document.onkeypress = function(){
if(event.keyCode == 13)document.form_name.submit();
}
</script>
Ummmm...that code will ONLY work in MSIE. Only MSIE has a global event.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum