Jump to content

ajax validation not working


smk2002

Recommended Posts

<?php 
/*
Template Name: Warranty New
*/
//Include the database class
require("report/db.class1.php");
?>

<script type="text/javascript" src="/addjs/jquery_003.js"></script>
<script type="text/javascript" src="/addjs/chainedselects.js"></script>
<script type="text/javascript" src="/addjs/contentwarranty.js"></script>
<link rel="stylesheet" href="/Reports/warranty.css" type="text/css" media="screen" />
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="/Reports/jquery.validate.js"></script>
<script src="http://jquery.bassistance.de/validate/additional-methods.js"></script>

<script type="text/javascript">
$(function() {	
        var scntDiv = $('#p_scents');
        var i = $('#p_scents div').size() + 1;
        
        $('#addScnt').on('click', function() {
			
		if ($('input#wproserial[]'+(i-1)).val() != '') {					 
			$('<table class="multiple-rows" ><tr class="prod-row prod-row"><td><input type="text" name="wwarrantycard[]'+i+'" id="wwarrantycard'+i+'" class="textinput2 required" autocomplete="off" value=""/></td><td><select class="selectbox " id="maker'+i+'" name="makes[]'+i+'"></select></td><td><select name="types[]'+i+'" id="typer'+i+'" class="selectbox "></select></td><td><select name="models[]'+i+'" id="modelr'+i+'" class="selectbox " ></select></td><td><input name="wproserial[]'+i+'" id="wproserial'+i+'" type="text" value="" class="textinput3"></td><td width="20px" style="text-align:center"><a href="javascript:void(0);" id="remScnt'+i+'" class="remScnt">X</a></td></tr></table>').appendTo(scntDiv);			
		} else {
           alert('Enter your product first!');
            $('input#wproserial[]'+(i-1)).focus();
        }
		 
		var1="maker"+i;
		var1_1=document.getElementById(var1);
		var2="typer"+i;
		var2_1=document.getElementById(var2);
		var3="modelr"+i;
		var3_1=document.getElementById(var3);
		
		initListGroup('vehicles', var1_1, var2_1, var3_1, 'cs');
		document.getElementById(var1).selectedIndex="";
		document.getElementById(var2).selectedIndex=""
		document.getElementById(var3).selectedIndex=""
                i++;
                return false;
        });		
		$('#p_scents').on('click', '.remScnt', function() { 
		if (i > 1) {
			$(this).parents('table').remove();
			i--;
		}
		return false;
	});
});
</script> 
<script type="text/javascript">
$(function() {
	$( "#wdatepurchase" ).datepicker({ dateFormat: 'dd-mm-yy', 	 });
		
	$( "#wdob" ).datepicker({ dateFormat: 'dd-mm-yy', changeMonth: true, changeYear: true, yearRange: "1950:2003",
    	onSelect: function() {
       $( "#datepicker" ).trigger('blur');
    } });
});

