jQuery script Code
<script type="text/javascript" language="javascript" >$(document).ready(function(){
$('#add_button').click(function(){
$('#user_form')[0].reset();
$('.modal-title').text("Add Category");
$('#action').val("Add");
$('#operation').val("Add");
$('#user_uploaded_image').html('');
});
$(document).on('submit', '#user_form', function(event){
event.preventDefault();
var brand_name = $('#brand_name').val();
if(brand_name != '')
{
$.ajax({
url:"ajax/category_insert.php",
method:'POST',
data:new FormData(this),
contentType:false,
processData:false,
success:function(data)
{
alert(data);
$('#user_form')[0].reset();
$('#userModal').modal('hide');
window.location.replace("<?php echo ADMINROOT;?>listCategory.php");
}
});
}
else
{
alert("Fields are Required");
}
});
$(document).on('click', '.update', function(){
var user_id = $(this).attr("id");
$.ajax({
url:"ajax/category_fetch_single.php",
method:"POST",
data:{user_id:user_id},
dataType:"json",
success:function(data)
{
$('#userModal').modal('show');
$('#brand_name').val(data.brand_name);
$('.modal-title').text("Edit Category");
$('#user_id').val(user_id);
//$('#user_uploaded_image').html(data.user_image);
$('#action').val("Edit");
$('#operation').val("Edit");
}
})
});
$(document).on('click', '.delete', function(){
var user_id = $(this).attr("id");
if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"ajax/category_delete.php",
method:"POST",
data:{user_id:user_id},
success:function(data)
{
alert(data);
window.location.replace("<?php echo ADMINROOT;?>listCategory.php");
}
});
}
else
{
return false;
}
});
});
</script>
PHP Ajax file code : category_insert.php
<?php
include '../configClass.php';
$newobj = new connectionClass();
$db = $newobj ->db;
include('function.php');
if(isset($_POST["operation"]))
{
if($_POST["operation"] == "Add")
{
$statement = "INSERT INTO `tbl_category`(`brand_name`) VALUES ( '".$_POST['brand_name']."')";
$result = $db->query($statement);
if(!empty($result))
{
echo 'Data Inserted';
}
}
if($_POST["operation"] == "Edit")
{
$statement = "UPDATE `tbl_category` SET `brand_name`='".$_POST['brand_name']."' WHERE `id`='".$_POST["user_id"]."'";
$result = $db->query($statement);
if(!empty($result))
{
echo 'Data Updated';
}
}
}
?>
Comments
Post a Comment