Error redirecting to failure page

I have a form that takes in a few fields.

Upon entering all fields, email will be sent to me with the details.

If any fields are missing, the form will redirect to fail.html file.

If I select at least one of the select values, I get redirected to the fail.html page.

However, the issue is that if I don't select any of the select multiple dropdown list, I get this error.

Warning: implode() [function.implode]: Invalid arguments passed in /home/mydomain/public_html/iComm/contact.php on line 6

Warning: Cannot modify header information - headers already sent by (output started at /home/mydomain/public_html/iComm/contact.php:6) in /home/mydomain/public_html/iComm/contact.php on line 19

This is the HTML.

 <div id="form">
            <form method="post" action="contact.php">
            <input type="text" name="fullName" placeholder="Full Name">
            <input type="email" name="email" placeholder="Email Address">
            <input type="text" name="number" placeholder="Phone">
            <div class="styled-select">
                <select multiple="multiple" name="choices[]">
                  <option style="color:grey";" value="Fri 8AM">Fri 20 Mar 8PM</option>
                  <option style="color:grey";" value="Sat 8AM">Sat 21 Mar 8AM</option>
                  <option style="color:grey";" value="Sat 2PM">Sat 21 Mar 2PM</option>
                  <option style="color:grey";" value="Sat 8PM">Sat 21 Mar 8PM</option>
                  <option style="color:grey";" value="Sun 8AM">Sun 22 Mar 8AM</option>
                </select>
            </div>
            <input type="submit" class="button" value="Sign Up">
            </form>

This is the PHP file.

    $fullName= $_POST['fullName'];
    $email=$_POST['email'];
    $tel=$_POST['number'];
    $choice = implode(',', $_POST['choices']);

    if(!empty($fullName) && !empty($email) && !empty($tel) && !empty($choice)){
        $to = 'm@gmail.com';
        $subject = '2015';
        $msg = "$fullName is keen to join. His/her email address is $email and number is $tel. He/She chose $choice.";

        mail($to, $subject, $msg, 'From: ' .$email);

        header("Location: success.html");
        }
    else{
        if(empty($fullName) || empty($email) || empty($tel) || empty($choice)){
            header("Location: fail.html");
        }
    }
?>

使用isset()

if(isset($_POST['choices'])) {  // This will check if this variable is set or not.
    $choice = implode(',', $_POST['choices']);  
}

Check that $_POST['choices'] isset and is array and not empty and if it is - implode . Second error will disappear too.


Use isset on the 'choices' post variable before trying to use it. You are also trying to pass header info when you have already output text to the browser (with the error probably).

链接地址: http://www.djcxy.com/p/69756.html

上一篇: 解析错误:语法错误,意外的T

下一篇: 重定向到失败页面时出错