繁体   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