Ajax call not updating first time - php

I used Ajax call for display data and update data. Ajax call working well but it's not updating at single time. database updating well in server. For UI part it's not updating.
Used Ajax Call
$('#loginbutt').click(function(){
$.post('data.php',{action: "update"},function(res){
$('#result').html(res);
});
var servertime = $("#timer1").text();
var dataString = 'current_time='+ servertime;
$.ajax({
type: "POST",
url: "inset_intime.php",
data:dataString ,
cache: true,
beforeSend: function(html) {
document.getElementById("lastfive").innerHTML = '';
},
success: function(html){
$("#lastfive").append(html);
}
});
});
it's updating data .php but not updating inset_time.php. data is inserting well inti server. but UI not updating. IN data.php i given insert query for insert data. In inset_time.php i given select query for displaying data. but why it's not at a time when i click on loginbutt.

Try this
$(document).ready(function(){
$('#loginbutt').live('click',function(){
$.post('data.php',{action: "update"},function(res){
$('#result').html(res);
});
var servertime = $("#timer1").text();
var dataString = 'current_time='+ servertime;
$.ajax({
type: "POST",
url: "inset_intime.php",
data:dataString ,
cache: true,
beforeSend: function(html) {
document.getElementById("lastfive").innerHTML = '';
},
success: function(html){
$("#lastfive").append(html);
}
});
});
});

Related

with ajax post and show recived data

I write this code, but it doesn't work. I want to show an array in php using ajax.
It's an html select that chooses every option of this select list value of the option take in the variable and sends it to ajax. Ajax should then post data to php then php select the received data from database and show all of them.
But I can't show this data in ajax. :(
$(function(){
$("#topic").change(function(){
var str = "";
$( "select option:selected" ).each(function() {
str += $( this ).text() + " ";
options(str);
});
});
});
function options(option){
$.ajax({
type: "POST",
dataType: 'json',
url: "/Register/checkSelect", //Relative or absolute path to response.php file
data: {
option:option
}).done(function(){
$("#content").html(data);
alert("ok");
});
});
}
You have an error in your ajax request.
Here the right code:
function options(option){
$.ajax({
type: "POST",
dataType: 'json',
url: "/Register/checkSelect", //Relative or absolute path to response.php file
data: {
option:option
}
}).done(function(data){
$("#content").html(data);
alert("ok");
});
}

Stop previous ajax when new request is made

I have a script
$('#postinput').on('keyup',function(){
var txt=$(this).val();
$.ajax({
type: "POST",
url: "action.php",
data: 'txt='+txt,
cache: false,
context:this,
success: function(html)
{
alert(html);
}
});
});
Suppose someone types a ,ajax runs . Immediately he types b c and so on. Ajax runs everytime. Is there a way to stop previous request when new is made ?
You can use the abort() method of the xhr. Try this:
var currentXhr;
$('#postinput').on('keyup',function(){
currentXhr && currentXhr.readyState != 4 && currentXhr.abort(); // clear previous request
var txt = $(this).val();
var currentXhr = $.ajax({
type: "POST",
url: "action.php",
data: 'txt=' + txt,
cache: false,
context:this,
success: function(html) {
alert(html);
}
});
});
If you don't want to use a global variable, you could store the xhr in a data-* attribute of the #postinput element.
Another method is to only fire the AJAX request when the user has stopped typing:
var timer;
$('#postinput').on('keyup',function(){
clearTimeout(timer);
var txt = $(this).val();
var timer = setTimeout(function() {
$.ajax({
type: "POST",
url: "action.php",
data: 'txt=' + txt,
cache: false,
context:this,
success: function(html) {
alert(html);
}
});
}, 100); // sends request 100ms after typing stops.
});
You can do like this with Jquery-
jaxRequests = new Array();
queueRequest = function(whatever, you, want) {
if(ajaxRequests[ajaxRequests.length - 1]) {
ajaxRequests[ajaxRequests.length - 1].abort();
}
ajaxRequests[ajaxRequests.length] = //Insert New jQuery AJAX call here.
}

