As stated in the title. I am using JQuery and Validate plugin to validate user inputs in a form.
I am trying to implement check for duplicate values in a table.
For this I am using following code:
$().ready(function(){
//alert('JQuery Functioning');
$("#cat_name").change(function()
{
var cat_name = $("#cat_name").val();
$.ajax({
type:"POST",
url:"ws/cat_duplicate.php",
data:"cat_name="+cat_name,
cache: false,
success: function(html)
{
//alert(html);
if(html == 1)
{
$("#duplicate").html("<br/>Duplicate category Found. Please, Enter Unique Category Name");
$("#add_rec").attr("disabled",true);
}
else
{
$("#duplicate").html("");
$("#add_rec").attr("disabled",false);
}
}
}); //$.ajax({
}); //$("#cat_name").change(function()
$("#frmAddCategory").validate({
rules: {
cat_name: "required"
}, //rules: {
messages: {
cat_name: "<br />Please provide Category Name here"
} //messages: {
}); //$("#frmAddCategory").validate
});
Here is the HTML part:
<h2>Category Edit</h2>
<form id="frmAddCategory" name="frmAddCategory" method="post" action="">
<table width="25%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td>ID</td>
<td>Auto Generated</td>
</tr>
<tr>
<td>Category Name</td>
<td><input name="cat_name" type="text" id="cat_name" value="" maxlength="60" /><span id="duplicate"></span></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr align="center" valign="middle">
<td colspan="2"><input type="submit" name="add_rec" id="add_rec" value="Add" />
<a href="cat_list.php">Cancel</a></td>
</tr>
</table>
</form>
The PHP code that I am calling is:
<?php
if(isset($_POST['cat_name']))
{
require_once('../conn/blog.php');
mysqli_select_db($blog, $database_blog);
$cat_name = $_POST['cat_name'];
$query = "SELECT cat_name FROM category WHERE cat_name = '$cat_name'";
$query_retrived = mysqli_query($blog, $query) or die("Error:".mysqli_error($blog));
$rows = mysqli_fetch_assoc($query_retrived);
$result = mysqli_num_rows($query_retrived);
if ($result>0)
{
echo '1';
}
else
{
echo '0';
}
mysqli_free_result($query_retrived);
}
?>
But in this code I have to enable or disable the submit button. Instead of that I want the Validate plugin to handle checking of duplicate values.
How can we do this?
TIA