[英]How to make indication of mandatory fields and form's validation accessible for screen reader users?
[英]How to make it mandatory to check the form's checkbox?
我有一個用 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>
首先在 HTML form
為 JS 驗證設置required="required"
字段( required="required"
):
<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'];
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.