-1

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


编辑

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

联系人.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();

}
?>

表单.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

4 回答 4

2

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

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

联系方式.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'];
于 2012-11-19T08:00:14.530 回答
2

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

联系人.php

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

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

表单.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.phpURL 对数据进行解码和反序列化以供使用。

于 2012-11-19T08:20:10.513 回答
0

在 contact.php 中的表单中执行操作

<form action="form.php">
于 2012-11-19T08:04:50.340 回答
0

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

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

这可能会对您有所帮助。

于 2012-11-19T08:09:42.950 回答