...

View Full Version : Form issue with IE7 and below



Tobe_Sta
05-21-2011, 06:32 AM
Hey,

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;">
<label>Name:</label>
<input type="text" name="name"/>
<label>Email:</label>
<input type="text" name="email"/>
<label>Subject:</label>
<input type="text" name="subject" id="subject"/>
<label>Message:</label>
<textarea name="message"></textarea>

<button> Submit! </button>
</form>

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;
}
document.contactform.submit();
}

And finally, the PHP:


<?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
$validationOK=true;
if (!$validationOK) {
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
exit;
}

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\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>

Thanks in advance for any comments/help.

Toby.

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

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

Tobe_Sta
05-22-2011, 01: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.

Fumigator
05-22-2011, 02: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.

Tobe_Sta
05-22-2011, 02: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.

Fumigator
05-22-2011, 03: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.

Tobe_Sta
05-22-2011, 03: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.

Fumigator
05-23-2011, 04: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.

rizaj1t
09-22-2011, 01: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.

(http://eblogz.net)

Wanna
09-22-2011, 02: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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum