i want to show checkbox looping from db, if i try to select data id_kendaraan in field then show the data from that id.
this my view code:
<div class="modal fade" id="tambah" tabindex="-1" role="dialog" aria-labelledby="largeModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
<h3 class="modal-title" id="myModalLabel">Tambah Pemeliharaan</h3>
</div>
<form class="form-horizontal" method="post" action="<?php echo base_url()?>Data/Pemeliharaan/Tambah">
<div class="modal-body">
<div class="form-group">
<label class="control-label col-xs-3">Nama Kendaraan</label>
<div class="col-xs-8">
<select class="form-control" name="id_kendaraan" id="id_kendaraan" required>
<option>Pilih Kendaraan</option>
<?php
if(!empty($kendaraan_)) {
foreach ($kendaraan_ as $isi) {
?>
<option value="<?php echo $isi['id_kendaraan']?>"><?php echo $isi['nama']?> - <?php echo $isi['platno']?></option>
<?php }} ?>
</select>
</div>
</div>
<?php
$index =0;
foreach ($subkriteria_ as $kr_key => $kriteria) {
?>
<div class="form-group">
<label class="control-label col-xs-3"><?php echo $kriteria['nama_kriteria'] ?></label>
<div class="col-xs-8">
<input type="hidden" name="status_pemeliharaan" value="1" class="form-control">
<input type="hidden" name="id_kriteria[]" value="<?php echo $kriteria['id_kriteria'] ?>" class="form-control">
<?php if(!empty($kriteria['sub'])) { ?>
<select class="form-control" name="isi_kriteria[]" required>
<option>Pilih Sub Kriteria</option>
<?php $no = 1; foreach ($kriteria['sub'] as $data) { ?>
<option value="<?php echo $data['value'] ?>"><?php echo $data['namasubkriteria'] ?> - <?php echo $data['value'] ?></option>
<?php $no++; } ?>
</select>
<?php } else if($kriteria['link']=='tahun_beli') { ?>
<input name="isi_kriteria[]" value="" class="form-control" type="text" placeholder="Isi Kriteria..." readonly required>
<?php } else { ?>
<input name="isi_kriteria[]" value="" class="form-control" type="number" placeholder="Isi Kriteria..." required>
<?php } ?>
</div>
</div>
<?php } ?>
<div class="form-group">
<label class="control-label col-xs-3"></label>
<div class="col-xs-8">
<ul id="sparepartList" style="list-style-type: none; padding: 0;"></ul>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Tutup</button>
<button class="btn btn-info">Submit</button>
</div>
</form>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#id_kendaraan').on('input',function(){
var id_kendaraan=$(this).val();
$.ajax({
type : "POST",
url : "<?php echo base_url('Data/Pemeliharaan/GetKendaraanById')?>",
dataType : "JSON",
data : {id_kendaraan: id_kendaraan},
cache:false,
success: function(data){
$('#sparepartList').empty(); // clear all sparepart checkboxes
$.each(data, function(id_kendaraan, tahun_beli, id_jenis){
$('[name="isi_kriteria[]"]').val(data.tahun_beli);
$('[name="id_jenis"]').val(data.id_jenis);
$.each(data.spareparts, function (key, value) {
let li = $('<li><input type="checkbox" name="id_sparepart[]" value="' + value.id_sparepart + '" />' +
'<input type="text" name="sparepart" value="' + value.sparepart + '" /></li>');
$('#sparepartList').append(li);
});
});
}
});
return false;
});
});
</script>
my controller:
function GetKendaraanById(){
$id_kendaraan=$this->input->post('id_kendaraan');
$data=$this->PemeliharaanModel->GetById($id_kendaraan);
echo json_encode($data);
}
i get data from view $id_kendaraan for my parameter.
this my model:
function GetById($id_kendaraan){
$this->db->select('*');
$this->db->from('tb_kendaraan');
$this->db->where('tb_kendaraan.id_kendaraan',$id_kendaraan);
$query_kendaraan = $this->db->get();
$hasil = false;
if($query_kendaraan->num_rows() > 0)
{
$data_kendaraan = $query_kendaraan->row();
$hasil = [
'tahun_beli' => $data_kendaraan->tahun_beli,
'id_jenis' => $data_kendaraan->id_jenis,
'spareparts' => [] // empty array, to be filled with spareparts data
];
$this->db->select('*');
$this->db->from('tb_sparepart');
$this->db->where('tb_sparepart.id_jenis', $data_kendaraan->id_jenis);
$query_sparepart = $this->db->get();
foreach ($query_sparepart->result() as $data_sparepart) {
$spareparts = [
'id_sparepart' => $data_sparepart->id_sparepart,
'sparepart' => $data_sparepart->sparepart,
];
array_push($hasil['spareparts'], $spareparts);
}
}
return $hasil;
}
but if i try this code, field tahun_beli, id jenis is success to showing in the view. but the checkbox from sparepart is looping exaggerated.
i try the json, the data is correct, show tahun_beli, id_jenis, is show and sparepart is show by id_jenis
but in view i try this code... tahun_beli and id_jenis is correct show one data, but sparepart is looping 3 times.. sparepart should be show data 1 times...
I checked your code and I think the issue is at this one:
<script type="text/javascript">
$(document).ready(function(){
$('#id_kendaraan').on('input',function(){
var id_kendaraan=$(this).val();
$.ajax({
type : "POST",
url : "<?php echo base_url('Data/Pemeliharaan/GetKendaraanById')?>",
dataType : "JSON",
data : {id_kendaraan: id_kendaraan},
cache:false,
success: function(data){
$('#sparepartList').empty(); // clear all sparepart checkboxes
$.each(data, function(id_kendaraan, tahun_beli, id_jenis){
$('[name="isi_kriteria[]"]').val(data.tahun_beli);
$('[name="id_jenis"]').val(data.id_jenis);
$.each(data.spareparts, function (key, value) {
let li = $('<li><input type="checkbox" name="id_sparepart[]" value="' + value.id_sparepart + '" />' +
'<input type="text" name="sparepart" value="' + value.sparepart + '" /></li>');
$('#sparepartList').append(li);
});
});
}
});
return false;
});
});
</script>
try to remove the first $.each, your code would be like this
<script type="text/javascript">
$(document).ready(function(){
$('#id_kendaraan').on('input',function(){
var id_kendaraan=$(this).val();
$.ajax({
type : "POST",
url : "<?php echo base_url('Data/Pemeliharaan/GetKendaraanById')?>",
dataType : "JSON",
data : {id_kendaraan: id_kendaraan},
cache:false,
success: function(data){
$('#sparepartList').empty(); // clear all sparepart checkboxes
$('[name="isi_kriteria[]"]').val(data.tahun_beli);
$('[name="id_jenis"]').val(data.id_jenis);
$.each(data.spareparts, function (key, value) {
let li = $('<li><input type="checkbox" name="id_sparepart[]" value="' + value.id_sparepart + '" />' +
'<input type="text" name="sparepart" value="' + value.sparepart + '" /></li>');
$('#sparepartList').append(li);
});
}
});
return false;
});
});
</script>
Hopefully it works
Related
i am building a project with laravel where i am able to fetch the record from database into my modal but the problem is everytime do i update my modal then the options values in both the select also gets incremented with the same fetched result. How to avoid that. Here i am pasting two images before and after of edit and also what i have done till now. Please help me to complete it.
Before Image
After Image
My modal part
Edit
<div class="modal" id="editModal" tabindex="-1" role="dialog" aria-labelledby="insertModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title font-weight-bold" id="insertModalLabel">EditCustomer</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p><b>Please note:</b> Fields marked with <span class="control-label"></span> is mandatory</p>
<form action="{{ url('admin/update_product') }}" method="post" class="edit_database_operation">
#csrf
<input class="form-control" type="hidden" name="eid" id="eid">
{{-- <input class="form-control" type="hidden" name="brand_id" id="brand_id">
<input class="form-control" type="hidden" name="category_id" id="category_id"> --}}
<div class="form-row">
<div class="form-group col-md-6">
<label for="eproduct_category" class="control-label">Product Category</label>
<select id="eproduct_category" class="form-control" name="eproduct_category">
</select>
</div>
<div class="form-group col-md-6">
<label for="eproduct_brand" class="control-label">Product Brand</label>
<select id="eproduct_brand" class="form-control" name="eproduct_brand">
</select>
</div>
</div>
<button type="submit" class="btn btn-primary text-left">Submit</button>
</form>
</div>
</div>
</div>
</div>
The script part
$(document).on('click','.edit_product',function(e){
e.preventDefault();
var url = $(this).attr('data-href');
console.log(url);
$.ajax({
url:url,
method:"GET",
success:function(fb){
var resp = $.parseJSON(fb);
console.log(resp);
$('#eid').val(resp.id);
var brand_id = resp.brand_id;
brand_edit(brand_id);
var category_id = resp.category_id;
category_edit(category_id);
}
});
return false;
});
function brand_edit(brand_id){
url = '/admin/edit_product_brand/'+brand_id;
console.log(url);
$.ajax({
url:url,
method:"GET",
success:function(fb){
console.log(fb);
$('#eproduct_brand').append(fb);
}
});
return false;
}
function category_edit(category_id){
url = '/admin/edit_product_category/'+category_id;
console.log(url);
$.ajax({
url:url,
method:"GET",
success:function(fb){
console.log(fb);
$('#eproduct_category').append(fb);
}
});
return false;
}
$(document).on('submit','.edit_database_operation',function(e){
e.preventDefault();
var url = $(this).attr('action');
var data = $(this).serialize();
$.ajax({
url:url,
method:"POST",
data:data,
success:function(fb){
var resp = $.parseJSON(fb);
// console.log(resp);
if(resp.status=='true'){
toastr.success(resp.message,'Success');
$('#editModal').modal('hide');
$('.edit_database_operation')[0].reset();
$("#example1").load(" #example1 > *");
$("#example2").load(" #example2 > *");
// $('.edit_database_operation')[0].reset();
$("#eproduct_category").data("default-value",$("#eproduct_category").val());
}else if(resp.status=='false'){
$.each( resp.message, function( key , value ) {
toastr.error(value + '<br>','Error');
});
}else if(resp.status=='false-1'){
toastr.error(resp.message,'Error');
}
}
});
return false;
});
The Controller part
public function product_edit($id){
$edit = Product::where('id',$id)->first();
$arr = array('id'=>$edit->id,'category_id'=>$edit->category_id,'brand_id'=>$edit->brand_id);
echo json_encode($arr);
}
public function edit_product_brand($id){
$brande = Brand::where('status','active')->orderBy('id','DESC')->get();
$output = '';
// $output .= '';
foreach ($brande as $brand){
$brand_id = $brand->id;
$brand_name = $brand->brand_name;
$output .= '<option value="'.$brand_id.'" '.(($brand_id == $id) ? 'selected="selected"':"").'>'.$brand_name.'</option>';
}
// $output .='</select>';
return $output;
}
public function edit_product_category($id){
$category = Category::where('status','active')->orderBy('id','DESC')->get();
$output = '';
if(!$category->isEmpty()){
foreach ($category as $brand){
$category_id = $brand->id;
$category_name = $brand->category_name;
$output .= '<option value="'.$category_id.'" '.(($category_id == $id) ? 'selected="selected"':"").'>'.$category_name.'</option>';
}
}
return $output;
}
public function update_product(Request $request){
$update = Product::where('id',$request->eid)->first();
$validator = Validator::make($request->all(),
[
'eproduct_category'=>'required',
'eproduct_brand'=>'required',
],[
'eproduct_category.required'=>'Please enter product category',
'eproduct_brand.required'=>'Please enter product brand',
]);
if(!$validator->fails()){
$update->name = $request->eproduct_name;
$update->category_id = $request->eproduct_category;
$update->brand_id = $request->eproduct_brand;
if($update->update()){
$arr = array('status'=>'true','message'=>'Updated Successfully');
}else{
$arr = array('status'=>'false-1','message'=>'Not Updated');
}
}else{
$arr = array('status'=>'false','message'=>$validator->errors()->all());
}
echo json_encode($arr);
}
In your jquery code you have use .append() to append new options inside your select-box which just insert new elements to the end of each element in the set of matched elements.So that's the reason it was showing double values.
Instead use .html() to replace all element which are there inside your select-box. So you just need to change :
$('#eproduct_brand').append(fb);
To
$('#eproduct_brand').html(fb);
Same you do for eproduct_category as well.
Output of my requirement
I have tried many times to get the required result in html page.
am calling a function when i check a box in html ,the function giving back result of json data and this json data i wanna display dynamically into my card style.
Here is hmtl code:
<div id="filter"style="margin-top:100px;margin-left:100px">
<h2>Filter options</h2>
<div>
<input type="checkbox" id="Samsung" checked>
<label for="Samsung">Samsung</label>
</div>
<div>
<input type="checkbox" id="iPhone" checked>
<label for="iPhone">iPhone</label>
</div>
<div>
<input type="checkbox" id="HTC" checked>
<label for="HTC">HTC</label>
</div>
<div>
<input type="checkbox" id="LG" checked>
<label for="LG">LG</label>
</div>
<div>
<input type="checkbox" id="Nokia" checked>
<label for="Nokia">Nokia</label>
</div>
</div>
<table id="phones" >
<thead>
</thead>
<tbody >
<div style="margin-top:100px;margin-left:100px;margin-right:100px" >
<div>
<label style="margin-bottom:50px;margin-left:80px"><h2>Find your best hotel</h2></label>
</div>
<div class="col-xs-12 col-sm-4" style="background-color:lavender;height:310px;width:310px"><label id="hotel_image"></label></div>
<div class="col-xs-12 col-sm-6" style="background-color:lavenderblush;"><label id="hotel_name"></label></div><br>
<div class="col-xs-12 col-sm-3" style="background-color:lavender;margin-left:100px;margin-top:10px"><label id="hotel_price"></label></div>
<div class="col-xs-12 col-sm-3" style="background-color:#ccc;margin-top:220px;margin-left:300px"><label id="book_me"></label></div>
</div>
</tbody>
</table>
Here is javascript code:
<script>
function getPhoneFilterOptions(){
var opts = [];
$checkboxes.each(function(){
if(this.checked){
opts.push(this.id);
}
});
return opts;
}
function updatePhones(opts){
$.ajax({
type: "POST",
url: "submit.php",
dataType : 'json',
cache: false,
data: {filterOpts: opts},
success: function(data){
$.each(data, function() {
$.each(this, function(k , v) {
if(k==='img_path'){
v = "<a href='image_description.html'><image src='img_path' height='300px' width='300px' ></a>";
("#hotel_image").html(v);
} else if (k==='price'){
("#hotel_price").html(v);
} else if (k==='brand'){
("#hotel_name").html(v);
}
})
});
}
})
}
var $checkboxes = $("input:checkbox");
$checkboxes.on("change", function(){
var opts = getPhoneFilterOptions();
updatePhones(opts);
});
$checkboxes.trigger("change");
</script>
am geeting data json data from submit.php like this:
[
{
"img_path":"photo1.jpg",
"price":"2000",
"brand":"AAMSOTO"
},
{
"img_path":"photo4.jpg",
"price":"2500",
"brand":"AfMSOTO"
},
{
"img_path":"photo2.jpg",
"price":"3000",
"brand":"CAMSOTO"
},
{
"img_path":"photo3.jpg",
"price":"4000",
"brand":"BAMSOTO"
}
]
Assuming data is a javascript object the following inside your success method should work:
Make sure data is a javascript object, if its not you will have to use JSON.parse() to turn the JSON data into one.
$.each(data, function() {
var imageUrl = this['img_path']
var price = this['price']
var brand = this['brand']
const img = document.createElement('img');
img.src = imgUrl;
document.getElementById('#hotel_image').appendChild(img);
document.getElementById('#hotel_price').appendChild(document.createTextNode(price));
document.getElementById('#hotel_name').appendChild(document.createTextNode(brand));
}
I have a custom dropdown. Want to change the value of the button as selected option of dropdown
list.
This is my view code. I tried with jquery but unsuccessfull.
Please some one help me with jquery
<div class="col-md-5 col-sm-6">
<div class="form-group droplist">
<label for="project_category">project Categary</label><br/>
<input type="button" class="form-control" id="relig" value="<?php if(isset($projectDetails->project_category) && !empty($projectDetails->project_category)){
echo #$projectDetails->project_category;
}else{
echo "select Category";
}?>">
<div id="religions" class="dropdownmenu" style="display:none;padding-left:17px">
<?php foreach($categories as $cat){ ?>
<div id="<?php echo $cat['code'] ?>" class="maincategory">
<label><input type="radio" class="category" name="project_category" id="category" value="<?php echo $cat['code'] ?>"> <?php echo $cat['Project_classification_id']." - ".$cat['Description'] ?></label><br/>
</div>
<?php } ?>
</div>
</div>
</div>
I have above 140 dropdown values for that i used foreach, notonly 3 values
This is my jquery. I done all thing but unable to show the checked radio value as button value(title)
$('#relig').click(function(){
$('#religions').slideToggle("fast");
});
traversed_ids = [];
$(document).on('change','.project_category',function() {
maincat = $(".project_category:checked").val();
if ($.inArray(maincat, traversed_ids) < 0) { //check element exist in array or not
traversed_ids.push(maincat); //add element to array
changeCategoryList(maincat); //call ajax
}
});
function changeCategoryList(maincategory){
$.ajax({
url: '<?php echo site_url("abcd/xyz"); ?>',
type: 'POST',
data: { maincategory:maincategory },
dataType: 'json',
success: function(data) {
$.each(data, function(key, value) {
var MoreTag='';
MoreTag += '<div id="'+value.code+'" Style="padding-left:20px" class="subcategory">';
MoreTag += '<label ><input type="radio" class="project_category" name="project_category" id="project_category" value="'+value.code+'"> '+value.Project_classification_id+' - '+value.Description+'</label><br/>';
MoreTag += '</div>';
$("#"+maincategory).append(MoreTag);
});
}
});
}
$(".maincategory").click(function (e) {
e.stopPropagation(); //to stop event bubbling
if ($(this).children('.subcategory').is(':checked')) { //check if hidden or not
$(this).children('.subcategory').hide(); //if yes hide
} else {
$('.maincategory').children('.subcategory').hide();
$(this).children('.subcategory').show(); // else show
}
});
I have made sample example as per my understanding from your question.
Please check below example.
$(document).on('change','input[name="rgroup"]',function() {
radio = $("input[name='rgroup']:checked").val();
$("#btn").val(radio);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="rgroup" value="test1">Test1
<input type="radio" name="rgroup" value="test2">Test2
<input type="radio" name="rgroup" value="test3">Test3
<input type="radio" name="rgroup" value="test4">Test4
<br>
<input type="button" id="btn" value="Change">
I have created a form with menu and submenu. I load the menu dropdown from database but i am facing the problem to load submenu from database using ajax,json and codeigniter pls solve my issue.. thanks in advance...
This is My view coding
<form action="" method="post" id="frm_submenu">
<div class="form-group">
<label for="menu">Select Menu</label>
<select class="form-control" id="selectmenuid">
<option value="">-- Select Menu --</option>
<?php foreach($showData as $show):?>
<option value="<?php echo $show->menu_id?>"><?php echo $show->menu_name?></option>
<?php endforeach;?>
</select>
</div>
<div class="form-group">
<label for="menu">Select Sub Menu</label>
<select class="form-control" id="selectsubmenu">
</select>
</div>
<div class="form-group">
<label for="imagetitle">Image Title</label>
<input type="text" class="form-control" name="imagetitle" id="imagetitle" placeholder="Enter Image Title" required="required">
</div>
<div class="form-group">
<label class="btn btn-default btn-file">
Browse <input type="file" style="display: none;">
</label>
</div>
<button type="submit" class="btn btn-primary" id="submit">Submit</button>
</form>
This is my ajax and jquery and json coding
$( "#selectmenuid" ).change(function() {
var id = $('#selectmenuid').val();
populate_submenu(id);
});
function populate_submenu(id){
$('#selectsubmenu').empty();
$('#selectsubmenu').append("<option>Loading ....</option>");
$.ajax({
type: "POST",
url : "<?php echo site_url('Admin_Creator/populate_submenu')?>/"+id,
contentType:"application/json;charset=utf-8",
dataType:'json',
success:function(data){
$('#selectsubmenu').empty();
$('#selectsubmenu').append("<option>Select Sub Menu</option>");
$.each(data,function(i,name){
$('#selectsubmenu').append('<option value="'+data[i].submenu_id+'"'+data[name].submenu_name+'</option>');
});
}
});
}
This is my controller coding
public function populate_submenu($id){
$smid=$id;
$data['query']= $this->db->select("select * from submenu where menu_id='$smid'");
echo json_encode($data);
}
You had an error when structuring your item html with appends. Use this instead:
$.each(data,function(key,value){
$('#selectsubmenu').append('<option value="'+value.submenu_id+'">'+value.submenu_name+'</option>');
});
You have a few problems at a first view. First your $smid variable should be in double quotes or escaped corespondingly. Variables in single quotes are treated as they look so your php code should look like this:
public function populate_submenu($id){
$smid=$id;
$data['query']= $this->db->select('select * from submenu where menu_id="$smid"');
echo json_encode($data);
}
In your JS code you are not closing the > from the starting open <option> tag in the each() method and getting the submenu_name wrong, you should get it by the index, so:
$('#selectsubmenu').append('<option value="' + data[i].submenu_id + '"' + data[name].submenu_name + '</option>');
should be:
$('#selectsubmenu').append('<option value="' + data[i].submenu_id + '">' + data[i].submenu_name + '</option>');
The code should be:
$("#selectmenuid").change(function() {
var id = $('#selectmenuid').val();
populate_submenu(id);
});
function populate_submenu(id) {
$('#selectsubmenu').empty();
$('#selectsubmenu').append("<option>Loading ....</option>");
$.ajax({
type: "POST",
url: "<?php echo site_url('Admin_Creator/populate_submenu')?>/" + id,
contentType: "application/json;charset=utf-8",
dataType: 'json',
success: function(data) {
$('#selectsubmenu').empty();
$('#selectsubmenu').append("<option>Select Sub Menu</option>");
$.each(data, function(i, name) {
$('#selectsubmenu').append('<option value="' + data[i].submenu_id + '">' + data[i].submenu_name + '</option>');
});
}
});
}
I want to validate my form's input with database, so when user type on form's input and contain email already in use or exists it will display an alert and cant submit. I use CodeIgniter framework and jQuery.
I've tried using the code below to check if name exists and this could work. But when I apply it to the other case for email, it doesn't work and display message "The URI you submitted has disallowed characters."
How is the correct way to fix this?
View (kasir_halaman.php) :
<div id="addModal" class="modal fade" role="modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 class="modal-title"><span class="glyphicon glyphicon-plus"></span> Tambah Kasir</h3>
</div>
<div class="modal-body">
<form action="<?php echo site_url('admin/kasir/addpetugas'); ?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label>Nama</label>
<input type="text" id="nama" name="nama" class="form-control" maxlength="100" required>
</div>
<div class="form-group">
<label>E-mail</label>
<input type="email" id="email" name="email" class="form-control" maxlength="150" required>
</div>
<div class="form-group">
<label>Kategori</label>
<select class="form-control" name="kategoripetugas" required>
<option value=""> -- Pilih Kategori -- </option>
<option value="1">Admin</option>
<option value="2">Kasir</option>
</select>
</div>
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" maxlength="30">
</div>
<div class="form-group">
<label>Ulangi Password</label>
<input type="password" name="confirmpassword" class="form-control" maxlength="30">
</div>
<button type="submit" class="btn btn-primary" style="width:100%;">Tambah</button>
</form>
</div>
</div>
</div>
</div>
Controller (kasir.php) :
public function cekData($table, $field, $data)
{
$match = $this->Crud->read($table, array($field=>$data), null, null);
if($match->num_rows() > 0){
$report = 2;
}else{
$report = 1;
}
echo $report;
}
public function register_email_exists()
{
if (array_key_exists('email',$_POST)) {
if ($this->Crud->email_exists($this->input->post('email')) == TRUE ) {
echo false;
} else {
echo true;
}
}
}
Model (Crud.php) :
function email_exists($email)
{
$this->db->where('email', $email);
$query = $this->db->get('petugas');
if( $query->num_rows() > 0 ){ return TRUE; } else { return FALSE; }
}
jQuery AJAX (petugas.js) :
$(document).ready(function(){
var check1=0; var id;
$("#nama").bind("keyup change", function(){
var nama = $(this).val();
$.ajax({
url:'kasir/cekData/petugas/nama/'+nama,
data:{send:true},
success:function(data){
if(data==1){
$("#report1").text("");
check1=1;
}else{
$("#report1").text("*nama petugas sudah terpakai");
check1=0;
}
}
});
});
var check2=0;
$("#email").bind("keyup change", function(){
//var email = $(this).val();
$.ajax({
url:'kasir/register_email_exists',
data:{send:true},
success:function(data){
if(data==1){
$("#report2").text("");
check2=1;
}else{
$("#report2").text("*email sudah terpakai");
check2=0;
}
}
});
});
var check4=0;
$("#confirmpassword").bind("keyup change", function(){
var password = $("#password").val();
var confirmpassword = $(this).val();
if (password == confirmpassword){
$("#report4").text("");
check4=1;
}else{
$("#report4").text("*Password tidak sama");
check4=0;
}
});
$("#submit").click(function(event){
if(check1==0){
event.preventDefault();
}
if(check4==0){
event.preventDefault();
}
});
});
Use ajax post method instead and take data at php side from POST request
you can check more about jquery ajax here: http://api.jquery.com/jquery.post/
and about php post here: http://php.net/manual/en/reserved.variables.post.php
//JS
$("#email").bind("keyup change", function(){
var email = $(this).val();
$.ajax({
url:'kasir/register_email_exists',
type: "POST",// <---- ADD this to mention that your ajax is post
data:{ send:true, email:email },// <-- ADD email here as pram to be submitted
success:function(data){
if(data==1){
$("#report2").text("");
check2=1;
}else{
$("#report2").text("*email sudah terpakai");
check2=0;
}
}
});
});
// PHP
// At php side take your data from $_POST
$send = $_POST['send'];
$email = $_POST['email'];
...