PHP表单复选框

时间:2014-04-09 15:28:37

标签: php checkbox

使用复选框联系表单,我真是太糟糕了。现在,如果我点击我的网页中发送的内容,我会将此结果发送到我的电子邮箱中:

  • 电子邮件:whatever@whatever.com
  • Nombre:无论
  • Le interessa :(这是 复选框和它的空虚价值。我想要做出用户选择的选择
  • Otros inteses:无论

谢谢,我会提供一些帮助

HTML->

<form name="form-contact-us" id="form-contact-us">
                            <div class="row">
                                <div class="col-lg-6">
                                    <div class="form-group">
                                        <label for="txtName">Name: </label>
                                        <input type="text" placeholder="Name"
                                            class="form-control required" id="txtName" name="txtName" />
                                    </div>
                                </div>
                                <div class="col-lg-6">
                                    <div class="form-group">
                                        <label for="txtEmail">Email:</label>
                                        <input type="text" placeholder="E-mail ID"
                                            class="form-control required email" id="txtEmail" name="txtEmail" />
                                    </div>
                                </div>
                            </div>
                            <label for="txtMessage">Información:</label><br>
                            ***<input type="checkbox" id="web" name="check[]" value="Creación de web"> 
                                    <label for="web">Creación de web</label><br>
                                    <input type="checkbox" id="red" name="check[]" value="Gestión de reputación Online / Redes Sociales"> 
                                    <label for="red">Gestión de reputación Online / Redes Sociales</label><br>
                                    <input type="checkbox" id="seo" name="check[]" value="Gestión Estratégica (SEO, e-mail marketing)"> 
                                    <label for="seo">Gestión Estratégica (SEO, e-mail marketing)</label><br><br>***
                            <div class="form-group">
                                <label for="txtMessage">Otros:</label>
                                <textarea placeholder="Message" class="form-control required"
                                    id="txtMessage" name="txtMessage" rows="10"></textarea>
                            </div>

                            <div class="form-group">
                                <label for="txtCaptcha"></label>
                                <input
                                    type="text" class="form-control required captcha"
                                    placeholder="Are you human?" id="txtCaptcha" name="txtCaptcha" />
                            </div>
                            <div class="form-group">
                                <input type="button" id="btn-Send" value="Send" class="btn btn-theme-inverse" />
                            </div>
                            <div class="col-lg-12" id="contact-form-message"></div>
                        </form>

PHP-&GT;

<?php

    $formType = $_POST["formType"];
    $MailTo = 'victor.fernandez.gayan@gmail.com';

    $txtName = $_POST["txtName"];
    $txtEmail = $_POST["txtEmail"];
    $txtRating = "";
    $txtWebsite = "";
    $txtMessage = $_POST["txtMessage"];
    $putavariable = $_POST["check"];

    if(!(empty($_POST['check']))){
    foreach($_POST['check'] as $value){
        $check_msg .= "Persona interesada en: $value\n";
    }
    }
    if ($formType == "comments") {
        $txtWebsite = $_POST["txtWebsite"];
    }

    if ($formType == "reviews") {
        $txtRating = $_POST["txtRating"];
    }

    $success = TRUE;    

    try{
        if(trim($txtEmail)==NULL){
            throw new Exception("Please enter your e-mail id.");
        }
        else
        {
            if (!preg_match("/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/", trim($txtEmail)))
            {
                throw new Exception("Please enter the valid e-mail.");
            }
        }

        if(trim($txtName)==NULL){
            throw new Exception("Please enter your name.");
        }

        if(trim($txtMessage)==NULL){
            throw new Exception("Please enter your message.");
        }

        if($formType == "contact") {   
            $MailSubject = "New Contact Mail";
            $MailBody = "<strong>Email: </strong> ".$txtEmail."<br />";
            $MailBody .= "<strong>Nombre: </strong> ".$txtName."<br />";
            $MailBody .= "<strong>Le interessa: </strong> ".$putavariable."<br />";
            $MailBody .= "<strong>Otros intereses: </strong> ".$txtMessage."<br />";

        }

        if($formType == "comments") {   
            $MailSubject = "New Comment Mail";
            $MailBody = "<strong>Email: </strong> ".$txtEmail."<br />";
            $MailBody .= "<strong>Name: </strong> ".$txtName."<br />";
            $MailBody .= "<strong>Website: </strong> ".$txtWebsite."<br />";
            $MailBody .= "<strong>Comment: </strong> ".$txtMessage."<br />";

        }

        if($formType == "reviews") {   
            $MailSubject = "New Review Mail";
            $MailBody = "<strong>Email: </strong> ".$txtEmail."<br />";
            $MailBody .= "<strong>Name: </strong> ".$txtName."<br />";
            $MailBody .= "<strong>Rating: </strong> ".$txtRating."<br />";
            $MailBody .= "<strong>Review: </strong> ".$txtMessage."<br />";
        }

        $MailFrom = $txtEmail;

        $MailHeaders = 'MIME-Version: 1.0' . "\r\n";
        $MailHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

        $MailHeaders .= "From: <".$txtEmail.">"."\r\n";


        $result =  mail($MailTo, $MailSubject, $MailBody, $MailHeaders);

        if(!$result){
            throw new Exception("Email no enviado:(");
        }

        $success = TRUE;
    }
    catch (Exception $e){
        header("Status: 400 Bad Request", TRUE, 400);
        echo($e->getMessage());
        $success = FALSE;
    }

    if ($success)
    {
        header("Status: 200 OK", TRUE, 200);    
        // add your custom success message here
        echo("EMAIL ENVIADO :)");
    }
    else
    {
        header("Status: 400 Bad Request", TRUE, 400);

}
?>

1 个答案:

答案 0 :(得分:0)

我修改了表单:

<label for="txtMessage">Información:</label><br>
<input type="checkbox" id="web" name="check[]" value="Creación de web"> 
<label for="web">Creación de web</label><br>
<input type="checkbox" id="red" name="check[]" value="Gestión de reputación Online / Redes Sociales"> 
<label for="red">Gestión de reputación Online / Redes Sociales</label><br>
<input type="checkbox" id="seo" name="check[]" value="Gestión Estratégica (SEO, e-mail marketing)"> 
<label for="seo">Gestión Estratégica (SEO, e-mail marketing)</label><br><br>
<div class="form-group">
<label for="txtMessage">Otros:</label>
<textarea placeholder="Message" class="form-control required" id="txtMessage" name="txtMessage" rows="10"></textarea>

和PHP:

$formType = $_POST["formType"];
$MailTo = 'victor.fernandez.gayan@gmail.com';

$txtName = $_POST["txtName"];
$txtEmail = $_POST["txtEmail"];
$txtRating = "";
$txtWebsite = "";
$txtMessage = $_POST["txtMessage"];
$putavariable = $_POST["check"];

var_dump($_POST);
if(!(empty($_POST['check']))){
foreach($_POST['check'] as $value){
    $check_msg .= "Persona interesada en: $value\n";
}
}

结果:

array(5) { ["formType"]=> string(7) "contact" ["txtName"]=> string(10) "Imchecking" ["txtEmail"]=> string(25) "Imchecking@Imchecking.com" ["txtMessage"]=> string(16) "hello Imchecking" ["txtMailTo"]=> string(0) "" } EMAIL SENDED :)

我接近但我不知道如何处理它。我在发送电子邮件之前检查了复选框,但没有任何反应。