Page 1 of 1

submit button doesn't work when keypress

Posted: Wed Oct 22, 2003 1:15 am
by valen53
i face a stranger problem.

Code: Select all

<script language="JavaScript1.2">
function mainvalidate(form)
&#123;
  if (document.forms&#1111;0].key.value=="") &#123;
     alert("Electronic key can not be blank ! ");
	return(false); 
  &#125;	
&#125;
</script>

<input name="Confirm" type="submit" id="Confirm" value="Confirm" onClick="return mainvalidate(this.form)">
submit button doesn't work when press enter key. But it work when use mouse clicking.

anyone can give me comment about that problem ?
thank u very much.

Posted: Wed Oct 22, 2003 3:09 am
by twigletmac
In some browsers, pressing enter is not considered the same thing as physically selecting the submit button and clicking it. I know very little about JS but could you use the onSubmit event to run the validation instead of the onClick event?

Mac

Posted: Wed Oct 22, 2003 6:48 am
by richie256
onSubmit is an attribute for <FORM ...>

Posted: Wed Oct 22, 2003 9:28 am
by twigletmac
Exactly, relying on a press of a submit button whether in JS or PHP is essentially flawed thanks to differing browser implementations.

Mac

Posted: Wed Oct 22, 2003 12:41 pm
by cybaf
you could also set the action of the form to run the mainvalidate() function and thereafter change the action to where you want it to go and submit the form.

Posted: Wed Oct 22, 2003 9:22 pm
by valen53
thanks 4 all.

onSubmit was working. But when press the enter, the form cannot post, i dunno which part of my coding contain error.

Code: Select all

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ( $_POST&#1111;'Confirm'] == 'Confirm' ) &#123; 
    echo'
<script language="JavaScript">
     alert("  '.$Confirm.' Was Incorrect, Please Try Again ! ");
</script>
   ';  &#125;
?>
<form action="<? echo $PHP_SELF ?>" method="post" name="form1" onsubmit ="return mainvalidate(this.form)">
<input name="key" type="password" >
<input name="Confirm" type="submit" id="Confirm" value="Confirm" >
</body>
</html>
when mouse click, it will alert but enter key cannot.
i want to ask that mostly which part of code will cause that kind of error.

Posted: Thu Oct 23, 2003 7:25 am
by cybaf
try this:

Code: Select all

<html> 
<head> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript1.2"> 
function mainvalidate(form) 
&#123; 
  if (document.forms&#1111;0].key.value=="") &#123; 
     alert("Electronic key can not be blank ! "); 
   return(false); 
  &#125;    
&#125; 
</script>
</head> 
<body> 
<?php 
if ($_POST&#1111;'test']) &#123; 
    echo' 
<script language="JavaScript"> 
     alert("  '.$_POST&#1111;"Confirm"].' Was Incorrect, Please Try Again ! "); 
</script> 
   ';  &#125; 
?> 
<form action="<? echo $PHP_SELF ?>" method="post" name="form1" onSubmit ="mainvalidate(this.form)">
<input type="hidden" name="test" value="1">
<input name="key" type="password" > 
<input name="Confirm" type="submit" id="Confirm" value="Confirm" > 
</body> 
</html>

Posted: Fri Oct 24, 2003 10:46 am
by m3rajk
before you go changing your entire page, can you show us the html? and tell us where you were placing onclick versus onsubmit?

(i have a hunch i know what's causing that but need more to know if i'm right... which if i am there is likely nothing wrong per se, so the changes will be very minor, and i will explain it all if i am right, but to know that i need to see the html)

Posted: Sun Oct 26, 2003 7:52 pm
by valen53
thank for reply

i have try to test " cybaf " code already. But the problem still there.

when i use mouse clicking. it will popup
"Confirm Was Incorrect, Please Try Again ! "

but use keyboard enter,only popup
"Was Incorrect, Please Try Again ! "

The javascript working correctly.

i suprise that what's this problem .. b'cos my other file was work correctly. Then i use my other file to testing. Finally i found a quite funny solution.

i insert another textfield. Then the form just can post if press enter key.
Suprise, must be more than 1 textfield, form just can post if press enter key.
:lol: :lol:

Posted: Mon Oct 27, 2003 7:26 am
by cybaf
valen53 wrote:i have try to test " cybaf " code already. But the problem still there.
my bad... sorry about that... it seems as the submitbutton is not posted with the rest of the variables when the enterkey is used to submit the form. so if you test for some other variable istead of the submitbutton, you should be ok.