HTML表格,验证和在同一页面预览

时间:2015-01-10 13:21:57

标签: javascript php html

我刚刚建立了一个简单的联系表单,我在同一个文件和同一页面中预览它时遇到了问题。

在此编码中,有3种类型的划分:

  1. HTML表单
  2. php validation
  3. 第二个变体页面是预览页面;完成验证后触发
  4. 文件名form.php。

    以下是代码:

       <?php
    // validate data
    $fnameErr = $lnameErr  = "";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {    
      if (empty($_POST["fname"])) {
          $fnameErr = "First name is required";
        }
    elseif (preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["fname"]) === 0) {
      $fnameErr = "First name must contain letters and spaces only";
    }
    else  {
    $fname = $_POST["fname"];
    }
        if (empty($_POST["lname"])) {
            $lnameErr = "Last name is required";
        }
    elseif (preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["lname"]) === 0) {
      $lnameErr = "Last name must contain letters and spaces only";
    }
        else {
            $lname = $_POST["lname"];
        }
    
       }
    
    
    
    ?>
       // 1st variant that will be form 
    <title>Untitled 1</title>
    <style type="text/css">
    .style1 {
        border-collapse: collapse;
        border: 1px solid #000000;
        background-color: #99FF99;
    }
    .style3 {
        text-align: center;
        font-size: large;
    }
    .style4 {
        color: #0000FF;
    }
    .style5 {
        text-align: center;
    }
    .style6 {
        background-color: #FEFECB;
    }
    .error {
        color: #FF0000;
    }
    </style>
    <script src="jquery-1.10.2.js"></script>
    </head>
    <body>
    <table style="width: 600px" cellspacing="0" cellpadding="25" align="center" class="style1">
        <tr>
        <form method="POST"
     action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">     <td>
            <div class="style3">
                :: <strong><span class="style4">STUDENT INFORMATION</span> </strong>
                ::</div>
            <table style="width: 100%">
                        </table>
            <table style="width: 100%" class="style6" cellpadding="5">
                <tr>
                    <td style="height: 30px; width: 106px">First Name&nbsp;</td>
                    <td style="width: 14px; height: 30px;" class="style5"><strong>:</strong></td>
                    <td style="width: 204px; height: 30px;"><input name="fname" type="text" value="<?php echo htmlspecialchars($fname);?>">&nbsp;</td>
                    <td style="height: 30px"><span class="error"><?php echo $fnameErr;?></span></td>
                </tr>
                <tr>
                    <td style="width: 106px">&nbsp;</td>
                    <td style="width: 14px" class="style5"><strong></strong></td>
                    <td style="width: 204px">&nbsp;</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td style="width: 106px">Last Name</td>
                    <td style="width: 14px" class="style5"><strong>:</strong></td>
                    <td style="width: 204px"><input name="lname" type="text" value="<?php echo htmlspecialchars($lname);?>"></td>
                    <td><span class="error"><?php echo $lnameErr;?></span></td>
                </tr>    
            </table>
            </td>
            </form>
        </tr>
    </table>
    </body>
    </html>
    
    
    //--------this code below i want to become as preview page , 2nd variant page that will be switch on after validation complete ---------
    <body>
    
    <table style="width: 600px" cellspacing="0" cellpadding="25" align="center" class="style1">
      <tr>
        <td>
          <div class="style3">
            :: <strong><span class="style4">STUDENT INFORMATION</span> </strong>
            ::</div>
          <table style="width: 100%">
          </table>
          <table style="width: 100%" class="style6" cellpadding="5">
            <tr>
              <td style="height: 27px; width: 106px">First Name&nbsp;</td>
              <td style="width: 14px; height: 27px;" class="style5"><strong>:</strong></td>
              <td style="width: 204px; height: 27px;"><?php echo $fname ?> </td>
              <td style="height: 27px"><span class="error"></span></td>
            </tr>
            <tr>
              <td style="width: 106px">&nbsp;</td>
              <td style="width: 14px" class="style5"><strong></strong></td>
              <td style="width: 204px">&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td style="width: 106px">Last Name</td>
              <td style="width: 14px" class="style5"><strong>:</strong></td>
              <td style="width: 204px"> <?php echo $lname ?></td>
              <td><span class="error"></span></td>
            </tr>
            <tr>
              <td style="width: 106px">&nbsp;</td>
              <td style="width: 14px" class="style5"><strong></strong></td>
              <td style="width: 204px">&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td style="width: 106px">&nbsp;</td>
              <td style="width: 14px" class="style5"><strong></strong></td>
              <td style="width: 204px">&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
    </body>
    </html>
    

    , 非常感谢任何反馈,谢谢!

1 个答案:

答案 0 :(得分:0)

首先改变你的:

<form method="POST"
 action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<form method="POST" action="">

第二点 - 您必须决定显示第二页变体的条件。

例如:

if (isset($POST['fname']) && $_POST['fname']==$fname 
    && isset($POST['lname']) && $_POST['lname']==$lname ) {$setPageVariant = 'noform';} else {$setPageVariant = 'form';};

在html部分开始之前(第27-28行)

第三。在你的html替换:

<form method="POST" action="">

<?php if ($setPageVar=='form') { ?> <form method="POST" action=""> <?php } ?>

和相同的条件,但与输入的其他。替换例如:

<input name="fname" type="text" value="<?php echo htmlspecialchars($fname);?>">

<?php if ($setPageVar=='form') { ?>
    <input name="fname" type="text" value="<?php echo htmlspecialchars($fname);?>">
<?php } else { echo $fname; } ?>

我希望你有一个想法,可以继续你的项目:-)祝你好运!