Problem with php sending email form and alternative checkbox

Asked

Viewed 35 times

1

Hello I need help because I do not know a lot of php and I am not able to work the reply mail of my form in conditions. The checkbox only sends a reply of 1 of the selected articles and never sends all if more than 1 selected. I would like to know how I can put another type of checkbox instead of the box by type seen in the middle of the photo if that is possible. I’ll leave my code here. If you want to see with this with images Link

<?php
if(!isset($_POST['submit']))
{
	//This page should not be accessed directly. Need to submit the form.
	echo "error; you need to submit the form!";
}
$namefirst = $_POST['name_first'];
$contacto = $_POST['contacto'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
$info = $_POST['check1'];


$email_from = '[email protected]';//<== update the email address
$email_subject = "Nova Resposta ao Formulario";
$email_body = "Tem um novo pedido de informacoes da pessoa $namefirst com o contacto nº $contacto e com o email $visitor_email. \r\n".

    "Aqui esta os produtos:\r\n $info".

    "Aqui esta os comentarios adicionais:\r\n $message".
	
$to = "[email protected]";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thank-you.html');
@import url('https://fonts.googleapis.com/css?family=Montserrat');

ul {

list-style-type: none;

}
#field_1_5 {
    display: block; /* visibility: hidden works too */
}
#field_1_6 {
    display: block; /* visibility: hidden works too */
}
#field_1_7 {
    display: block; /* visibility: hidden works too */
}
#field_1_8 {
    display: block; /* visibility: hidden works too */
}
#field_1_9 {
    display: block; /* visibility: hidden works too */
}
#field_1_10 {
    display: block; /* visibility: hidden works too */
}
#field_1_11 {
    display: block; /* visibility: hidden works too */
}
#field_1_12 {
    display: block; /* visibility: hidden works too */
}
.center {
}
.list {
	list-style-type: none;
	margin-left: 150px;
	padding-left: 0px;
	display: inline-block;
	border: solid rgba(223,223,223,0.99);
	text-align: center;
	width: 305px;
	background-color: rgba(237,237,237,1.00);
}
.list1 button {
	bottom: 30px;
	padding: 5px 10px;
	border: 3px solid #FFFFFF;
	background-color: #FFFFFF;
	width: 170px;
	text-align: center;
}
#image1 {
	opacity: 1;
}

* {
	padding: 0;
	margin: 0;
}

.list img {
	border: solid rgba(223,223,223,0.99);
}
body img {
	float: right;
}


body {
	background-image: url(../img/../img/Background-1080p-50-opacidade--fundo-preto.jpg);
	background-size: 1920px 1080px;
	font-family: 'Montserrat', sans-serif;
	font-size: 1.5em;
	font-weight: bolder;
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	box-sizing: border-box;
	background-repeat: no-repeat;
	color: #FBFBFB;

}
.list1 {
	margin: 5px;
	padding-left: 5px;
	display: inline-block;
}
.list1 #field_4 {
	width: 500px;
}
.list label span {
	color: #000000;
}


h1 {
	font-size: 2em;
	font-weight: bolder;
	color: #FFFFFF;
	text-shadow: 0px 0px #FFFFFF;
}
<!DOCTYPE HTML>
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>Questionario | MONTRAS DIGITAIS</title>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
	<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
	<link rel="stylesheet" href="css/style.css">
	<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
</head>

