我有一个用PHP配置的表单,我迷路了,我不知道如何强制性地检查我在条款和条件中添加的复选框。我已经输入了ID,但不知道如何将其放入PHP文件。我不知道是否应该向javascript文件添加内容。我向您展示了这三个文件,以便它们可以告诉我如何更正错误,而应该将其添加到PHP文件中。 我不知道如何以其他方式添加PHP代码和Javascript。
我给发送时的表格显示以下内容:
发送表单时出错。请稍后重试
发送表单时控制台中出现几个错误:
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
谢谢
// 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">×</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>
答案 0 :(得分:1)
首先,在HTML required="required"
上使您的字段必填(form
)来进行JS验证:
<input
type="checkbox"
name="aceptar_terminos"
id="aceptar_terminos"
value="aceptar_terminos"
required="required"
/>
当您使用serialize()
时,复选框值仅在选中时才发送,那么您也可以在PHP上进行验证。喜欢:
if(
!empty($_POST['form_name']) AND
!empty($_POST['form_email']) AND
!empty($_POST['aceptar_terminos']) AND
$_POST['aceptar_terminos'] == 'aceptar_terminos'
) { ... }
还更新您的PHP,使其仅需要文件,如果表单有效,则调用该类:
<?php
$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(
!empty($_POST['form_name']) AND
!empty($_POST['form_email']) AND
!empty($_POST['aceptar_terminos']) AND
$_POST['aceptar_terminos'] == 'aceptar_terminos'
) {
//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
$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'];
}
?>