drs3925 2013-10-30 06:45
浏览 69
已采纳

如何在PHP中的页面之间导航

I need to verify the inputs in a Page given and then, if any errors are present, then I should be displaying the same page with the error messages. If the inputs are correct, then I should be displaying the next page. How would I achieve this ??

enter code here

<html>
<head></head>
<body>

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$valid= true;
if (empty($_POST["name"]))
  {$nameErr = "Name is required";}
else
  {$name = test_input($_POST["name"]); $valid=false;}

if (empty($_POST["email"]))
  {$emailErr = "Email is required";}
else
  {$email = test_input($_POST["email"]); $valid=false;}

if (empty($_POST["pwd"]))
  {$pwdErr = "Password is required";}
else
  {$pwd = test_input($_POST["pwd"]); $valid=false;}

if (empty($_POST["gender"]))
  {$genderErr = "Gender is required";}
else
 {$gender = test_input($_POST["gender"]); $valid=false;}

 }
if($valid){
header('Location: dbregister.php');
exit();
 }

function test_input($data)
 {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
 } 

 ?>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name : <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
Email: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
Password: <input type="password" name="pwd">
<span class="error">* <?php echo $pwdErr;?></span>
<br><br>
Gender: <input type="radio" name="sex" value="Male"> <input type="radio" name="sex"           value="Female">
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
Click here to Submit: <input type="submit" value="register">
</form>
</body>
</html>

I was able to validate the inputs and display the error messages, but unable to send another page which says, registration is successful 'dbregister.php'

Also I am getting undefined error for $valid in if($valid){
   header('Location: dbregister.php');
   exit();}
  • 写回答

1条回答 默认 最新

  • 普通网友 2013-10-30 09:52
    关注

    There are plenty of tutorials for error handling in forms e.g. http://www.w3schools.com/php/php_form_validation.asp.

    Sample file (named index.php):

    <?
        if(!isset($_POST["name"])){
            echo "<div>ERROR IN NAME FIELD</div>";
        }
    ?>
    <form action="index.php">
        <input type="text" id="name">
        <input type="submit" value="Submit">
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式