$(document).ready(function() {

 	$.validator.addMethod("dateRule", function(value, element) {
        return value.match(/^(0[1-9]|[12][0-9]|3[01])[- //.](0[1-9]|1[012])[- //.](19|20)\d\d$/);
    	},
    	"Please enter a date in the format dd/mm/yyyy"
	);

	$('form.warregform').on('submit', function(event) {
		 
			// adding rules for inputs with class 'first'
            $('#wfirstname').each(function() {
                $(this).rules("add", 
                    {
                        required: true,						
						minlength: 3,
						messages: {
							required: "Type your first name!",
							minlength: jQuery.format("Please, at least {0} character are necessary!")
						}
                    })
            });  
			// adding rules for inputs with class 'last'
            $('#wlastname').each(function() {
                $(this).rules("add", 
                    {
                        required: true,						
						minlength: 3,
						messages: {
							required: "Type your last name!",
							minlength: jQuery.format("Please, at least {0} character are necessary!")
						}
                    })
            });  
			// adding rules for inputs with class 'last'
            $('#wyourphone').each(function() {
                $(this).rules("add", 
                    {
                        required: true,	
						number: true,					
						minlength: 9,
						messages: {
							required: "Type you contact number! (eg. 015679322)!",
							minlength: jQuery.format("Please, at least {0} number are necessary!")
						}
                    })
            });
			// adding rules for inputs with class 'last'
            $('#wdatepurchase').each(function() {
                $(this).rules("add", 
                    {
                        required: true,										
						minlength: 10,
						dateRule: true,
						messages: {
							required: "Insert your Purchase Date (dd-mm-yyyy)!",	
							date: "Please enter a valid date.",
							minlength: jQuery.format("Please, at least {0} are necessary!")
						}
                    })
            });
            // adding rules for inputs with class 'comment'
            $('input.textinput2').each(function() {
                $(this).rules("add", 
                    {
                        required: true,
						number: true,
						minlength: 3,
						messages: {
							required: "Type warranty cards number!",
							minlength: jQuery.format("Please, at least {0} number are necessary")
						}
                    })
            });  
			// adding rules for inputs with class 'comment'
            $('select.selectbox').each(function() {
                $(this).rules("add", 
                    {
                        required: true,
						messages: {
							required: "",
						}
                    })
            });  
			 
			// adding rules for inputs with class 'comment'
            $('input.textinput3').each(function() {
				
                $(this).rules("add", 
                    {
                        required: true,						
						minlength: 3,
						remote: "report/user_check.php",
							
						messages: {
							required: "Type Serial number!",							
							minlength: jQuery.format("Please, at least {0} character are necessary"),
							remote: jQuery.format("{0} is already taken"),
							// remote: 'This email address has already been used'
												
						}
                    })
            });     
			
			// adding rules for inputs with class 'last'
            $('#wshoplocation').each(function() {
                $(this).rules("add", 
                    {
                        required: true,
						messages: {
							required: "Choose Shop Location!",							
						}
                    })
            });     

            // test if form is valid 
            if($('form.warregform').validate().form()) {
                console.log("validates");
            } else {
                console.log("does not validate");
            }
        })

        // initialize the validator
        $('form.warregform').validate();	
	 
});


</script>

<?php
$hasErrors = false;
//If form was submitted
if (isset($_POST['btnSubmit'])) {

for ( $i=0;$i<count($_POST['wwarrantycard']);$i++) {
	{
		$wwarrantycard = $_POST['wwarrantycard'][$i];
		$makes = $_POST['makes'][$i];
		$types = $_POST['types'][$i];
		$models = $_POST['models'][$i];
		$wproserial = $_POST['wproserial'][$i];				
	}
	
}
if(empty($_POST['wfirstname'])){
	$nameErr = "With more than 3 letters!";	
	$hasErrors = true;	    
}if(empty($_POST['wlastname'])){
	$nameErr = "With more than 3 letters!";	
	$hasErrors = true;	    
}else if (empty($_POST['wyourphone'])){
	$phoneErr = "With more than 7 Numeric! (eg. 015679322)";	
	$hasErrors = true;	    
}else if(strlen($_POST['wyourphone']) <= 7){
	$phoneEErr = "With more than 7 Numeric! (eg. 015679322)";	
	$hasErrors = true;	    
}else if (empty($wwarrantycard)){
	$wcardErr = "Missing insert product after you click add more product. Please insert and try again!!!";	
	$hasErrors = true;	 	
}else if (empty($makes)){
	$wcardErr = "Missing insert product after you click add more product. Please insert and try again!!!";	
	$hasErrors = true;	 	
}else if (empty($types)){
	$wcardErr = "Missing insert product after you click add more product. Please insert and try again!!!";	
	$hasErrors = true;	 	
}else if (empty($wproserial)){
	$wcardErr = "Missing insert product after you click add more product. Please insert and try again!!!";	
	$hasErrors = true;	 	
}else if (empty($_POST['wshoplocation'])){
	$shopErr = "Please Select Shop Location.";	
	$hasErrors = true;	
} 
else{
	
	
	
	
	
	
}
}
?>

<?php if (!isset($_POST['btnSubmit']) || $hasErrors) { ?>

<body onLoad="initListGroup('vehicles', document.warrantyform.maker, document.warrantyform.typer, document.warrantyform.modelr, 'cs'); resetListGroup('vehicles');" >

 <div class="divider"></div>   
 <div class="clear"></div>
<form name="warrantyform" id="warrantyform" class="warregform" enctype="multipart/form-data" autocomplete="OFF" method="post">
	
    <div class="warrleft">
    	<label class="labeln"><strong>First Name: * </strong> <span id="nameInfo"></span></label>
        <label for="wfirstname" class="error"></label>
        <input class="textinput " type="text" name="wfirstname" id="wfirstname" size="30" maxlength="60" value="" autocomplete="off"/>
	</div>
    <div class="warrleft">
    	<label class="labeln"><strong>Last Name: * </strong> <span id="nameInfo"></span></label>
        <label for="wlastname" class="error"></label>
        <input class="textinput " type="text" name="wlastname" id="wlastname" size="30" maxlength="60" value="" autocomplete="off"/>
	</div>
    <div class="clear"></div> 
    <div class="warrleft">
    	<label class="labeln"><strong>Your Email: </strong> <span id="emailInfo">NOTE: Warranty confirmation will be sent to your email address</span></label>
        <input class="textinput " type="text" name="wyouremail" id="wyouremail" size="30" maxlength="60" value="" autocomplete="off"/>
    </div>
    <div class="warrleft">
    	<label class="labeln"><strong>Contact Number: * </strong> <span id="wcontactInfo"></span></label>
        <label for="wyourphone" class="error"></label>
        <input class="textinput " type="text" name="wyourphone" id="wyourphone" size="30" maxlength="60" value="" autocomplete="off"/>
	</div>
    <div class="clear"></div> 
    <div class="warrleft">
    	<label class="labeln"><strong>Gender: </strong></label><br/>
        <select name="wgender" id="wgender" >
        	<option value="Male">Male</option>
        	<option value="Female">Female</option>
        </select>
	</div>
    <div class="warrleft">
    	<label class="labeln"><strong>Date Of Birth: </strong></label><br/>
        <input class="textinput " type="text" name="wdob" id="wdob" size="30" maxlength="60" value="" autocomplete="off" readonly/>
   </div>   
   <div class="clear"></div> 
    <div class="warrleft">
    	<label class="labeln"><strong>Company Name: </strong></label><br/>
        <input type="text" maxlength="100" value="" name="wcompanyname" id="wcompanyname" class="textinput" autocomplete="off">
    </div>
    <div class="warrleft">
    	<label class="labeln"><strong>Job Title: </strong></label><br/>
        <input type="text" maxlength="100" value="" name="wjobtitle" id="wjobtitle" class="textinput" autocomplete="off">
    </div>
    <?php $todays_date = date("d-m-Y");  ?>
    <div class="warrleft">
    	<label class="labeln"><strong>Date of Purchase: * </strong></label> <label for="wdatepurchase" class="error"></label>
        <input type="text" maxlength="100" value="<?php echo $todays_date; ?>" name="wdatepurchase" id="wdatepurchase" class="textinput" autocomplete="off">
    </div>
    <div class="clear"></div> 
    <span id="wcardInfo"></span>
    <table class="multiple-rows" >
        <tr class="prod-row2">
        	<td><label><strong>Warranty Card No: *</strong> </label></td>
            <td><label><strong>Product Category *</strong></label></td>
            <td><label><strong>Sub Category *</strong></label></td>
            <td><label><strong>Model Name *</strong></label></td>
            <td><label><strong>Serial Number *</strong></label></td>
            <td width="20px"> </td>
        </tr>
        <tr class="prod-row prod-row">  
            <td><input type="text" name="wwarrantycard[]" id="wwarrantycard0" value="" class="textinput2 " autocomplete="off"/></td>
            <td><select name="makes[]" id="maker" class="selectbox "></select></td>
            <td><select name="types[]" id="typer" class="selectbox "></select></td>
            <td><select name="models[]" id="modelr" class="selectbox "></select></td>
            <td><input name="wproserial[]" id="wproserial0" type="text" value="" class="textinput3 " autocomplete="off"></td>
            <td> </td>        
        </tr>
        </table>
		<div id="p_scents"></div>        

        <div class="addproducts"><a href="javascript:void(0);" id="addScnt">Add another product</a></div>

        <div class="clear"></div> 
        
        <div class="warrleft">
    	<label class="labeln"><strong>Shop Location: * </strong><span id="wshopInfo"></span></label>
        <label for="wshoplocation" class="error"></label>
        <select name="wshoplocation" id="wshoplocation" >
        	<option value="">Select Shop</option>
        	<option value="I-Qlick">I-Qlick</option>
            <option value="CS_KTH">CS_KTH</option>
            <option value="CS_Ahhoo">CS_Ahhoo</option>
            <option value="CS_Sunsimexco">CS_Sunsimexco</option>
            <option value="Other">Other</option>
        </select>
   		</div>


 		<div class="warrleft">	
       <label class="labeln"><strong>Rate Shop Service:</strong></label>
            <select class="" name="wrate" id="wrate">
                <option value="Very Good">Very Good</option>
                <option value="Good">Good</option>
                <option value="Normal">Normal</option>
                <option value="Bad">Bad</option>
            </select> 
       </div>
       <div class="clear"></div> 
       <div class="warrleft">	
       		<input type="checkbox" name="wpromo" id="wpromo" value="Yes" /> <strong>I want to receive i-Qlick's promotions by email.</strong></p>
       </div>
       <div class="clear"></div> 
       <div class="warrleft">	
       	<input id="go" name="btnSubmit" type="submit" value="Submit" class="btn"/>
        <!--<input id="go" name="btnSubmit" type="submit" value="Submit" class="btn" onClick="targetGroup('textinput2');"/>-->
        <input type="reset" value="Reset" onClick="resetListGroup('vehicles')" >
       </div>   
</form>

<!--<script type="text/javascript" src="<php bloginfo( 'url' ); ?>/Reports/validation.js"></script>-->

<?php } ?>

above is My Html Files...

 

I can't check validation with existing database, this serial no. is already register or not. every input data is showing message "is already taken". so i can't submit. My Serial no input field is unlimited.

 

user_check.php

<?php
include("db.class1.php");

if (isset($_POST['wproserial'])) {
	//create instance of database class
	$db = new mysqldb();
	$db->select_db();
	
	for ( $i=0;$i<count($_POST['wproserial']);$i++) {
		{			
			$wproserial = $_POST['wproserial'][$i];				
		}	
	}
	//$query = "SELECT * FROM products WHERE SerailNo";
	//$query = "SELECT * FROM products WHERE SerailNo='".$wproserial."'";
	
	//$query = "SELECT * FROM products WHERE SerailNo='$wproserial'";
	$query = "SELECT * FROM `products` WHERE `SerailNo` = '".mysql_real_escape_string($wproserial)."'";


	//$query = "SELECT EXISTS (SELECT * FROM products WHERE SerailNo='".mysql_real_escape_string($wproserial)."')";
	if($db->num_rows($db->query($query)) < 1) {
		/*return true;
    }else {
        return false;
    }   */ 
	  	$valid = 'true';
	}else {
    	$valid = 'false';
	}
	echo json_encode($valid);
} 

?>
Link to comment
Share on other sites

Try creating some error tracing to find out where the problem is. Add this to the top of your user_check.php file just to make sure it isn't the problem:

die("true");

That will simply return the string "true" and prevent the rest of the script from executing. If you don't get the "is already taken" message after doing that, then the problem is somewhere in your logic, or possibly the output. I'm not too familiar with rules, but I think it just looks for a "true" or "false" string; your output (using json_encode) is include quotations in the output.

Link to comment
Share on other sites

I try

 

Try creating some error tracing to find out where the problem is. Add this to the top of your user_check.php file just to make sure it isn't the problem:

die("true");

That will simply return the string "true" and prevent the rest of the script from executing. If you don't get the "is already taken" message after doing that, then the problem is somewhere in your logic, or possibly the output. I'm not too familiar with rules, but I think it just looks for a "true" or "false" string; your output (using json_encode) is include quotations in the output.

 Yes,  after doing that, same like what you said..

Link to comment
Share on other sites

So, it validated after adding that line? If so, the next thing to check is whether or not it is the formatting of the output that is the culprit. Try removing the json_encode function and just output the validation variable:

echo $valid;

And don't forget to remove the die("true") line from the top first.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.