2014-10-29 114 views
0

我有在线注册表单,我已经完成了验证,但如果我提交表单时没有任何数据,我会收到验证错误,并且空白电子邮件将被发送到我的邮箱。 任何一个帮助我转义验证并发送表单数据到电子邮件

这里是表单代码从if条件

<body> 

<?php 


// define variables and set to empty values 
$nameErr = $cnameErr = $mobilenoErr = $emailErr = $cityErr= $postalcodeErr = $addressErr = ""; 

$name = $cname = $mobileno = $email = $city= $postalcode = $address = ""; 

    $name=$_POST['name']; 
    $cname=$_POST['cname']; 
    $mobileno=$_POST['mobileno']; 
    $email=$_POST['email']; 
    $city=$_POST['city']; 
    $postalcode=$_POST['postalcode']; 
    $address=$_POST['address']; 

if ($_SERVER["REQUEST_METHOD"] == "POST") { 
    if (empty($_POST["name"])) { 
    $nameErr = "Name is required"; 
    } else { 
    $name = test_input($_POST["name"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) { 
     $nameErr = "Only letters and white space allowed"; 
    } 
    } 

    if (empty($_POST["cname"])) { 
    $cnameErr = "Company Name is required"; 
    } else { 
    $cname = test_input($_POST["cname"]); 
    } 

    if (empty($_POST["mobileno"])) { 
    $mobilenoErr = "Mobile Number is required"; 
    }else { 
    $mobileno = test_input($_POST["mobileno"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[789][0-9]{9}$/",$mobileno)) { 
     $mobilenoErr = "Not A Valid Number"; 
    } 
    } 

    if (empty($_POST["email"])) { 
    $emailErr = "Email is required"; 
    } else { 
    $email = test_input($_POST["email"]); 
    // check if e-mail address is well-formed 
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { 
     $emailErr = "Invalid email format"; 
    } 
    } 
    if (empty($_POST["city"])) { 
    $cityErr = "City is required"; 
    } else { 
    $city = test_input($_POST["city"]); 
    // check if name only contains letters and whitespace 
    if (!preg_match("/^[a-zA-Z ]*$/",$city)) { 
     $cityErr = "Only letters and white space allowed"; 
    } 
    } 
    if (empty($_POST["postalcode"])) { 
    $postalcodeErr = "Postal Code is required"; 
    } else { 
    $postalcode = test_input($_POST["city"]); 
    } 

    if (empty($_POST["address"])) { 
    $addressErr = "Address is required"; 
    } else { 
    $address = test_input($_POST["address"]); 
    } 
} 

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
if(!empty($_POST['name'])&& !empty($_POST['cname'])&&!empty($_POST['mobileno'])&& !empty($_POST['email'])&&!empty($_POST['city'])&&!empty($_POST['postalcode'])&&!empty($_POST['address'])){ 
    header('Location: Submission.php?$submit=1'); 
    exit();} 
$to = '[email protected]'; 

$subject = 'I need to show html'; 

$from ='[email protected]'; 
ini_set("sendmail_from", $from); 
$headers = "From: " .$from. "\r\n"; 
    $headers .= "Reply-To: ".$from. "\r\n"; 
    $headers .= "MIME-Version: 1.0\r\n"; 
    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
    $body ='User Name:'.$name.'<br>'; 
    $body .= 'Company Name:'.$cname.'<br>'; 
    $body .= 'Mobile Numbaer:'.$mobileno.'<br>'; 
    $body .= 'Email Id:'.$email.'<br>'; 
    $body .= 'City:'.$city.'<br>'; 
    $body .= 'Postalcode:'.$postalcode.'<br>'; 
    $body .= 'Address:'.$address.'<br>'; 



$headers = "From: " . $from . "\r\nReply-To: " . $from . ""; 
    $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) { 

    echo("<p>Sent</p>"); 
} else { 
    echo("<p>Error...</p>"); 
} 
?> 
<div class="gridContainer clearfix"> 
    <div id="div1" class="fluid"><!-- header ends here--> 
    <div id="header" class="fluid"> 
     <div class="fluid logo_container zeroMargin_tablet"> 
     <div class="fluid logo_mvc"></div> 
     <!-- logo_mvc ends here--> 
     <div class="fluid logo_gsm"></div> 
     <!-- logo_gsm ends here--> 
    </div> 
    <!-- logo_container ends here--> 
</div> 
    <div class="fluid imageslide zeroMargin_desktop"> 
     <div class="fluid imageslide_gs zeroMargin_desktop"></div> 
     <!-- imageslide_gs ends here--> 
     <div class="fluid imageslide_content"> 
     <h1>IP Product Introduction and VoIP PBX 
Appliance Training Day @ Toronto</h1> 
     </div><!-- imageslide_content ends here--> 
     <div class="fluid imageslide_product"></div> 
     <!-- imageslide_product ends here--> 
    </div><!-- imageslide ends here--> 
    <div class="fluid content"> 
    <div class="fluid content_det"> 
    <h3>Event information</h3> 
    <p>Please join us at the Fairfield Inn & Suites Toronto Airport where Grandstream will offer four different sessions during the day. </p> 
    <h3>Introduction to Grandstream IP products</h3> 
    <p><b>8:45am - 10:15am</b><br/> 
Introduction to Grandstream, and basic information on Grandstream products including ATAs, gateways, routers and telephones. </p> 
<h3>Introduction to IP cameras IP and Surveillance products</h3> 
    <p><b>12:45pm - 2:15pm</b><br/> 
Basic information on IP cameras and surveillance products, and the introduction of the brand new GVR3550 Network Video Recorder. </p> 
<h3>Advanced Technical Training for UCM VoIP PBX's</h3> 
    <p><b>2:30pm - 4:30pm</b><br/> 
This session will focus on the advanced features of the UCM series, including the new features of the upcoming software and the brand new UCM6510 VoIP PBX for T1 networks. </p> 
    </div><!-- content_det ends here--><div class="fluid contet_form"> 

    <h2>Register Now</h2> 
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
    <div class="fluid div_form"><label><b>First name *:</b></label> 
    <input type="text" size="20px" name="name" placeholder="Enter Your Name Here" value="<?php echo $name; ?>"/><span class="error"><?php echo $nameErr;?></span> 

    </div> 


    <div class="fluid div_form"><label><b>Company Name *:</b></label> 
    <input type="text" size="20px" name="cname" placeholder="Enter Your Company Name Here" value="<?php echo $cname; ?>"/><span class="error"><?php echo $cnameErr;?></span></div> 

    <div class="fluid div_form"><label><b>Mobile Number *:</b></label> 
    <input type="text" size="20px" name="mobileno" placeholder="Enter Your Mobile Number Here" value="<?php echo $mobileno; ?>"/><span class="error"><?php echo $mobilenoErr?></span> 
    </div> 

    <div class="fluid div_form"><label><b>Email Id *:</b></label> 
    <input type="email" size="20px" name="email" placeholder="Enter Your Email Id Here" value="<?php echo $email; ?>"/><span class="error"><?php echo $emailErr?></span></div> 


    <div class="fluid div_form"><label><b>City *:</b></label> 
    <input type="text" size="20px" name="city" placeholder="Enter Your City Name Here" value="<?php echo $city;?>"/><span class="error"><?php echo $cityErr?></span></div> 

    <div class="fluid div_form"><label><b>Postal Code *:</b></label> 
    <input type="text" size="20px" name="postalcode" placeholder="Enter Postal Code Here" value="<?php echo $postalcode; ?>"/><span class="error"><?php echo $postalcodeErr?></span> 
    </div> 

    <div class="fluid div_form"><label><b>Address *:</b></label> 
    <input type="text" size="20px" name="address" placeholder="Enter Address Here" value="<?php echo $address; ?>"/><span class="error"><?php echo $addressErr?></span></div> 


<button name="submit" >Submit</button> 
    </form> 

    </div><!-- contet_form ends here--> 
    </div><!-- content ends here--> 
    </div><!-- div1 ends here--> 
</div> 
</body> 
+0

喜@sandeep又新的问题? – Choco 2014-10-29 10:22:13

+0

@是的,任何建议或回答 – 2014-10-29 10:24:58

+0

是的,当然我会提供现在检查它 – Choco 2014-10-29 10:25:30

回答

0

如果

if(!empty($name)&& !empty($cname)&&!empty($mobileno)&& !empty($email)&&!empty($city)&&!empty($postalcode)&&!empty($address)){ 

删除此,并把

if(!empty($_POST['name'])&& !empty($_POST['cname'])&&!empty($_POST['mobileno'])&& !empty($_POST['email'])&&!empty($_POST['city'])&&!empty($_POST['postalcode'])&&!empty($_POST['address'])){ 


      $to = '[email protected]'; 

      $subject = 'I need to show html'; 

      $from ='[email protected]'; 
      ini_set("sendmail_from", $from); 
       $name=$_POST['name']; 
       $cname=$_POST['cname']; 
       $mobileno=$_POST['mobileno']; 
       $email=$_POST['email']; 
       $city=$_POST['city']; 
       $postalcode=$_POST['postalcode']; 
       $address=$_POST['address']; 
      $headers = "From: " .$from. "\r\n"; 
       $headers .= "Reply-To: ".$from. "\r\n"; 
       $headers .= "MIME-Version: 1.0\r\n"; 
       $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; 
       $body ='User Name:'.$name.'<br>'; 
       $body .= 'Company Name:'.$cname.'<br>'; 
       $body .= 'Mobile Numbaer:'.$mobileno.'<br>'; 
       $body .= 'Email Id:'.$email.'<br>'; 
       $body .= 'City:'.$city.'<br>'; 
       $body .= 'Postalcode:'.$postalcode.'<br>'; 
       $body .= 'Address:'.$address.'<br>'; 
      $headers = "From: " . $from . "\r\nReply-To: " . $from . ""; 
       $headers .= "Content-type: text/html\r\n"; 
if (mail($to, $subject, $body, $headers)) { 

    echo("<p>Sent</p>"); 
} else { 
    echo("<p>Error...</p>"); 
} 

    header('Location: Submission.php?submit=1'); 
} 
+0

它力气解决我的问题 – 2014-10-29 10:40:13

+0

在这里我正在编辑我的问题更新代码 – 2014-10-29 10:41:00

+0

@sandeepSK尝试使用此编辑的代码 – Choco 2014-10-29 10:43:01

0

移动下面的变量,并把它们上面,如果条件

$name=$_POST['name']; 
$cname=$_POST['cname']; 
$mobileno=$_POST['mobileno']; 
$email=$_POST['email']; 
$city=$_POST['city']; 
$postalcode=$_POST['postalcode']; 
$address=$_POST['address']; 

并更换if(isset($name)){

if(!empty($name)){

+0

如果我这样做,我根本没有得到任何邮件 – 2014-10-29 10:27:44

+0

分享您更新的代码? – 2014-10-29 10:36:35

相关问题