簡體   English   中英

我的PHP表單有問題

[英]Problems with my PHP Form

我無法獲取復選框(收件人(管理員或內容編輯器))的值,要顯示該復選框,它會顯示“數組”或“ 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;
?>

您可以在main if語句外部進行重定向,只需將其移入內部即可。

另外,如果返回“ Array”,則無法訪問數組中的正確參數。

         for ($i=0; $i < count($_POST['recipient']);$i++) {
             $recipient = $_POST['recipient'][$i];
              }

您將需要更改您的功能

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM