我正在制作一个注册表单,其中php代码和html表单在同一页面中,我还有一个验证字段的java脚本,但如果验证完成,我需要通过调用发布数据php并在数据库中提交
if(isset($_POST['Name'])){
$order = "INSERT INTO `reg_form`(`Name`) VALUES ('$_POST[Name]')";
$result = mysql_query($order, $db2);
if (!$result) {
echo("<br>Input is failed");
} else {
echo 'Regestered successfully !!!';
}
}
<form name="register" id="register" class="b1 f_l" action="register.php" method="post">
<div class="input-group" style="padding-top: 20px;">
<span class="label_1">Name </span>
<input type="text" class="inp_t" placeholder="Your Name" name="Name" id="name" required>
</div>
<div style="padding-top: 20px;">
<button type="submit" class="btn btn-primary register" value="submit" >Submit</button>
</div>
</form>
<script language=JavaScript>
$('#register').validator()
.submit(function(e){
var form = $(this);
var valid = form.validator({messageClass: "alert alert-danger validate_error", position: 'center right'});
if (valid.data("validator").checkValidity()){
alert('not working');
}// VALID
e.preventDefault();
return false;
});// SUBMIT
</script>
答案 0 :(得分:1)
因此,如果表单未正确验证,您只想阻止默认操作:
if (valid.data("validator").checkValidity())
{
// Assuming this means Validation failed
alert('not working');
e.preventDefault();
return false;
}
else
{
// It's working, let the submission go through
return true;
}
现在,您需要为提交按钮指定一个名称,以便PHP了解它
<button type="submit" name='submit-btn' class="btn btn-primary register" value="submit" >Submit</button>
最后,您需要更新PHP脚本以处理表单提交。 将此脚本放在register.php文件的顶部,因为您的表单提交的位置,并且由于您通过POST提交,请检查帖子数组:
// Submit button value check here
if(isset($_POST['submit-btn'] && $_POST['submit-btn'] == "submit")
{
// Handle your form submission here
// Read the data from the $_POST array using the name you gave to the form elements
// Create a database connection and store your data in the database
}
答案 1 :(得分:0)
你应该写&#34;返回true&#34;在你的JS函数中提交表单的一些地方。如果验证成功,则从JS函数返回true
答案 2 :(得分:0)
只有在验证失败时才需要调用event.preventDefault()
。所以把它移到else子句。
<script language=JavaScript>
$('#register').validator()
.submit(function(e){
var form = $(this);
var valid = form.validator({messageClass: "alert alert-danger validate_error", position: 'center right'});
if (valid.data("validator").checkValidity()){
alert('not working');
return true;
}// VALID
else {
e.preventDefault();
return false;
}
});// SUBMIT
</script>
答案 3 :(得分:0)
使用onClick="validate()"
调用验证程序函数,验证成功后,使用document.getElementById("register").submit();