dtzd65908 2014-02-01 12:19
浏览 18
已采纳

PHP形式验证和发布工作但不在一起?

In order to work properly my form needs to be set up to first check the validation and then post the data if the validation passes. This is fine but I am not sure how to combine the validation code with the post code in the form action. Example if the action is: action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> the form validates correctly but does not send anywhere! If I change the form action to action="contact-engine.php"> then the form is posted but without validation! With this in mind I need to combine into the action both the validation and then (once passed validation) the contact-engine.php problem is I simply do not know how to do this? I really am a learner in php and this is complicated for me! Any help is really appreciated I have been working on this from now for a few days! (N.B. both pages are .php) Full code is as follows:

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Help!</title>

    <style type="text/css">
        .error {color: #FF0000;}
    </style>
</head>

<body>

<?php
    // define variables and set to empty values
    $nameErr = $emailErr = "";
    $name = $email = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{

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

//Email
   if (empty($_POST["email"]))
   {$emailErr = "Email is required";}
   else
   {
   $email = test_input($_POST["email"]);
   if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
   {
   $emailErr = "Invalid email format";
   }}
}
function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}
?>

<form method="post" id="form" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <p><span class="error">* required field.</span></p><br />

    <div class="contact-font" style=" margin-top: 20px;">
        <span class="asterix">* </span>Name:<br />
        <input type="text" name="name" class="border" size="25" value="<?php if(isset($_POST['name'])) {echo $_POST['name']; } ?>">
        <span class="error"><?php echo $nameErr;?></span>
    </div>

    <div class="contact-font" style=" margin-top: 20px;">
        <span class="asterix">* </span>Email: (please double check enty)<br />
        <input type="text" name="email" class="border" size="25" value="<?php if(isset($_POST['email'])) {echo $_POST['email']; } ?>"><span class="error">
        <?php echo $emailErr;?></span>
    </div>

    <div>
        <input type="submit" value="Send" id="submit">
    </div>
</form>

</body>
</html>


And below is the contact-engine code:
<html>
<head>
    <title>Contact Engine</title>
</head>

<body>

    <br />Name:<?php echo htmlspecialchars($_POST['name']); ?><br />
    <br />Email:<?php echo htmlspecialchars($_POST['email']); ?><br />

</body>
</html>
  • 写回答

1条回答 默认 最新

  • duan19850312 2014-02-01 12:50
    关注

    You can try this. This is a simple code below through which you can achieve this:

    //sample index.php file
    <?php
      include 'submitted.php';
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST">
          <input type="text" name="foo" />
          <input type="submit" value="submit">
    </form>
    </body>
    </html>
    

    Here, I am submitting the form to the same page using post method. But, I have included another file which is include 'submitted.php'.

    Here is the test script inside the submitted.php

    //sample submitted.php
    <?php
      if(isset($_POST['foo']))
     {
        if(strlen($_POST['foo']) < 5){
      echo "String length too small";
      }
      else
      {
       echo $_POST['foo'];
      }
     }
    ?>
    

    For test, it simply checks if the length is more than five or not. If it is not, the error message is displayed on the same page where your page is present.

    Test it yourself too.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题