Got Twice trigger on change paste keyup - php

So i have scanner and input text, if scanner get value max 9 digit, is will insert ot my database (automacly). These my coding
$(window).load(function(){
$( "#scannerinput" ).focus();
$('#scannerinput').bind("change paste keyup", function(){
var barcode = $(this).val();
var judul = $(this).attr("target-judul");
var dataString = "judul=" + judul + "&barcode=" + barcode;
if(this.value.length ==9){
$.ajax
({
type: "POST",
url: url+"ajax",
data: dataString,
cache: false,
success: function(data)
{
//window.location.href = url;
}
});
$( "#scannerinput" ).blur();
//console.log(dataString);
}
});
});
My problem is my code make insert twice or get trigger twice. How to make it just once trigger??? Any idea??

add array of exist data:
var exists_dataString_arr=[]; //array of exist data
$(window).load(function(){
$( "#scannerinput" ).focus();
$('#scannerinput').bind("change paste keyup", function(){
var barcode = $(this).val();
var judul = $(this).attr("target-judul");
var dataString = "judul=" + judul + "&barcode=" + barcode;
if((this.value.length ==9)&&(exists_dataString_arr.indexOf(ataString)<0)){ //if not in array
$.ajax
({
type: "POST",
url: url+"ajax",
data: dataString,
cache: false,
success: function(data)
{
//window.location.href = url;
exists_dataString_arr=[];
}
});
exists_dataString_arr.push(dataString); //add data in array
$( "#scannerinput" ).blur();
//console.log(dataString);
}
});
});

Related

How to pass id with url to php file using ajax call?

I want to pass id with ajax to a php file. I tried a lot but couldnot succed. I want to return to the page with id. Please help me.
I tried like this.
<script>
jQuery.ajax({
var registration_date=$("#registration_date").val()
var building_length_ft=$("#building_length_ft").val();
var building_length_in=$("#building_length_in").val();
var building_breadth_ft=$("#building_breadth_ft").val();
var reg_id=$_GET['id'].val();
var dataString = $('#a').serialize();
$.ajax({
url:'registration_detail.php',
method:'POST',
data: dataString,
success:function(data){
alert(data);
}
});
});
</script>
I tried below code as well.
var reg_id=$_GET['id'].val();
$.ajax({
url:'registration_detail.php',
method:'POST',
data:{
'reg_id=' + reg_id,
},
success:function(data){
alert(data);
}
});
I tried below code as well.
var id = $('#id').val();
$.ajax({
url:'registration_detail.php',
method:'POST',
data: { id: id},
success: function(response) {
$('#result').html(response);
}
});
You should fix your code:
<script>
jQuery.ajax({
var registration_date=$("#registration_date").val()
var building_length_ft=$("#building_length_ft").val();
var building_length_in=$("#building_length_in").val();
var building_breadth_ft=$("#building_breadth_ft").val();
// There is a PHP script here:
var reg_id=<?= $_GET['id'] ?>;
var dataString = $('#a').serialize();
$.ajax({
url:'registration_detail.php',
method:'POST',
data: dataString,
success:function(data){
alert(data);
}
});
});
</script>

Updating database table with AJAX on change

I'm trying to update a database column once an input field has changed.
however the code below just isn't doing it and am puzzled.
<script>
$("#addline1").change(function() {
var itemVal = $("#addline1").val();
var dataString = "companyid=" + companyid + "&addline1=" + itemVal;
processChange(dataString);
});
function processChange(dataString){
$.ajax({
type: "POST",
url: "../inc/dataforms/test.php",
data: dataString,
complete: function(data) {
var Resp = data.responseText;
console.log(Resp);
}
});
};
</script>
companyid is already defined elsewhere on the page.
I've tried change, onchange...
My PHP code is:
mysqli_query($dbc,"UPDATE `comp_companies` SET `regoffice1` = '$_POST[addline1]'
WHERE `company_id` = '$_POST[companyid]'");
Saying unexpected token (
somewhere in here
$("#addline1").change(function() {
var itemVal = $.("#addline1").val();
var dataString = "companyid=" + companyid + "&addline1=" + itemVal;
processChange(dataString)
});
I suggest you to use an object instead of a string... To pass the data, as the method used is POST.
(Assuming companyid is defined...)
<script>
$("#addline1").change(function() {
var itemVal = $("#addline1").val(); // Remove the extra dot that was here
// var dataString = "companyid=" + companyid + "&addline1=" + itemVal;
// I suggest the following:
var dataObj = {companyid:companyid, addline1:itemVal};
processChange(dataObj);
});
function processChange(dataObj){
$.ajax({
type: "POST",
url: "../inc/dataforms/test.php",
data: dataObj,
dataType: "json", // If you expect a json as a response
complete: function(data) {
var Resp = data.responseText;
console.log(Resp);
});
});
});
</script>

how to pass whole data using ajax in php using keypress event

