jeudi 21 février 2019

How to make it mandatory to check the form's checkbox?

I have a form configured with PHP and I'm lost, I do not know how to make it mandatory to check the checkbox that I put in the terms and conditions. I have put the ID but I do not know how to put it in the PHP file. I do not know if I should add something to the javascript file. I show you the three files so they can tell me how to correct the errors, rather I should add to the PHP file. I have added the PHP code along with the Javascript since I do not know how to add it in any other way.

The form when I give to send shows me the following:

There was an error sending the form. Please try again later

I have several errors in the console when sending the form:

POST https://agrochema.000webhostapp.com/includes/contact.php net::ERR_NAME_NOT_RESOLVED

    send @ jquery-1.12.4.js:17
    ajax @ jquery-1.12.4.js:17
    (anonymous) @ form-script.js:21
    dispatch @ jquery-1.12.4.js:16
    r.handle @ jquery-1.12.4.js:16


-- XHR failed loading: POST "https://agrochema.000webhostapp.com/includes/contact.php"

s

    end @ jquery-1.12.4.js:17
    ajax @ jquery-1.12.4.js:17
    (anonymous) @ form-script.js:21
    dispatch @ jquery-1.12.4.js:16
    r.handle @ jquery-1.12.4.js:16

Thank you

// Archivo PHP 

<?php

//require_once('phpmailer/class.phpmailer.php');
require_once('phpmailer/PHPMailerAutoload.php');

$mail = new PHPMailer();


//$mail->SMTPDebug = 3;                               // Enable verbose debug output
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'tls://smtp.gmail.com:587';             // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'example@gmail.com';                // SMTP username
$mail->Password = 'Password';                         // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$message = "";
$status = "false";

$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
$errorMessage = 'There was an error while submitting the form. Please try again later';

if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
    if( $_POST['form_name'] != '' AND $_POST['form_email'] != '' ) {

        $name = $_POST['form_name'];
        $email = $_POST['form_email'];
        $message = $_POST['form_message'];

        $botcheck = $_POST['form_botcheck'];

        $toemail = 'miguelestabaenlaparra@gmail.com';                // Your Email Address
        $toname = 'Unlock Design';                         // Your Name

        if( $botcheck == '' ) {

            $mail->SetFrom( $email , $name );
            $mail->AddReplyTo( $email , $name );
            $mail->AddAddress( $toemail , $toname );

            $name = isset($name) ? "Name: $name<br><br>" : '';
            $email = isset($email) ? "Email: $email<br><br>" : '';
            $message = isset($message) ? "Message: $message<br><br>" : '';

            $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Form was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

            $body = $name.' '.$email.' '.$message.' '.$referrer;

            $mail->MsgHTML( $body );
                        $mail->SMTPOptions = array(
                        'ssl' => array(
                                'verify_peer' => false,
                                'verify_peer_name' => false,
                                'allow_self_signed' => true
                        ));
            $sendEmail = $mail->Send();

            if( $sendEmail == true ):
                $responseArray = array('type' => 'success', 'message' => $okMessage);
            else:
                $responseArray = array('type' => 'danger', 'message' => $errorMessage);
            endif;
        } else {
            $responseArray = array('type' => 'danger', 'message' => $errorMessage);
        }
    } else {
        $responseArray = array('type' => 'danger', 'message' => $errorMessage);
    }
} else {
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}

//$status_array = array( 'message' => $message, 'status' => $status);
//echo json_encode($status_array);

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);
    
    header('Content-Type: application/json');
    
    echo $encoded;
}
else {
    echo $responseArray['message'];
}
?>


// ARCHIVO JAVASCRIPT 

// CONTACT FORM 2 SCRIPT
  // ===========================
  $(function () {
      $('#contact_form2').validator();
      $('#contact_form2').on('submit', function (e) {
          if (!e.isDefaultPrevented()) {
            var url = "includes/contact2.php";
            $.ajax({
                type: "POST",
                url: url,
                data: $(this).serialize(),
                success: function (data)
                {
                  var messageAlert = 'alert-' + data.type;
                  var messageText = data.message;

                  var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>' + messageText + '</div>';
                  if (messageAlert && messageText) {
                      $('#contact_form2').find('.messages').html(alertBox).fadeIn('slow');
                      $('#contact_form2')[0].reset();
                      setTimeout(function(){ $('.messages').fadeOut('slow') }, 6000);
                  }
                }
            });
            return false;
          }
      })
  });
<DOCTYPE html>
<body>
    <section class="ulockd-contact-page">
      <div class="container">
        
        <div class="row">
          <div class="col-md-12">
            <div class="ulockd-contact-form ulockd-style-two">
              <form id="contact_form" name="contact_form" class="contact-form" action="includes/contact.php" method="post"
                novalidate="novalidate">
                <div class="messages"></div>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="form_name" name="form_name" class="form-control ulockd-form-fg required" placeholder="Nombre"
                        required="required" data-error="Nombre requerido." type="text">
                      <div class="help-block with-errors"></div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="form_email" name="form_email" class="form-control ulockd-form-fg required email"
                        placeholder="Email" required="required" data-error="Email requerido." type="email">
                      <div class="help-block with-errors"></div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="form_phone" name="form_phone" class="form-control ulockd-form-fg required" placeholder="Teléfono"
                        required="required" data-error="Numero de telefono requerido." type="text">
                      <div class="help-block with-errors"></div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <input id="form_subject" name="form_subject" class="form-control ulockd-form-fg required"
                        placeholder="Tema" required="required" data-error="Tema requerido." type="text">
                      <div class="help-block with-errors"></div>
                    </div>
                  </div>
                  <div class="col-md-12">
                    <div class="form-group">
                      <textarea id="form_message" name="form_message" class="form-control ulockd-form-tb required" rows="8"
                        placeholder="Su mensaje" required="required" data-error="Mensaje requerido."></textarea>
                      <div class="help-block with-errors"></div>
                    </div>
                    <input type="checkbox" name="aceptar_terminos" id="aceptar_terminos" value="aceptar_terminos" /> He leído y acepto los <a href="terminos.html" target="_blank">terminos y condiciones</a>
                    <div class="form-group ulockd-contact-btn">
                      <input id="form_botcheck" name="form_botcheck" class="form-control" value="" type="hidden">
                      <button type="submit" class="btn btn-default btn-lg ulockd-btn-thm" data-loading-text="Getting Few Sec...">ENVIAR</button>
                    </div>
                  </div>
                </div>
              </form>
            </div>
          </div>
          
        </div>
      </div>
    </section>
</body>
</html>



Aucun commentaire:

Enregistrer un commentaire