[js+php] elementary - ajax json, php call

Costas

Administrator
Staff member
indicators - http://zanstra.home.xs4all.nl/picks/progress.html

I have an inputbox when it length is > 9 needed to validate for double record..

the input element
JavaScript:
<div class='form-group']
	<label>Telephone :</label>[img]img/mini_indicator.gif[/img]
	<input name='telephone' class='form-control' placeholder='Telephone']
</div>

JavaScript:
		$('[name=telephone]').on('input',function(e){
			if ($(this).val().length>9)
			{
			 	$("#tel_indicator").show();
			 	
					$.ajax({
						url : 'tab_leads_details_ask_4_double.php',
						dataType : 'json',
						type : 'POST',
						data : {
							"tel" : $(this).val()
						},
			            success : function(data) {
			            	$("#tel_indicator").hide();
			            	
			            	if (data=="error")
			            		alert("Error - when checking for double records via telephone");
			            	else if (data.rec_count>0)
			            	{
								$("[name=telephone]").css({ 'background': 'rgba(255, 0, 0, 0.3)' });
							}
							else 
								$("[name=telephone]").css({ 'background': 'white' });
						}
					});
			}
			else 
				$("[name=telephone]").css({ 'background': 'white' });
		});

JavaScript:
//tab_leads_details_ask_4_double.php
<?php

require_once ('config.php');

if(!isset($_POST["tel"])){
	echo json_encode("error");
	return;
}

$db       = connect();

$r = getScalar($db,"select count(client_id) from clients where telephone=?",array($_POST["tel"]));

echo json_encode(array("rec_count"=>$r));

?>

snap5411.png


1-length = 9
2-length = 10 -> ajax call php
3-php returns > 0
 
Top