I am passing empcode using key press event, but my whole empcode is not transfered and the last digit is cut.
Here is my code:
$(document).ready(function(){
$("#e_code").keypress(function(){
//var dataString=document.getElementById("e_code").value;
var dataString = 'e_code='+ $(this).val();
$.ajax({
type: "POST",
url: "getdata.php",
data: dataString,
cache: false,
success: function (html) {
$('#details').html(html);
$('#custTrnHistory').show()
}
});
});
});
on getdata file code is
write code in keyup instead of keypress
$("#e_code").keyup(function(){
You can bind your keypress on document --- try this
$(document).on('keypress',"#e_code",function(){
Try this
$(document).ready(function(){
var minlength = 5; //change as per the the length of empcode
$("#e_code").keyup(function () {
var inputvalue= $(this).val();
if (inputvalue.length >= minlength ) {
var dataString = 'e_code='+ $(this).val();
$.ajax({
type: "POST",
url: "getdata.php",
data: dataString,
cache: false,
success: function (html) {
$('#details').html(html);
$('#custTrnHistory').show()
}
});
}
});
});

Error in updating table fields when posting values through ajax

I m trying to update profile of student through ajax the script is runing good i get all the values passed in dataString but it is not updating the values of fields when the savebasic.php is called through ajax.
Jscript:
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".savestudent").click(function() {
var _firstname=$("#firstname").html();
var _lastname=$("#lastname").html();
var _gender=$("#gender").html();
var _location=$("#location").html();
var _aboutme=$("#about").html();
var _dob=$("#dob").html();
var dataString= 'fname='+ _firstname + '&lname='+ _lastname + '&gender='+ _gender + '&location='+ _location + '&about='+ _aboutme + '&dob='+ _dob ;
alert(dataString);
$.ajax
({
type: "POST",
url: "savebasic.php",
data: dataString,
cache: false,
success: function(html)
{
alert('success');
},
error: function(html)
{
}
});
});
});
</script>
savebasic.php:
<?php
include_once('controller/profile.controller.php');
$profileObject=new ProfileController();
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$gender='M';
$loc=$_POST['location'];
$about=$_POST['about'];
$birth=$_POST['dob'];
$upt=$profileObject->updateUserprofile('59',$fname,$lname,$birth,$gender,$loc,$about);
?>
if i directly pass values on savebasic.php it works . but while passing through ajax it does nothing.
You are using data field in ajax in a wrong way. You should do:
data:{'fname=': _firstname, 'lname=':_lastname,'gender=':_gender,'location=': _location}
your data string is set up like a GET - normal syntax would be:
data: {'fname': _firstname, 'lname':_lastname .... }
Change your code to assign data in following way
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".savestudent").click(function() {
var _firstname=$("#firstname").html();
var _lastname=$("#lastname").html();
var _gender=$("#gender").html();
var _location=$("#location").html();
var _aboutme=$("#about").html();
var _dob=$("#dob").html();
var data= { fname:_firstname,
lname:_lastname,
gender=: _gender ,
location:_location,
about: _aboutme,
dob: _dob};
alert(data);
$.ajax
({
type: "POST",
url: "savebasic.php",
data: data,
cache: false,
success: function(html)
{
alert('success');
},
error: function(html)
{
}
});
});
});
</script>
Also, it is not correct way to get the value from the element..
If the firstname, lastname and other fields are in text boxes, then you should access those like this.
var _firstname=$("#firstname").val();
Or if they are in span or div tag, then access those like this
var _firstname=$("#firstname").text();
your data part is wrong . also you forgot to mention the dataType
I have edited your code .
check it out :
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(".savestudent").click(function() {
var _firstname=$("#firstname").html();
var _lastname=$("#lastname").html();
var _gender=$("#gender").html();
var _location=$("#location").html();
var _aboutme=$("#about").html();
var _dob=$("#dob").html();
// var dataString= 'fname='+ _firstname + '&lname='+ _lastname + '&gender='+ _gender + //'&location='+ _location + '&about='+ _aboutme + '&dob='+ _dob ;
// alert(dataString);
$.ajax
({
type: "POST",
contentType: 'text/html',
dataType: 'text',
url: "savebasic.php",
data: {
fname: _firstname, lname :_lastname, gender :_gender, location : _location
,about : _aboutme , dob : _dob
},
cache: false,
success: function(html)
{
alert('success');
},
error: function(html)
{
}
});
});
});
</script>
this will give you 100% correct result .

ajax and php reload for paging having trouble figuring it out

$(function(){
$('.page').click(function() {
var paging = $(this).attr("name");
var dataString = 'page='+paging;
$.ajax({
type: "POST",
url: "<?php echo $backtrack; ?>shop.php",
data: dataString,
success: function(){
$('#products').load('shop.php/skateboards/ #products > *', function(){
$('#products').delay(200).fadeOut();
$('#products').delay(600).fadeIn();
});
$(".current-page").attr("class", "");
}
});
return false;
});
});
I am guessing this is not working because I am reloading the part of the page that gets the post. What I want to do is post the page number to the current page and then reload the items inside the products div, with it being the next set of items
I think you need to do something like this:
$('.page').click(function() {
$('#products').delay(200).fadeOut();
var paging = $(this).attr("name");
var dataString = 'page='+paging;
$.ajax({
type: "POST",
url: "<?php echo $backtrack; ?>shop.php",
data: dataString,
complete: function() {
$.get('shop.php/skateboards/', function(data) {
$('#products').replaceWith(data);
});
}
});
return false;
});

Categories