在提交表单之前,我需要验证表单上的多个checkboxGroups,但我不能这样做。
下面是我的代码:
<div class="form-wrapper">
<form class="form1" action="update-2.php" id="profile" onClick="validate()" method="post">
<fieldset class="preguntas">
\-在这里我创建了7个具有不同id的复选框组,我将其中一个作为示例-\
<p>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Rimel" id="maquillaje">
Rimel</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Sombra" id="maquillaje">
Sombra</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Delineador" id="maquillaje">
Delineador</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Cuidado para sombracelhas" id="maquillaje">
Cuidado para sombracelhas</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Batom" id="maquillaje">
Batom</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Batom liquido" id="maquillaje">
Batom liquido</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Gloss" id="maquillaje">
Gloss</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Batom que aumenta labios" id="maquillaje">
Batom que aumenta làbios</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Delineador de labios" id="maquillaje">
Delineador de làbios</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Primer" id="maquillaje">
Primer</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Corretivo" id="maquillaje">
Corretivo</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Base hidratante" id="maquillaje">
Base hidratante</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Po corretivo" id="maquillaje">
Pó corretivo</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Blush" id="maquillaje">
Blush</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Iluminador" id="maquillaje">
Iluminador</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Bronzer e contorno" id="maquillaje">
Bronzer e contorno</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Pincéis e outros aplicadores de maquiagem" id="maquillaje">
Pincéis e outros aplicadores de maquiagem</label>
<br>
</p>
<p>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Rimel" id="maquillaje">
Rimel</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Sombra" id="maquillaje">
Sombra</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Delineador" id="maquillaje">
Delineador</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Cuidado para sombracelhas" id="maquillaje">
Cuidado para sombracelhas</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Batom" id="maquillaje">
Batom</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Batom liquido" id="maquillaje">
Batom liquido</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Gloss" id="maquillaje">
Gloss</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Batom que aumenta labios" id="maquillaje">
Batom que aumenta làbios</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Delineador de labios" id="maquillaje">
Delineador de làbios</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Primer" id="maquillaje">
Primer</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Corretivo" id="maquillaje">
Corretivo</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Base hidratante" id="maquillaje">
Base hidratante</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Po corretivo" id="maquillaje">
Pó corretivo</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Blush" id="maquillaje">
Blush</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Iluminador" id="maquillaje">
Iluminador</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Bronzer e contorno" id="maquillaje">
Bronzer e contorno</label>
<br>
<label>
<input type="checkbox" name="maquillaje[]" class="option" value="Pincéis e outros aplicadores de maquiagem" id="maquillaje">
Pincéis e outros aplicadores de maquiagem</label>
<br>
</p>
我尝试用一个php错误处理程序来验证它:
if(empty($_POST['maquillaje'])){
header("location: wahtever.php?check");
}else{ .... all the code to write on the database...}
但它无论如何都会刷新页面,
我还尝试使用javascript来验证它。
<script>
document.getElementById("profile").onsubmit = function () {
var send = document.getElementById("maquillaje"),
sendValue = send.value,
sendCheck = send.checked,
errors = "";
if (!sendCheck) {
errors += "Please answer question 5";
}
if (errors != "") {
alert(errors);
return false;
}
alert("Your details are being sent");
return true;
}
</script>
但我得到了相同的结果,它不会验证复选框,而是会刷新页面,即使复选框被选中...
我也尝试过Jquery,但我会一直收到警告文本框,而且它永远不会提交表单
我是一个新手,可能我犯了一个愚蠢的错误,请帮帮我!
发布于 2019-03-01 04:22:07
jQuery方法可能是您最简单的解决方案。
您需要添加的主要内容是"preventDefault“函数。这将阻止表单提交到后端。然后,您可以检查空白字段。如果没有错误,则通过添加this.submit()
提交表单
<script>
$("#profile").submit(function(e) { // add 'e'
e.preventDefault(); //add this line
var send = document.getElementById("maquillaje"),
sendValue = send.value,
sendCheck = send.checked,
errors = "";
if (!sendCheck) {
errors += "Please answer question 5";
}
if (errors != "") {
alert(errors);
return false;
} else {
alert("Your details are being sent");
this.submit(); //submit the form
return true;
}
});
</script>
https://stackoverflow.com/questions/54933497
复制相似问题