我必须将复选框值发送到我的电子邮箱

时间:2017-07-29 18:50:35

标签: php forms email checkbox

我必须将复选框值发送到我的电子邮箱。现在,我真的,真的,真的非常新的php,所以我需要帮助。在复选框的页面上,我有一个基本表单 (您的姓名,姓氏,电话号码和地址)也使用PHPMailer发送到电子邮件。但现在我必须将该表单和复选框值作为一封邮件发送。我已经有复选框和基本表单的HTML代码,以及发送该表单的PHP代码,但我不知道如何将checbox值包含在该PHP代码中。有人可以帮忙吗?

我的HTML:

<?php
session_start();
require_once'helpers/security.php';
$errors=isset($_SESSION['errors'])?$_SESSION['errors']:[];
$fields=isset($_SESSION['fields'])?$_SESSION['fields']:[];
?>
<!doctype html>
<html>
<head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="css.css">
        <script src="jquery-3.1.1.min.js"></script>
        <title>Top Food-Smuti sam svoj smoothie</title>
    </head>
    <body>
    <header class="header">
<img src="tel.png" class="tel"><span class="broj">060 399 333 6</span> | <img src="email.png" class="mail"> <span class="email">topfoodbgd@gmail.com | <img src="lokacija.png" class="lokacija"> <span class="ulica">Knjeginje Zorke 2</span> | <img src="clock.png" class="sat"><span class="vreme"> 11:00-19:00 | </span> <span class="follow">Pratite nas:</span> <a href="https://www.facebook.com/Top-Food-407570532782168/"><img src="facebook3.png" class="fb"></a>  <a href="https://www.instagram.com/topfoodbgd/?hl=sr"><img src="instagram3.png" class="in"></a>  <a href="https://twitter.com/TopFoodBGD"><img src="twitter3.png" class="tw"></a>
</header>
    <div class="a">
     <div class="b"> 
         <ul>
             <li><a href="index.php">Dostava obroka</a></li>
             <li><a href="Dostava firme.html">Dostava obroka firmama</a></li>
             <li><a href="ketering za svecane prilike.html"> Ketering za svečane prilike</a></li>
             <li><a href="Kontakt.html">Kontakt</a></li>
         </ul>
     </div>
</div>
<input type="checkbox" name="Djumbir" value="Djumbir" class="sam">Đumbir<br/>
<input type="checkbox" name="Spanac" value="Spanac" class="sam">Spanać<br/>
<input type="checkbox" name="Brokoli" value="Brokoli" class="sam">Brokoli<br/>
<input type="checkbox" name="Kelj" value="Kelj" class="sam">Kelj<br/>
<input type="checkbox" name="Crveni pasulj" value="Crveni pasulj" class="sam">Crveni pasulj<br/>
<input type="checkbox" name="Cvekla" value="Cvekla" class="sam">Cvekla<br/>
<input type="checkbox" name="Celer" value="Celer" class="sam">Celer<br/>
<input type="checkbox" name="Sargarepa" value="Sargarepa" class="sam">Šargarepa<br/>
<input type="checkbox" name="Limeta" value="Limeta" class="sam">Limeta<br/>
<input type="checkbox" name="Krastavac" value="Krastavac" class="sam">Krastavac</br>
<input type="checkbox" name="Zelena jabuka" value="Zelena jabuka" class="smoothie">Zelena jabuka<br/>
<input type="checkbox" name="Avokado" value="Avokado" class="smoothie">Avokado<br/>
<input type="checkbox" name="Banana" value="Banana" class="sam">Banana<br/>
<input type="checkbox" name="Malina" value="Malina" class="sam">Malina<br/>
<input type="checkbox" name="Pomorandza" value="Pomorandza" class="sam">Pomorandža<br/>
<input type="checkbox" name="Mango" value="Mango" class="sam">Mango<br/>
<input type="checkbox" name="Aronija" value="Aronija" class="sam">Aronija<br/>
<input type="checkbox" name="Ananas" value="Ananas" class="sam">Ananas<br/>
<input type="checkbox" name="Badem" value="Badem" class="sam">Badem<br/>
<input type="checkbox" name="Orah" value="Orah" class="sam">Orah<br/>
<input type="checkbox" name="Ovsene pahuljice" value="Ovsene pahuljice" class="sam">Ovsene pahuljice<br/>
<input type="checkbox" name="Semenke bundeve" value="Semenke bundeve" class="sam">Semenke bundeve<br/>
<input type="checkbox" name="Chia seme" value="Chia seme" class="sam">Chia seme<br/>
<input type="checkbox" name="Lan" value="Lan" class="sam">Lan<br/>
<input type="checkbox" name="Cimet" value="Cimet" class="sam">Cimet<br/>
<input type="checkbox" name="Mirođija" value="Mirođija" class="sam">Mirođija<br/>
<input type="checkbox" name="Rukola" value="Rukola" class="sam">Rukola<br/>
<input type="checkbox" name="Nana" value="Nana" class="sam">Nana<br/>
<input type="checkbox" name="Cimet" value="Cimet" class="sam">Cimet<br/>
<input type="checkbox" name="Kokosovo ulje" value="Kokosovo ulje" class="sam">Kokosovo ulje<br/>
<input type="checkbox" name="Agava sirup" value="Agava sirup" class="sam">Agava sirup<br/>
        <div class="container">
            <div class="contact">
                <div class="panel">
                    <?php if(!empty($errors)):?>
                    <div class="panel">
                        <ul><li><?php echo implode('</li> <li>', $errors)?></li></ul>
                    </div>
                <?php endif; ?>
                </div>
                    </p>Zelite da porucite nesto od ovih proizvoda? Popunite formu i mi cemo pozvati vas!</p>
                <form action="contact4.php" method="post">
                    <div class="form-group">
                        <label for="name">Ime *</label>
                        <input type="text" name="name" autocomplete="off" class="form-control" placeholder="Upisite ime" <?php echo isset ($fields['name'])? 'value="'.e($fields['name']).'"':''?>>
                    </div>
                    <div class="form-group">
                        <label for="email">Prezime *</label>
                    <input type="text" name="email" autocomplete="off" class="form-control" placeholder="Upisite prezime"<?php echo isset ($fields['email'])? 'value="'.e($fields['email']).'"':''?>>
                    </div>
                     <div class="form-group">
                     <label for="email">Broj telefona *</label>
                    <input type="text" name="comment" autocomplete="off" class="form-control" placeholder="Upisite broj telefona"<?php echo isset ($fields['comment'])? 'value="'.e($fields['comment']).'"':''?>>
                    </div>
                    <div class="form-group" >
                        <label for="message">Adresa* </label>
                    <input type="text" name="message" autocomplete="off" class="form-control" placeholder="Upisite adresu"<?php echo isset ($fields['message'])? 'value="'.e($fields['message']).'"':''?>>
                     <br>

                        <input type="submit" value="Naruci" class="form-control" class="btn btn-primary">
                    </div>
                    <p class="muted">* oznacava obavezno polje</p>
                </form>
            </div>
        </div>
    </body>
