Okay so I'm building a very simple one textfield form. Once the input type=submit is pressed it calls some php script to run. The problem is with IE6 and the "enter" key. In IE7 and Firefox this works great when the user presses the Enter key rather than mouse clicking the Submit button. Mouse clicking the Submit button works without flaw on all browsers.
So to fix this I turned to JavaScript. Now the JavaScript works and fires just as it should but for some reason doesn't really "submit" the form meaning the PHP line: isset($_POST['do_send']) doesn't get set to true when the JavaScript code submit() fires. Here is my code:
EDIT: I should mention that the PHP form code is on the same page as the Form itself, hence the reason action="" is set this way. I did this so I can "easily" use the JavaScripts alert function for user feedback.
PHP:
Now the Form itself:PHP Code:<?php
//Newsletter submit code
$em = $_POST['em'];
$msg="";
//checks if submit button was pressed
if(isset($_POST['do_send'])) {
//email validation: checks for "@" and "."
if(!stristr($em, "@") OR !stristr($em, ".")) {
$msg = "Invalid email address. Please try again.";
echo "<script type='text/javascript'>alert('" . $msg . "')</script>";
//if valid email
} else {
$query = ("INSERT INTO newsletter (email) VALUES ('$em')");
$result = mysql_query($query);
//if there was an error. we're particularly interested in error 1062: duplicate entries
if(!$result) {
//if duplicate email entry
if(mysql_errno() == 1062) {
$msg = "We\'re sorry but this email already exists in our database. Please try again.";
echo "<script type='text/javascript'>alert('" . $msg . "')</script>";
}
//if email isn't already in the database enter it now
} else {
$msg = "Thank you for subscribing to the IHS newsletter. You can unsubscribe at any time by clicking a link in the email.";
echo "<script type='text/javascript'>alert('" . $msg . "')</script>";
}
}
} else {
//For debugging
$msg = "Form didn\'t submit.";
echo "<script type='text/javascript'>alert('" . $msg . "')</script>";
}
?>
And finally the JavaScript:HTML Code:<form id="form1" method="post" action="" onsubmit="sendForm(event)">
Newsletter
<input type="text" value="Enter your email address" name="em" /><input id="newsbtn" name="do_send" type="submit" value="" style="background:transparent url(images/arrow_go.gif) no-repeat; border:none; width:33px; margin:1px 0 0 3px; cursor:hand; cursor:pointer" />
</form>
I've searched all over to find a solution to my problem and haven't had any luck so any and all help will be greatly appreciated!Code:function sendForm(event) {
if(event.keyCode == 13) {
submitForm();
}
}
function submitForm() {
document.getElementById('form1').submit();
}
-Jeff