jquery .load not loading immediately after loading data

I was wondering if this was making an asynchronous request...write now Im using:
<script type='text/javascript'>
$(document).ready(function() {
var ktitle = $('.hiddentwo').text();
$('div#tab2').load('morefour.php?title=' + encodeURIComponent(ktitle));
});
</script>
what Im doing though is adding text in the first, into the database, on the current php file (addtext.php). Im passing the Id of the current document to the morefour.php and that is loading the added text on the second tab...the thing is, Im having to refresh to see the content again. Im running on localhost btw.
For more clarity, Im running another jquery script that on clicks, retrieves this data to send it to a php file to enter into a database
$(".button").click(function() {
var content = $(this).siblings().outerHTML();
$.ajax({
async: false,
type: "POST",
url: "tosqltwo.php",
data: {
content: content
}
});
});
$(function(){ //shorthand of $(document).ready
$('div#tab2').html($.ajax({
type: "GET", //if you are doin $_GET['title'] in morefour.php
url: "morefour.php",
data : {title:ktitle},
dataType: 'html', //i am not sure about this part
async: false
}).responseText)
});
or you can try
$(function(){
$.ajax({
url : 'morefour.php',
data : {title:ktitle},
type:'GET',
dataType:'html',
success: function(data) {
$('div#tab2').html(data);
}
});
});
you can use $.ajax function with async to false.
$.ajax({
async: false,
url : 'morefour.php',
data : 'title=' + encodeURIComponent(ktitle),
success: function(data) {
$('div#tab2').html(data);
}
});

send each element with class name via AJAX to php

Hi i'm using the below code to take data and send it to a php page, it works as i want it to but its only sending the first ".order" it comes across, ie. i need to send every element with class="order", would this be some sort of .each()?
$('#submit').click(function(){
var order=$('.order').html();
var dataString = 'order='+ order;
$.ajax
({
type: "POST",
url: "order.php",
data: dataString,
cache: false,
success: function(html)
{
$("#response").html(html);
}
});
});
Did this instead and it now works, bizarre!
$('#submit').live('click',function(){
var order=$('.order').text();
var dataString = 'order='+ order;
$.ajax
({
type: "POST",
url: "order.php",
data: dataString,
cache: false,
success: function(html)
{
$("#response").html(html);
}
});
});
Try var order = $('form').serialize() as explained here http://api.jquery.com/serialize/
Other than this, you must do something like:
$('.order').each(function(){
// Get values from order here... something like: order += $(this).html();
// Also, note that if '.order' are inputs you should use $(this).val() instead of $(this).html();
});
Hope it helps.
I believe this should do it:
var order=$('.order')
.map(function(){ return this.innerHtml; })
.get().join('');
var dataString = 'order='+ order;

How can I get my div id to reload via ajax and jquery

I'm creating a CMS using jQuery and AJAX. When I click, my "Add Campaign" buttom, it creates a unique client ID in the DB and on a hard reload, the new client shows up in its container. I am trying to use ajax to reload the container on the fly and I'm not having the exact luck i am hoping for. I can get it to reload, but it's like it's pulling in descriptions of each of the clients as well!
function AddNewClient() {
dataToLoad = 'clientID=' + clientID + '&addClient=yes';
$.ajax({
type: 'post',
url: '/clients/controller.php',
datatype: 'html',
data: dataToLoad,
target: ('#clientssidebar'),
async: false,
success: function(html){
$('#clientssidebar').html(html);
},
error: function() {
alert('An error occured!');
}
});
};
Try:
function AddNewClient()
{
dataToLoad = 'clientID=' + clientID + '&addClient=yes';
$.ajax({
type: 'post',
url: '/clients/controller.php',
data: dataToLoad,
success: function(html){
$('#clientssidebar').html(html); },
error: function() {
alert('An error occured!'); }
});
}

Categories