Bladephp: Now with 7+ codeigniter, php, and laravel project 7+ php project From $18/Year From Rs. 1000/Year

How to jquery validation remote (ajax) using Codeigniter

by
Category:javascript
ajax codeigniter mysql javascript form validation
How to jquery validation remote (ajax) using Codeigniter

As you can see to pass through data you can simply use the key pair syntax so the request sent below the data is “&emails=example @gmail.com”. The return values for your backend script is true and false.

We’re going to use the jQuery Validation Plugin to validate our form. The basic principle of this plugin is to specify validation rules and error messages for HTML elements in JavaScript.

HTML Code

<form action="#" id="add-goldsmith" method="post">
	<div class="row">
		<div class="col-md-6">
			<div class="form-group">
				<label>Full Name <span style="color: #F00">*</span></label>
				<input type="text" name="fullname" class="form-control"  maxlength="499" autofocus  autocomplete="off" />
			</div>
		</div>
		<div class="col-md-6">
			<div class="form-group">
				<label>Email Address <span style="color: #F00">*</span></label>
				<input type="email" name="email" class="form-control"  maxlength="254" autocomplete="off" />
			</div>
		</div>
	</div>

	<div class="row">
		<div class="col-md-6">
			<div class="form-group">
				<label>Mobile </label>
				<input type="text" name="mobile" class="form-control" maxlength="499" autofocus autocomplete="off" />
			</div>
		</div>
		<div class="col-md-6">
			<div class="form-group">
				<label>Address </label>
				<textarea name="address" id="address" class="form-control" ></textarea>
			</div>
		</div>
	</div>
	<div class="row">
		<div class="col-md-4">
			<div class="form-group ">
				<input type="submit" class="btn btn-primary text-center" value="Add New GoldSmith" />
			</div>
		</div>
	</div>
</form>

Jquery / JavaScript Code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="http://bladephp.co/download/js/jquery.validate.min.js"></script>	
<script type="text/javascript">
$(function() {
	$("#add-goldsmith").validate({
		rules: {
			fullname:"required",
			email: {
				required: true,
				email: true,
				remote: {
					url: "Checkemail_goldsmith",
					type: "post"
				 }
			},
		},
		messages: {
			fullname: "Please enter your Full Name",
			email:{
				required: "Please provide a Email",
				remote: "Email already in use!",
			},
		},
		submitHandler: function(form) {
			form.submit();
		}
	});
});
</script> 

Remote Call in Controller (function Checkemail_goldsmith())

public function Checkemail_goldsmith()
{
	$requestedEmail = $this->input->post('email');
	$getemail=$this->db->get_where('gold_smith',array('email' => $requestedEmail))->num_rows();
	if($getemail == 0)
	{
		echo 'true';
	}
	else
	{
		echo 'false';
	}
}

1 Comments

Rodmark

28 Nov 2017

Thanks for this information!

Html code work in comment box


Related Post