所以这是我的
<script type="text/javascript">
$(document).ready(function() {
$("#btn-pay").click(function() {
var no_dvd = $('input:checkbox').is(':checked');
if(!no_dvd) {
alert("Please select at least 1 DVD!");
return false;
} else {
var $this = $(this);
var $form = $this.parents('form');
var action = $("#paid").attr('action');
var sname=$("#sname").val();
var email=$("#email").val();
var contact = $("#contact").val();
var selectedItems = new Array();
$("input[name='vid[]']:checked").each(function() {selectedItems.push($(this).val());});
var values = new Array();
$.each($("input[name='vid[]']:checked"), function() {
values.push($(this).val());
});
var form_data = {
vid : selectedItems,
sname : sname,
email : email,
contact : contact,
is_ajax : 1
};
$.ajax({
type : "POST",
url : action,
data : form_data,
success : function(response) {
if (response == 'exceeds') {
alert("The total DVD you selected has exceeded the maximum DVD you can rent!");
return false;
}
else if (response == 'empty') {
alert("Please select at least 1 DVD!");
return false;
}
else if (response == 'empty_text') {
alert("All fields are required.Please fill in all the text!");
return false;
}
else if (response == 'emailinvalid') {
alert("Please input correct email!");
return false;
}
else if (response == 'phoneinvalid') {
alert("Please input correct contact number!");
return false;;
}
else if (response == 'success') {
alert("uhuuu!");
return false;
//window.parent.location="<?php echo base_url();?>payment/success";
}
} ,
error: function(){
alert("Error detected");
}
});
}
});
});
</script>
我的表单文本字段:
<form method='post' id='paid' action='<?php echo base_url();?>payment/pay'>
<input type='checkbox' class='vid' name='vid[]' value='<?php echo $vid;?>' title='Check to rent this'/>
<input required name="sname" id='sname' type="text" class="bok_form1" />
<input required name="email" id='email' type="email" class="bok_form1" />
<input required='required' size="8" name="contact" id='contact' type="tel" class="bok_form1" />
<input type='image' id='btn-pay' src="<?php echo base_url(); ?>assets/images/confirm.png" alt="Pay" border="0" />
</form>
我的付款/ pay.php
public function pay() {
$sname=mysql_real_escape_string($_POST['sname']);
$email=mysql_real_escape_string($_POST['email']);
$contact = mysql_real_escape_string($_POST['contact']);
$vid=($_POST['vid']);
$cart=$this->session->userdata('shoppingCart');
//print_r($vid);
$dvd=$this->session->userdata('total_dvd');
if(empty($sname) || empty($email) || empty($contact)) {
echo "empty_text";
} elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "emailinvalid";
}elseif(!ctype_digit($contact) || strlen($contact)>8 || strlen($contact)<8) {
echo "phoneinvalid";
}elseif(count($vid)>$dvd) {
echo "exceeds";
} else {
echo "success";
}
}
因此,当名称和电子邮件的文本字段输入工作时(ajax return和jquery将提醒任何内容,如我在'success:'条件中所述),不知何故,当它转到'invalidphone'时(我输入'abc'作为联系人)价值),它不会提醒,而是它将转到控制器(支付/支付)纯PHP页面并回显'无效电话' 为什么呢?
在其余的响应中(空/ empty_text / emailinvalid),jquery会提醒它并保持在同一页面。
我做错了什么?