使用外部PHP处理器从表单中获取复选框值

时间:2012-07-13 10:31:12

标签: php forms checkbox

我有一个表单将其数据发送到process.php文件以便在该文件中处理我有以下代码:

<?php
    $name = $_GET['name'];
    $number = $_GET['number'];
    $email = $_GET['email'];
    $comment = $_GET['message'];
    $sales = $_POST['sales'];
    $lettings = $_POST['lettings'];

    $to = "me@me.com";

    $subject = "New website registration";

    $message = "Name: ".$name."\r\n";
    if(isset($number) && $number!='')
        $message.= "Number: ".$number."\r\n";
    if(isset($email) && $email!='')
        $message.= "Email: ".$email."\r\n";
    if(isset($comment) && $comment!='')
        $message.= "Comment: ".$comment."\r\n";
    if(isset($sales))
    {
        $message.= "I am Interested in Sales" . "\r\n";
    }else{
        //
    }
    if(isset($lettings))
    {
        $message.= "I am Interested in Lettings";
    }else{
        //
    }

    $headers = "From: ".$name." <".$email.">";

    $result = mail($to,$subject,$message,$headers);

    return $result;
?>

表单的HTML如下:

<form id="register_form" name="register" method="post" action="/content/contact/process.php">
    <input type="text" id="name" name="name" class="regform" onFocus="if(this.value=='Name'){this.value='';}" onBlur="if(this.value==''){this.value='Name';}" value="Name" />
    <input type="text" id="number" name="number" class="regform" onFocus="if(this.value=='Phone number'){this.value='';}" onBlur="if(this.value==''){this.value='Phone number';}" value="Phone number" />
    <input type="text" id="email" name="email" class="regform" onFocus="if(this.value=='Email address'){this.value='';}" onBlur="if(this.value==''){this.value='Email address';}" value="Email address" />
    <textarea id="message" name="message" class="regtext" onFocus="if(this.value=='Message'){this.value='';}" onBlur="if(this.value==''){this.value='Message';}">Message</textarea>
    <label for"sales">Sales<input type="checkbox" name="sales" value="1" /></label>
    <label for"sales">Lettings<input type="checkbox" name="lettings" value="1"/></label>
    <input id="submit" type="submit" value="Submit" class="regsender" />
    <div class="loading"></div>
</form>

选中复选框后,电子邮件中不会显示预期的消息。我在这里错过了一些东西。我已经尝试了几种不同的方法来实现这一点,但没有一种方法能够实现。任何帮助,将不胜感激。感谢。

2 个答案:

答案 0 :(得分:0)

您正在使用method = POST并使用$ _GET

检索值

答案 1 :(得分:0)

我已更新您的代码检查

<?php
    if(isset($_POST['submit'])){
        $name = $_POST['name'];
        $number = $_POST['number'];
        $email = $_POST['email'];
        $comment = $_POST['message'];
        if (isset($_POST['sales'])) 
        {
            $sales = $_POST['sales'];
        }
        else if(isset($_POST['lettings']))
        {
            $lettings =$_POST['lettings'];
        }

        $to = "me@me.com";

        $subject = "New website registration";

        $message = "Name: ".$name."\r\n";
        if(isset($number) && $number!='')
            $message.= "Number: ".$number."\r\n";
        if(isset($email) && $email!='')
            $message.= "Email: ".$email."\r\n";
        if(isset($comment) && $comment!='')
            $message.= "Comment: ".$comment."\r\n";

        if(isset($_POST['sales']))
        {
            $message.= "I am Interested in Sales" . "\r\n";
        }
        else if(isset($_POST['lettings']))
        {
            $message.= "I am Interested in Lettings";
        }
        echo $message;
        $headers = "From: ".$name." <".$email.">";

        $result = mail($to,$subject,$message,$headers);

        return $result;
      }
    ?>

<form id="register_form" name="register" method="post" action="testing_page.php">
    <input type="text" id="name" name="name" class="regform" onFocus="if(this.value=='Name'){this.value='';}" onBlur="if(this.value==''){this.value='Name';}" value="Name" />
    <input type="text" id="number" name="number" class="regform" onFocus="if(this.value=='Phone number'){this.value='';}" onBlur="if(this.value==''){this.value='Phone number';}" value="Phone number" />
    <input type="text" id="email" name="email" class="regform" onFocus="if(this.value=='Email address'){this.value='';}" onBlur="if(this.value==''){this.value='Email address';}" value="Email address" />
    <textarea id="message" name="message" class="regtext" onFocus="if(this.value=='Message'){this.value='';}" onBlur="if(this.value==''){this.value='Message';}">Message</textarea>
    <label for"sales">Sales<input type="checkbox" name="sales" value="1" /></label>
    <label for"sales">Lettings<input type="checkbox" name="lettings" value="1"/></label>
    <input id="submit" name="submit" type="submit" value="Submit" class="regsender" />
    <div class="loading"></div>
</form>