D
D
drtvader2015-10-05 14:14:22
PHP
drtvader, 2015-10-05 14:14:22

How to make multiple checkbox?

Good afternoon! How can I send multiple checkboxes to email at once? Thus, as I have done, an empty line comes.

<label><INPUT TYPE="checkbox" name="model1[]" VALUE="Комод">Комод</label> 
<label><INPUT TYPE="checkbox" name="model1[]" VALUE="Двойной пенал">Двойной пенал</label>
<label><INPUT TYPE="checkbox" name="model1[]" VALUE="Пенал">Пенал</label> 
<label><INPUT TYPE="checkbox" name="model1[]" VALUE="Стол">Стол</label>
<label><INPUT TYPE="checkbox" name="model1[]" VALUE="Тумба">Тумба</label> 
<label><INPUT TYPE="checkbox" name="model1[]" VALUE="Шкаф">Шкаф</label>

?<?php

/* config start */

$emailAddress = '';

/* config end */


require "phpmailer/class.phpmailer.php";

session_name("fancyform");
session_start();


foreach($_POST as $k=>$v)
{
  if(ini_get('magic_quotes_gpc'))
  $_POST[$k]=stripslashes($_POST[$k]);
  
  $_POST[$k]=htmlspecialchars(strip_tags($_POST[$k]));
}


$err = array();

if(!checkLen('name'))
  $err[]='The name field is too short or empty!';

if(!checkLen('email'))
  $err[]='The email field is too short or empty!';
  
else if(!checkEmail($_POST['email']))
  $err[]='Your email is not valid!';

if(!checkLen('subject'))
  $err[]='You have not selected a subject!';
  
if(!checkLen('message1'))
  $err[]='The message field is too short or empty!';
  
if(!checkLen('message'))
  $err[]='The message field is too short or empty!';




if(count($err))
{
  if($_POST['ajax'])
  {
    echo '-1';
  }

  else if($_SERVER['HTTP_REFERER'])
  {
    $_SESSION['errStr'] = implode('<br />',$err);
    $_SESSION['post']=$_POST;
    
    header('Location: '.$_SERVER['HTTP_REFERER']);
  }

  exit;
}


$msg=
'<strong>Имя:</strong>	'.$_POST['name'].'<br />
<strong>e-mail:</strong>	'.$_POST['email'].'<br />
<strong>phone:</strong>	'.$_POST['telephone'].'<br />
<strong>IP:</strong>	'.$_SERVER['REMOTE_ADDR'].'<br /><br />
<strong>Модель кроватки:</strong>	'.$_POST['model'].'<br />
<strong>Дополнительная мебель для кровати:</strong>	'.$_POST['model1[]'].'<br />
<strong>Постельное бельё:</strong>	'.$_POST['model2'].'<br />
<strong>Tема письма:</strong>	'.$_POST['subject'].'<br />
<strong>Адрес доставки:</strong><br /><br />

'.nl2br($_POST['message1']).'
<strong>Комментарий:</strong><br /><br />

'.nl2br($_POST['message']).'

';


$mail = new PHPMailer();
$mail->IsMail();

$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->AddAddress($emailAddress);
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->Subject = "Tема письма: ".$_POST['subject'];

$mail->MsgHTML($msg);

$mail->Send();


unset($_SESSION['post']);

if($_POST['ajax'])
{
  echo '1';
}
else
{
  $_SESSION['sent']=1;
  
  if($_SERVER['HTTP_REFERER'])
    header('Location: '.$_SERVER['HTTP_REFERER']);
  
  exit;
}

function checkLen($str,$len=2)
{
  return isset($_POST[$str]) && mb_strlen(strip_tags($_POST[$str]),"utf-8") > $len;
}

function checkEmail($str)
{
  return preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sowander, 2018-08-16
@Sowander

Additional bed furniture: '.$_POST['model1[]'].'
specifying name="model1[]" creates an array $_POST[model1] = [0=>value,1=> ...]
$_POST['model1[]'] does not exist and returns empty strings

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question