2012-11-19 148 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解碼並反序列化數據以供使用。