So here is my
<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>
while my form textfields:
<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>
my payment/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";
}
}
So when the textfields input for name and email works (ajax return and jquery will alert anything as I stated inside 'success:' condition), somehow, when it goes to 'invalidphone' turn (i typed 'abc' as contact value), it won't alert, but instead it will go to the controller (payment/pay) plain php page and echo out 'invalidphone' Why?
While the rest of the responses (empty/empty_text/emailinvalid), jquery will alert it and stays at the same page.
What did I do wrong?