I have ajax script that print comment
I would like to add loader while the server query works
what do I need to add to the "success" in order to see LOADER in my html page?
function printComments (obj) {
var element = $(obj);
var contactID = element.attr("contactID");
var type = element.attr("id");
var info = 'contactID=' + contactID + "&type=" + type + "&catID=" + catID;
$("#loader").html('<img src="images/loading.gif" align="absmiddle">');
// alert(info);
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
success: function(msg){
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
}
});
return false;
}
You can just play with hide() and show() like,
function printComments (obj) {
var element = $(obj);
var contactID = element.attr("contactID");
var type = element.attr("id");
var info = 'contactID=' + contactID + "&type=" + type + "&catID=" + catID;
$("#loader").show(); // displaying loader here
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
success: function(msg){
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
$("#loader").hide(); // hiding loader here
}
});
//return false;
}
example
HTML
<a href="#" id="verification" >test</a>
<img src="example_preloader.gif" id="ajaxPreloader" style="display:none" />
JS
$('#verification').click(function() {
$('#ajaxPreloader').toggle();
$.ajax({
type : "POST",
url : "example url",
data : {'exampleData': ''},
success : function(msg) {
$('#ajaxPreloader').text('');
location.reload();
},
error: function(error) {
$('#ajaxPreloader').text('');
}
});
});
Use beforeSend to show the loader and then in succes function hide that loader
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
beforeSend:function(){
$("#loader").show();
},
success: function(msg){
$("#loader").hide();
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
}
});
Try Below code:
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
**beforeSend: function() {
$("#loader").html("<img src="images/loading.gif" align="absmiddle">");
},**
success: function(msg){
$('#loader').hide();
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
}
});
added this line of code extra:
beforeSend: function() {
$("#loader").html("<img src="images/loading.gif" align="absmiddle">");
},
function printComments (obj) {
var element = $(obj);
var contactID = element.attr("contactID");
var type = element.attr("id");
var info = 'contactID=' + contactID + "&type=" + type + "&catID=" + catID;
$("#follows").html('<img src="images/loading.gif" align="absmiddle">');
$("#commentsContent").html('<img src="images/loading.gif" align="absmiddle">');
//loader will hide replaced by output while the server query works
$.ajax({
type: "POST",
url: "ajax/followPrint.php",
data: info,
success: function(msg){
if (type == "followsTab")
$("#follows").html(msg);
if (type == "commentsTab")
$("#commentsContent").html(msg);
}
});
return false;
}
Related
Here is MY Ajax Code
<script>
$(document).ready(function(){
$("#contactForm").submit(function(e){
var name = $("#name").val();
var phone_no = $("#phone_no").val();
var alt_ph_no = $("#alt_ph_no").val();
var id_card_number = $("#id_card_number").val();
var email_id = $("#email_id").val();
var address = $("#address").val();
$.ajax({
method: 'POST',
url: "<?php echo url('admin/customer_add') ?>",
async: false,
data: "&name=" + name + "&phone_no=" + phone_no + "&alt_ph_no=" + alt_ph_no + "&id_card_number=" + id_card_number + "&email_id=" + email_id + "&address=" + address + "_token=" + $('#token').val(),
success: function(result){
if (result == "success"){
alert("Done");
}
}
});
});
});
</script>
My Controller
public function customer_add(Request $request)
{
// dd($request);
Customer::create([
'name' => $request->input('name'),
'phone_no' => $request->input('phone_no'),
'alt_ph_no' => $request->input('alt_ph_no'),
'id_card_number' => $request->input('id_card_number'),
'email_id' => $request->input('email_id'),
'address' => $request->input('address'),
'date_con' => now(),
]);
}
When i submit this form values is store in DB but page reload on this
URL http://127.0.0.1:8000/admin/customer_add i want form is submit and
page not reload.
Use e.preventDefault(); if you don't want to reload.
<script>
$(document).ready(function(){
$("#contactForm").submit(function(e){
e.preventDefault();
var name = $("#name").val();
var phone_no = $("#phone_no").val();
var alt_ph_no = $("#alt_ph_no").val();
var id_card_number = $("#id_card_number").val();
var email_id = $("#email_id").val();
var address = $("#address").val();
$.ajax({
method: 'POST',
url: "<?php echo url('admin/customer_add') ?>",
async: false,
data: "&name=" + name + "&phone_no=" + phone_no + "&alt_ph_no=" + alt_ph_no + "&id_card_number=" + id_card_number + "&email_id=" + email_id + "&address=" + address + "_token=" + $('#token').val(),
success: function(result){
if (result == "success"){
alert("Done");
}
}
});
});
});
</script>
Or
$('body').on('submit','#contactForm',function(e){
e.preventDefault();
$.ajax({
url : "{{ url('admin/customer_add') }}",
data: new FormData(this),
type: 'POST',
contentType: false,
cache: false,
processData:false,
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
},
beforeSend: function(){
//loding..
},
success:function(result){
if (result == "success"){
$('#modal').modal('hide');
}
},
});
});
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);
}
});
});
I want to post the content of div(text) to a php page with ajax, i have already this script which post all the input fields of the three forms
.on('actionclicked.fu.wizard' , function(e, info){
if(info.step == 3 ) {
var dataString = $("#form1, #form2, #form3").serialize();
var mydiv = $('#mydiv').html();
$.ajax({
data: dataString + mydiv,
type: 'POST',
url: 'confirmation.php',
cache: false,
success: function(data) {
message : 'you request was submitted'
}
});
}
})
but i need to post also the text of the div in the php page i use
$mydiv =$_REQUEST['mydiv'];
I can echo all the other fields but nothing return for $mydiv.
Are you looking for something like this?
<div id="myDiv">
<h1>This is a page</h1>
</div>
$("#post").click(function(){
var htmlData = $("#myDiv").html();
$.post('postPage.php', {'html': htmlData },function(response){
//
});
`.on('actionclicked.fu.wizard' , function(e, info){
if(info.step == 3 ) {
var a = $("#form1").serialize();
var b = $("#form2").serialize();
var c = $("#form3").serialize();
var d = $(#mydiv).text();
data = {};
data['astring'] = a;
data['bstring'] = b;
data['cstring'] = c;
data['ddivtext'] = d;
$.ajax({
data: data,
type: 'POST',
dataType: "json",
url: 'confirmation.php',
cache: false,
success: function(data) {
message : 'you request was submitted'
}
});
}
})
I have written some code with jQuery Session, GET etc. What happens is when you click a Text Link it should come up with "Success"
But when there's no Data it works, but when I put in the data and fix it, the code stops working completely.
$(function() {
$(".open").click(function() {
var status = "Open";
var ticketid = "<?php echo $_GET['id']; ?>";
var username = "<?php echo $_SESSION['MM_Username']; ?>";
var dataString = 'status=' + status + 'id=' + ticketid + 'username=' + username;
if(status==='' || ticketid==='' || username==='' || dataString==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type: "POST",
url: "core/ticketData.php",
data: dataString,
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
This is from my View Source
$(function() {
$(".open").click(function() {
var status = "Open";
var dataString = 'status='+status;
if(status==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type: "POST",
url: "core/ticketData.php?id=772&username=NoMansLand",
data: dataString,
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
but when the PHP is empty it works completely, but when it's there it stops. I have tried Echo & print.
Any ideas?
UPDATED
Ok I have worked on this, The alerts work when you: Click it, Check Variables, Go through else, but when you hit $.ajax({ it wont alert.
<script>
$(function() {
$(".open").click(function() {
var status = "Open";
var ticketid = "<?php echo $_GET['id']; ?>";
var username = "<?php echo $_SESSION['MM_Username']; ?>";
var dataString = 'status=' + status + '&id=' + ticketid + '&username=' + username;
if(status==='' || ticketid==='' || username==='' || dataString==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
dataString = {
'status' = status,
'id' = ticketid
'username' = username
};
$.ajax({
type: "POST",
url: "core/ticketData.php",
data: dataString,
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
UPDATED
Removed the site, issue has been resolved.
You aren't concatenating correctly, you are missing &s in the data, and that's why it fails:
dataString = 'status=' + status + '&id=' + ticketid + '&username=' + username;
//---------------------------------^-------------------^
Change the way you declare
if(status==='' || ticketid==='' || username==='' || dataString==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
// declare it inside the else
// this is more optimised
dataString = {
'status' = status,
'id' = ticketid
'username' = username
};
$.ajax({
type: "POST",
url: "core/ticketData.php",
data: dataString,
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}your `dataString`
You can implement this in the following way with data representation in json format:
$(function() {
$(".open").click(function() {
var status = "Open";
var ticketid = "<?php echo $_GET['id']; ?>";
var username = "<?php echo $_SESSION['MM_Username']; ?>";
if(status==='' || ticketid==='' || username==='' || dataString==='') {
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
} else {
$.ajax({
type: "POST",
url: "core/ticketData.php",
data: {'status': status, 'id':ticketid, 'username':username},
success: function(result){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
Basically, I'm trying to make it so that when a post is submitted to my site, it sends the post using AJAX so that they don't change page, and then if the AJAX post is successful, retrieve all the posts for said user from MySQL and write them onto the page.
My problem is that the browsers (Chrome, IE) are completely ignoring the AJAX request.
My form:
<div id="updatestatus">
<form action="" method="post" id="ps">
<textarea name="status" id="status"></textarea>
<input type="hidden" name="uid" id="uid" value="<?php echo $uid; ?>" />
<input type="submit" id="poststatus" name="poststatus" value="Share" />
</form>
</div>
My AJAX request:
$(function() {
$("#poststatus").click(function() {
var status = $("textarea#status").val();
if (status == "") {
return false;
}
var uid = $("input#uid").val();
var dataString = 'status='+ status + '&uid=' + uid;
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
});
});
});
});
My ajax/query.php request
<?php
//connect stuff
$uid = strip_tags(stripslashes(htmlspecialchars(htmlentities(mysql_real_escape_string($_GET['uid'])))));
$result = mysql_query("SELECT * FROM mingle_status WHERE uid = '$uid' ORDER BY timestamp DESC"); //query
$array = mysql_fetch_row($result); //fetch result
echo json_encode($array);
?>
Thanks in advance for any help - Joe
In this section of JS code
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
});
});
You need to remove the ending bracket after the curly brace which follows the last return false, such as...
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid=<?php echo $uid; ?>",
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>");
return false;
}
});
return false;
};
});
Try this
$(function() {
$("#poststatus").click(function() {
var status = $.trim($("#status").val());
if (status == "") {
return false;
}
var uid = $("#uid").val();
var dataString = 'status='+ status + '&uid=' + uid;
$.ajax({
type: "POST",
url: "updatestatus.php",
data: dataString,
success: function() {
$.ajax({
url: 'ajax/query.php',
data: "uid="+<?php echo $uid; ?>,
dataType: 'json',
success: function(data) {
var status = data[0];
var sid = data[1];
$('#mainprofile').html("<div id='statuses'><p>"+status+"</p></div>);
return false;
}
});
return false;
}
});
});
});