<body class="">
<form id="" class="" method="post" action="email.php">
                   
                                            <div id="" class="">
                                               <br>
                                              <center><h1 >Selecione os artigos <br> e obtenha mais informações </h1></center>
                                                <br>
												
                           <ul id="" class="">
                                                   
                                                    <li class="list">
                                                        <label class="" for="field_1_5">
                                                            <span class="">
        <img src="img/Montras-Digitais.jpg" alt="Montras Digitais" width="300" title="Montras Digitais">
        </span>
                                                            <br>
                                                            <input type="checkbox" id="field_1_5" name="check1" value="Montras Digitais">
                                                            <span class="">Montras Digitais</span>
                                                        </label>
                                                    </li>
                                                    
                                                    <li class="list">
                                                        <label class="" for="field_1_6">
                                                            <span class="">
        <img src="img/MediaKiosk-II.jpg" alt="Quiosque Interactivo" width="300" title="Quiosque Interactivo">
        </span>
                                                            <br>
                                                            <input type="checkbox" id="field_1_6" name="check1" value="Quiosque Interactivo">
                                                            <span class="">Quiosque Interactivo</span>
                                                        </label>
                                                    </li>
                                                    <li class="list">
                                                        <label class="" for="field_1_7">
                                                            <span class="">
        <img src="img/Ementas-Digitais.jpg" alt="Ementas Digitais" width="300" title="Ementas Digitais" id="image1">
        </span>
                                                            <br>
                                                            <input type="checkbox" id="field_1_7" name="check1" value="Ementas Digitais">
                                                            <span class="">Ementas Digitais</span>
                                                        </label>
                                                    </li>
                                                    <li class="list">
                                                        <label class="" for="field_1_8">
                                                            <span class="">
        <img src="img/Impressora.jpg" alt="impressora de bebidas" width="300" title="impressora de bebidas">
        </span>
                                                            <br>
                                                            <input type="checkbox" id="field_1_8" name="check1" value="Impressora de bebedas">
                                                            <span class="">Impressora de Bebidas</span>
                                                        </label>
                                                    </li>
                                                    <br><br>
                                                    <li class="list">
                                                        <label class="" for="field_1_9">
                                                            <span class="">
        <img src="img/Ecra-Transparente.jpg" alt="Ecrans Transparentes" width="300" title="Ecrans Transparentes">
        </span>
                                                            <br>
                                                            <input type="checkbox" id="field_1_9" name="check1" value="Ecrans Transparentes">
                                                            <span class="">Ecrãns Transparentes</span>
                                                        </label>
                                                    </li>
                                                    <li class="list">
                                                        <label class="" for="field_1_10">
                                                            <span class="">
        <img src="img/Ecra-Especial.jpg" alt="Ecrans Especiais" width="300" title="Ecrans Especiais">
        </span>
                                                            <br>
                                                            <input type="checkbox" id="field_1_10" name="check1" value="Ecrans Especiais">
                                                            <span class="">Ecrãns Especiais</span>
                                                        </label>
                                                    </li>
                                                    <li class="list">
                                                        <label class="" for="field_1_11">
                                                            <span class="">
        <img src="img/MediaTotem.jpg" alt="Totem Digital" width="300" title="Totem Digital">
        </span>
                                                            <br>
                                                            <input type="checkbox" id="field_1_11" name="check1" value="Totem Digital">
                                                            <span class="">Totem Digital</span>
                                                        </label>
                                                    </li>
                                                    <li class="list">
                                                        <label class="" for="field_1_12">
                                                            <span class="">
        <img src="img/Cracha.jpg" alt="Crachas Digitais" width="300" title="Crachas Digitais">
        </span>
                                                            <br>
                                                            <input type="checkbox" id="field_1_12" name="check1" value="Crachas Digitais">
                                                            <span class="wpforms-image-choices-label">Crachás Digitais</span>
                                                        </label>
                                                    </li>
                                              </ul>
                                            </div>
	<br>
	<img src="img/Logo_MediaScreen_Branco.png" alt="Logo" width="300" height="321" class="" style="float: right; margin-right: 50px;" />
	
                              <center> <div id="" class="list1">
								  
                                                <label class="" for="field_1">Nome <span class="">* </span>
                                                </label>
                                                        <input name="name_first" type="text" required class="" id="field_1" size="30">
                                            </div></center>
							
  <center> <div id="" class="list1" >
	
      <label class="" for="field_2">Contacto Telefónico <span class="">*</span>
                                                </label>
                                                <input name="Contacto" type="text" required class="" id="field_2" max="12" min="9" size="30">
                                            </div></center>
								
  <center>  <div id="" class="list1" >
	 
      <label class="" for="field_3">Email <span class="">*</span>
      </label>
                                                <input name="email" type="email" required class="" id="field_3" size="30">
  </div></center>
							
                          <center>     <div id="" class="list1"> 
                            <label class="" for="field_4">Comentarios Adicionais <span class="">* </span></label>
                                                <textarea name="comentario" class="" id="field_4"></textarea>
                                            </div></center>

			
                                      <center><div class="list1">
										 
                                        <button type="submit" name="submit" class="" id="" value="submit">Submeter</button>
                                        </div> </center>
</form>
   
</body>

</html>

  • 1

    Take a look at this answer: https://answall.com/a/19021/5878

  • The keys of the Array that is sent by the post is the Tag name in the input, as all its inputs have the tag name='check1', PHP understands that there is only one position in the array with that name. Try changing the checkbox name tags to: check1, check2, check3...

1 answer

1


In the name attribute, at the end of your name, we put brackets.

When you place a bracketed "name" it is sent in vector or array form to the receiver.

Example:

if we submit the form with these elements,

<input type="checkbox" id="field_1_5" name="check1[]" value="Montras Digitais">
<input type="checkbox" id="field_1_6" name="check1[]" value="Quiosque Interactivo">
<input type="checkbox" id="field_1_7" name="check1[]" value="Ementas Digitais">
<input type="checkbox" id="field_1_8" name="check1[]" value="Impressora de bebedas">

and recover in PHP

$info = $_POST['check1'];

Print_r($info);

we will have the following result

    Array
    (
        [0] => Montras Digitais
        [1] => Quiosque Interactivo
        [2] => Ementas Digitais
        [3] => Impressora de bebedas
    )

Completion

  • Put clasps on name of checkboxes name="check1[]"
  • In PHP do so

    .....................
    .....................
    $info = $_POST['check1'];
    
    if(count($info) > 0){
       foreach($info as $item){
           $produtos .= $item .'<br>';
       }
    }
    
    
             ....................
             ....................
             $email_body = "Tem um novo pedido de informacoes da pessoa $namefirst com o contacto nº $contacto e com o email $visitor_email. \r\n".
    
             "Aqui esta os produtos:\r\n $produtos".
             ..................
             ..................
    

Browser other questions tagged

You are not signed in. Login or sign up in order to post.