</html>

<?php
unset($_SESSION['errors']);
unset($_SESSION['fields']);
?>

的 我的PHP:

<?php
session_start();
require_once 'libs/phpmailer/PHPMailerAutoload.php';

$errors =[];

if(isset($_POST['name'],$_POST['email'],$_POST['message'],$_POST['comment'])){
    $fields=[
        'ime'=>$_POST['name'],
        'prezime'=>$_POST['email'],
        'broj telefona'=>$_POST['comment'],
        'adresa'=>$_POST['message']
    ];
    foreach($fields as $field=>$data){
        if(empty($data)){
            $errors[]='Polje '.$field . ' je obavezno ';
        }
    }
    if(empty($errors)){
        $m=new PHPMailer;
        $m->isSMTP();
        $m->SMTPAuth=true;
        $m->Host='smtp.gmail.com';
        $m->Username='';//replace with your email address
        $m->Password='';//replace with your password
        $m->SMTPSecure='ssl';
        $m->Port=465;

        $m->isHTML();
        $m->Subject ='Porudzbina';
        $m->Body='Od: '.$fields['ime'].' '.$fields['prezime'].'<p>Adresa: '.$fields['adresa'].'</p> <p>Broj telefona: '.$fields['broj telefona'].'</p>';

        $m->FromName='Musterija';
        $m->AddAddress('pavles643@gmail.com','Some one');
        if ($m->send()) {
            header('Location:thanks.php');
            die();
        }else{
            $errors[]="Zao nam je sada ne mozemo da pošaljemo porudžbinu, molimo pokušajte kasnije.";
        }
    }
}else{
    $errors[]= 'Nešto je pošlo naopako.';
}
$_SESSION['errors']=$errors;
$_SESSION['fields']=$fields;
header ('Location:smuti sam svoj smoothie.php');

1 个答案:

答案 0 :(得分:0)

安全的方式:

if(isset($_POST['Brokoli'])) //input named Brokoli will be set if user ticks it
{
   $selected[] = "brokoli";
} 

OR

$included = array("Brokoli", "Blablabla");
$selected = array();
foreach($_POST as $key => $value)
{
   if(in_array($key, $included))
   {
        $selected[] = $key;
   }
}

(如果你也可以使用速记,但你说你是PHP的新手,所以我不想让它复杂化)

快捷方式:(可能不安全)

$excluded = array("name", "surname");
$selected = array();
foreach($_POST as $key => $value)
{
   if(!in_array($key, $excluded))
   {
        $selected[] = $key;
   }
}

这不安全,因为用户可以根据需要更改POST数据。

准备好阵列后,可以使用它来制作字符串

$str = implode($selected, " - "); //example output: Brokoli - Blablabla - Something

并实现邮件正文字符串

$mail->Body = "some text here.. Selected boxes: " . $str;