复选框值未显示

时间:2012-11-19 07:56:35

标签: php redirect header

表单输入未显示在form.php页面上,否定了我的表单验证。该错误表示form.php上所有变量的未定义变量。请告诉我在代码中编辑的内容,使其在form.php上显示表单输入。当我在同一页面上使用它时,它可以工作,但我希望它显示在另一个页面上。


修改

感谢到目前为止,但我无法获取复选框,收件人(管理员或内容编辑器)的值,以显示它显示“数组”或“A”。

contact.php

   <?php
    $errnam = "";
    $errmail = "";
    $errsub = "";
    $errrec = "";
    $hasErrors = false;

    if(isset ($_POST['submitted'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $subject = $_POST['subject'];
    $recipient = $_POST['recipient'];
    $message = $_POST['message'];



            if(preg_match("/^[\w\-'\s]/", $_POST['name'])){
               $name = $_POST['name'];   
            }  
            else{  
                 $errnam ='<strong>Please enter a name.</strong>';
                 $hasErrors = true;  
            }  

            if (preg_match("/^[\w.-_]+@[\w.-]+[A-Za-z]{2,6}$/i", $email)){
              $email = $_POST['email'];

            }  
            else{  
                $errmail = '<strong>Please enter a valid email.</strong>';
                $hasErrors = true;  
            } 


            if(preg_match("/^[\w\-'\s]/", $_POST['subject'])){
                $subject = $_POST['subject'];

            }  
            else{  
                 $errsub = "<strong>Please enter a subject.</strong>";
                 $hasErrors = true; 
            }

            if (!empty($_POST['recipient'])) {  
             for ($i=0; $i < count($_POST['recipient']);$i++) {
                 $recipient = $_POST['recipient'];
                  }
           }else{
            $errrec = "<strong>Please select a recipient</strong>";
            $hasErrors = true; 
          } 
                $message = $_POST['message'];
    }

    if ($hasErrors){
        echo "<strong>Error! Please fix the errors as stated.</strong>";
    }else{
        header("Location: form.php?name=".$name."&email=".$email."&subject=".$subject. "&recipient=".$recipient. "&message=".$message);

    exit();

}
?>

form.php的

<?php
$name = $_GET['name'];
$email = $_GET['email'];
$subject = $_GET['subject'];
$recipient = $_GET['recipient'];
$message = $_GET['message'];

echo "<h2>Thank You</h2>";
echo "<p>Thank you for your submission. Here is a copy of the details that you have sent.</p>"; 
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong>" .$recipient. "<br />";  
echo "<strong>Message:</strong> <br /> " .$message;
?>

4 个答案:

答案 0 :(得分:2)

问题是当您header("Location:")form.php时,所有POST值都会丢失。您必须使用标题重新发送它们,或将它们修改为GET并再次检索它们。将它们(contact.php和form.php)放在一个页面中应该更有效。这样,表单数据只需要发送一次。

您可以将POST值作为GET发送到form.php,就像这样。

contact.php:

header("Location: form.php?name=".$name."&email=".$email."&subject=".$subject."&message=".$message);

form.php(检索值):

$name = $_GET['name'];
$email = $_GET['email'];
$message = $_GET['message'];
$subject = $_GET['subject'];

答案 1 :(得分:2)

如果您想将数据从contact.php传输到form.php,您应该使用以下内容:

<强> contact.php

$data = urlencode(
        serialize(
             array(
                   "name" => $name, 
                   "email" => $email,
                   "subject" => $subject,
                   "message" => $message)
                  ));

header('Location: form.php?data=' . $data);

<强> form.php的

$data = unserialize(urldecode($_GET['data']));

$name = $data["name"];
$email = $data["email"];
$subject = $data["subject"];
$message = $data["message"];

这将序列化来自contact.php的数据数组,然后URL对其进行编码并将其作为GET变量发送到form.php。之后,form.php URL对数据进行解码和反序列化以供使用。

答案 2 :(得分:0)

在contact.php中以表格形式采取行动

<form action="form.php">

答案 3 :(得分:0)

如果要显示表单元素,则必须使用此方法。

<form method="POST" action="contact.php">
Email<input type="text" name="email">
.......
.......
.......
// All elements
</form>

这可能会对你有帮助。