2012-11-19 149 views
-1

表单输入不显示在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.-_][email protected][\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; 
?> 
+1

哪里是表格? –

+0

使用print_r($ _ POST); //知道值是否发布 –

回答

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']; 
+0

如何重新发送头文件? – user1811235

+0

您可以使用Jacob所述的方法,也可以在GET参数中附加信息。我编辑了我的评论以反映第二种方法。 –

+0

谢谢。我认为这种方法是我最喜欢的方法。 – user1811235

0

给行动的形式contact.php

<form action="form.php"> 
0

如果您想要显示表单元素,那么你必须使用这种方法。

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

这可能对你有所帮助。

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"]; 

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