我有这个查询
$tww_update_sql = "UPDATE `telesales_anc` SET
`Ime` = '".$_POST['Ime'];
我有一个带提交按钮的表单如下:
<form id="contact" method="post" action="ANC_pozivanje_test.php">
<div class="col-md-2 col-sm-12 col-xs-12 form-group">
<small>Ime</small>
<input id="contact_name" type="text" name="Ime" placeholder="Ime" class="form-control" value="<?php echo $values['Ime']?>">
</div>
<div class="form-group">
<div id="contact_submit" class="col-md-9 col-sm-9 col-xs-12">
<button type="submit" class="btn btn-success" value="Save">Save</button>
</div>
</div>
我用于检查字段的脚本填充如下:
<script>
$(document).ready(function() {
<!-- Real-time Validation -->
<!--Name can't be blank-->
$('#contact_name').on('input', function() {
var input=$(this);
var is_name=input.val();
if(is_name){input.removeClass("invalid").addClass("valid");}
else{input.removeClass("valid").addClass("invalid");}
});
<!-- After Form Submitted Validation-->
$("#contact_submit button").click(function(event){
var form_data=$("#contact_name").serializeArray();
var error_free=true;
for (var input in form_data){
var element=$("#contact_name"+form_data[input]['']);
var valid=element.hasClass("valid");
var error_element=$("span", element.parent());
if (!valid){
error_element.removeClass("error").addClass("error_show");
error_free=false;
}
else{
error_element.removeClass("error_show").addClass("error");
}
}
if (!error_free){
event.preventDefault();
}
else{
alert('Status nije popunjen');
}
});
});
</script>
问题是当提交按钮时(如果没有填充字段)我收到消息&#34; Status nije popunjen&#34;但是表格是提交的信息。
是否有必要使用脚本?
答案 0 :(得分:0)
似乎你检查error_free
是否为假,在这种情况下你会阻止提交,但是,如果它是真的(所以如果填充了字段),你只会发出警报。不应该event.preventDefault()
在其他地方吗?
答案 1 :(得分:0)
你不应该听button.click,而应该听form.submit。
<!-- After Form Submitted Validation-->
$("#contact").on('submit', function(event) {
方法event.preventDefault()
将阻止表单提交,并且应与alert()
if (!error_free) {
event.preventDefault();
alert('Status nije popunjen');
}
答案 2 :(得分:0)
您要做的就是验证客户端的数据,确保提供有效的名称。我将在这里重新创建。
您只需对其进行更改即可。但我相信这解决了你的需要。另请注意,您应验证服务器上的数据。不要只依赖客户端验证
'use strict';
$(document).ready(function() {
//bind click event on the button
var form = document.forms['frm-contact'];
$('#btn-contact').bind('click', function(event) {
event.preventDefault(); //this will prevent the form from getting submitted, so that we can validate the user name.
var field = form.elements['txt-name'],
name = field.value,
len = name.length,
error = '';
if (len === 0) {
error = 'Enter your name';
}
else if (len < 2) {
error = 'name should not be less than 2 characters';
}
else if (len > 36) {
error = 'name should not exceed 36 characters';
}
else if (/^[^a-z]+/i.test(name)) {
error = 'name must start with letters';
}
else if (!/^['.a-z\- ]+$/i.test(name)) {
error = 'invalid characters found'; // name should contain only alphabets, dash character, space character and dot character only
}
//done validating. show error now or submit the form.
if (error) {
alert(error);
$(field).addClass('error');
}
else {
$(field).removeClass('error');
form.submit();
}
});
});
.error {
background-color: red;
color: #fff;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form name="frm-contact" id="frm-contact" method="post" action="example.php">
<div class="form-group">
<label for="txt-name">Name:</label>
<input type="text" name="txt-name" id="txt-name" placeholder="enter your name" value="" />
</div>
<div class="text-center">
<button type="submit" class="btn btn-success" name="btn-contact" id="btn-contact" value="contact us">SEND</button>
</div>
</form>
&#13;
<?php
$cats = Product::find()->where('active = 1')->all();
$prArr = array();
if ($cats) {
foreach ($cats as $cat) {
$prArr[$cat->id] = $cat->title;
}
}
$selectedProducts = '';
if (isset($_POST['RelProducts']) and ! empty($_POST['RelProducts'])) {
$selectedProducts = array();
foreach ($_POST['RelProducts'] as $cat) {
$selectedProducts[$cat] = $cat;
}
}
?>
<?= Html::dropDownList('RelProducts[]', $selectedProducts, $prArr, ['multiple' => 'multiple', 'style' => 'width:300px;', 'rows' => 10, 'id' => 'relProductSelect']); ?>
&#13;