简体   繁体   中英

Problems with my PHP Form

I can't get the value of the checkbox, the recipient(Administrator or Content Editor), to display it displays "Array" or "A". Also the page redirects automatically without the form first appearing. How do I rectify this? Here is my code:

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;
?>

you have the redirection outside the main if statement, just move it inside.

Also you are not accessing the right paramater in the array if you get "Array" back.

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

you will need to change your function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM