-1

I am trying to validate an email field and if valid submit my form. But the form is giving me an error when i submit it to the server. If i submit it with a submit button it works fine.

<script>

function ValidateEmail(inputText)  
{  
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
if(inputText.value.match(mailformat))  
{  
document.form1.submit();
}  
else  
{  
alert("You have entered an invalid email address!");  
document.form1.email.focus();  
return false;  
}  
}  

</script>

<form action="mailer.php" id="form1"   name="form1" method="POST">



  <input type="text"  size="19" name="name"/>
<input type="text"  size="19" name="phone"/>
<input type="text"  size="19" name="email"/>


    <a  onclick="ValidateEmail(document.form1.email)" href="#">Submit</a>




</p>
</form>


<?php
if(isset($_POST['submit'])) {

$to = "marshal@smedia.ca"; 
$to2 = $_POST['email'];
$subject = "Lead Gen";
$subject2 = "Coupon2";
$first_field = $_POST['name'];
$email_field = $_POST['email'];
$phone_field = $_POST['phone'];
$headers .= "Content-type: text/html\r\n"; 
$headers .= 'From: Nurray GM <welcome@mycompany.com>' . "\r\n";


$body = "

From: $first_field \n   

E-Mail: $email_field\n 

Phone Number: $phone_field\n ";


$body2 = "


<html>
 <body bgcolor='#DCEEFC'>
    <p>Hello  $first_field,</p> 

    <p>Thank you for applying for pre approval. balbalblah.</p> 

  <img src='www.smedia.ca/clients/leadgen/murray/coupon.jpg' width='500px' height='200px' />


  </body>
</html> 

";


mail($to, $subject, $body, $headers);

mail($to2, $subject2, $body2, $headers );

header("Location: http://www.smedia.ca/clients/leadgen/murray/redirect.html");  
} else {
echo "Sorry - something seems to be wrong. Your message was not sent";
}


?>
4

2 回答 2

0

You shouldn't do it that way. You should attach the validate function to onsubmit event of the form, and decline submitting if there are issues (by returning false or preventing the default behaviour).

<form action="mailer.php" id="form1" onsubmit="return mysubmitcheck()"
      name="form1" method="POST">

.

function mysubmitcheck() { if (...) return true; else return false; }

That's the way it was done with pure javascript, and it still works. You should, instead of a link, have a regular submit button for submitting forms.

Nowadays people do the same with jquery (like explained here), but if you're not using jquery anywhere else it doesn't add special value here.

Attaching to events such as onsubmit can also be done so that it wouldn't pollute HTML, like

window.onload = function() {
  // find the form
  form.onsubmit = mysubmitcheck;
}
于 2013-01-25T18:10:31.217 回答
0

如果您坚持在链接上使用内联脚本,请更改

<a  onclick="ValidateEmail(document.form1.email)" href="#">Submit</a>

<a onclick="ValidateEmail(document.form1.email); return false" href="#">Submit</a>

避免重新加载页面

这是我会怎么做

var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;  
window.onload=function() {
  document.getElementById("form1").onsubmit=function() {
    if(!this.email.value.match(mailformat)) {  
     alert("You have entered an invalid email address!");  
     this.email.focus();  
     return false;  
    }
    return true;  
  } 
}

并使用 an<input type="submit" />而不是链接。如果您希望它看起来像一个链接,我们可以将按钮样式设置为

于 2013-01-25T18:14:29.117 回答