douhuan1648 2014-10-28 08:37
浏览 62
已采纳

表单正在提交到另一页面而没有在php中验证

This is the code i have used for online Registration
Validation is not working when i submit it another page,form is submitting to another page without any validation.i just need too know how to submit it with validation, can some one help out

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

$name = $cname = $mobileno = $email = $city= $postalcode = $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;
}
?>
<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="Submission.php">
    <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>
  • 写回答

4条回答 默认 最新

  • dtqpw68806 2014-10-28 09:18
    关注

    To me it seems you are new to PHP form handling. For beginners reinventing the wheel and is dangerous as it's so easy to open up security flaws in your script. Use a framework or CMS that can handle forms for you (e.g., WordPress and Contact Form 7 or just use something easy as Zebra Form or something complete like CakePHP, Laravel or Symfony.

    Just don't do everything yourself unless you really know what you are doing. It might take some time to get started, but it will definitely pay off in the long run.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码