View Full Version : Form issue with IE7 and below

05-21-2011, 07:32 AM

I have a compatibility issue with my contact form with Internet Explorer 7 and below. When I fill out the form correctly and click submit, nothing happens. I have posted the html, js and php, though I took the js out and that didn't help, so at a guess I'd say it's an issue with the php. All newer browsers function perfectly using this code.

Here is my html of the form

<form name="contactform" action="contactengine.php" method="post" class="formcontainer" onSubmit="javascript:validate(); return false;">
<input type="text" name="name"/>
<input type="text" name="email"/>
<input type="text" name="subject" id="subject"/>
<textarea name="message"></textarea>

<button> Submit! </button>

The javascript:

function validate()
if (document.contactform.name.value =="")
alert("Name can't be left blank!");
return false;
if (document.contactform.email.value =="" || (document.contactform.email.value.search("@") == -1 ||( document.contactform.email.value.search("com") == -1 && document.contactform.email.value.search("net") == -1 && document.contactform.email.value.search("au") == -1 && document.contactform.email.value.search("org") == -1)))
alert("Please enter a valid email address!");
return false;

if (document.contactform.subject.value =="")
alert("Subject can't be left blank!");
return false;

if (document.contactform.message.value =="")
alert("Please enter a comment or question!");
return false;

And finally, the PHP:


$EmailFrom = "contact@website.com";
$EmailTo = "myemail@example.com";
$Subject = $_REQUEST['subject'];
$Name = Trim(stripslashes($_POST['name']));
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['message']));

// validation
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

if(isset($_POST["title"]) && strlen($_POST["title"])){
die("SPAM BOT");

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";

Thanks in advance for any comments/help.


05-21-2011, 08:58 AM
You forgot to tell us what problem you're having.

05-21-2011, 11:44 AM
As Fumigator says, what part of this isn't working? - The javascript, the php, what?

05-22-2011, 02:50 AM
Haha wow woops. The form doesn't work in IE 7 and below. I fill out the fields and click submit, and simply nothing happens.

I'm an alright coder, but I'm not good with compatibility issues with IE versions.

05-22-2011, 03:24 AM
onSubmit="javascript:validate(); return false;"

Returning false is going to cause the form to not submit. I thought this was true for all browsers but I'm wrong apparently... I'll test it real quick.

05-22-2011, 03:58 AM
onSubmit="javascript:validate(); return false;"

Returning false is going to cause the form to not submit. I thought this was true for all browsers but I'm wrong apparently... I'll test it real quick.

It only returns false if it doesn't validate through the js. I just re tested it just now without the validation in the form and it still doesn't work with IE 7 and below.

It works with "onClick="this.form.submit()"" in the button attributes, but then that overrides the validation and sends it anyway.

05-22-2011, 04:20 AM
Try using <input type="submit" value="submit" name="sub"> instead of <button>Submit!</button>. Make sure you don't name the <input> element "submit"-- that can mess things up.

05-22-2011, 04:33 AM
Fumigator - Thank you so much! That worked perfectly.

One last thing though. In IE 8 and below, the js code "alert("Please enter a valid email address!");" doesn't work. The form is prevented from being sent, but there is nothing telling the end user why it hasn't been sent.

Is there a more conventional way of "alerting" the user to the fields that need to be filled in.

05-23-2011, 05:04 PM
To clarify, the alert() command works fine on all IE browsers. If it's not working in your script, there's something else going on. But generally, you can manipulate the DOM to inject html into the page (document.write() or elementID.innerHTML() for example), or you can reload the page and include error messages within the DOM when the page is built (generated from your PHP script).

You really shouldn't be relying solely on client-side validation anyway; it's easy to turn off Javascript and then anything can get to your PHP script. If you aren't validating the data coming into your PHP script, your process may fail because the data is unreliable.

09-22-2011, 02:56 PM
It looks fine in all browsers except IE7 and I assume below. IE7 seems to be inserting a carriage return before the 'Contact' heading making the alignment off compared to the other pages. I guess it has something to do with the <form> tag. As this page is generated dynamically, I can't simply move the <form> tag further down the page.


09-22-2011, 03:09 PM
Validate the form with javascript is a good way, but like someone told before. Javascript can be shut off very easily.

That's why you also need to validate this in the PHP page.