Display items from json file in an alert (sweetalert) jQuery - javascript

I am using sweetalert for alerts and want to display some data from an adjacent .json file in a sweetalert alert with jQuery. The json file has 5 items and in a seperate attempt using getJSON I managed to display the first 2 so I am trying the $ajax method to do more. The current code I have just goes right down to the "always" function and displays "complete" in the alert, it doesn't display the success method. So I would like to know how to alter the code to display the items in the alert.
The code is below.
jQuery(document).ready(function($) {
$(document).on('click', '#json', function(event) {
event.preventDefault();
$.ajax({
url: 'somedata.json',
type: 'GET',
dataType: 'json',
data: {param1: 'value1'}
})
.done(function() {
swal("success");
})
.fail(function() {
swal("error");
})
.always(function() {
swal("complete");
});
});
});

Try somthings like this
jQuery(document).ready(function($) {
$(document).on('click', '#json', function(event) {
event.preventDefault();
$.ajax({
url: 'somedata.json',
type: 'GET',
dataType: 'json',
data: {param1: 'value1'}
})
.done(function(data) {
data.each(el,function(){
setTimeout(function(){
swal(el);
},300);
});
});
});
});
EDIT
from this bug report you have to set time between instances Source

Related

How to intercept POST data with Javascript

I am using this javascript function to send data to my php script (via POST) I am then using this information to retrieve data from my database and I would like to reuse the information retrieved in my JavaScript code.
Here is my code :
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function() {
// HERE I would like to inctercept the data that my php script put in myFunctions.php produces and use it to generate content;
}
});
});
So basically, when clicking on the button #validerChoixDeCours, my code sends some data to myFunctions.php which generates a response stored in a php variable and I want to use that response in my JS code.
Thank you in advance for your help :)
Its actually quite easy!
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function(e) {
e contains the response from the php script
}
});
});
The first parameter of success contains the response from the request. so, in this case, the 'e' variable contains the output which you can use.
Add data variable
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function(data) {
alert(data);
}
});
});
make sure your server side code look like this
<?php
$output = "Any String or Array";
echo json_encode($output);
?>

how to select form id with jquery before sending out a form

I have several forms on my page with different IDs. Each form has a submit button and should be handled separately.
So the if i specify the form id the code below works fine, since I have x amount of forms which are not known in advance I need to make this script more general.
$(function() {
$('#form1').on('submit', function() {
$.ajax({
type: 'post',
url: 'post.php',
dataType: 'json',
data: $('#form1').serialize(),
success: function() {
console.log($('#form1').serialize());
}
});
});
});
I am able to get the form id with the scrpt below, but I cannot figure out how to combine with the script above.
$("form").submit(function() {
var myId = this.id;
alert(myId);
});
You need to store a reference to this, so that you can access the form element that triggered the submit event within the callback:
$('form').on('submit', function() {
var self = this;
$.ajax({
type: 'post',
url: 'post.php',
dataType: 'json',
data: $(self).serialize(),
success: function() {
console.log($(self).serialize());
}
});
});

Bootstrap modal popup and fadeout after few seconds according to ajax success

I am trying to display a bootstrap modal with success of a ajax request. Its working for me. But my problem is when I try to hide it after few seconds as soon as it triggered.
This is how I tried it.
$.ajax({
type: "POST",
url: "includes/process.php",
data: $(".banner-form").serialize(), // serializes the form's elements.
success: function(data)
{
$("#myModal").fadeTo(2000, 500).slideUp(500, function(){
$("#myModal").modal('close');
});
}
});
But this is not working properly when I submitting the form first time. After that when I submitting the form this modal is not popup. Can anybody tell me what is the reason for this?
Thank you.
Here it is,
$.ajax({
url: 'link/',
dataType: 'json',
success: function (s) {
$('#MyModal').modal({
show: false
});
},
error: function (e) {
}
});
This should work
$.ajax({
url: "test.html",
context: document.body
// wait until ajax request is done
}).done(function() {
// close the modal
$('#myModal').modal('hide')
// try this if above does not work!
// $('#modal').modal('toggle');
});

Ajax call multiple time onclick event on bootstrap modal

By clicking a button Its loaded a bootstrap modal. On the modal there have a form and on click save button I am trying to submit form by ajax call. At first time the ajax call trigger one time, but 2nd time the ajax url trigger two times. I see on firebug console section the post url is called multiple times.
Here Is my jquery code.
$(".show-modal").click(function() {
$('.upload-modal').modal();
$(".save-logo").click(function(e) {
e.preventDefault();
$.ajax({
type : "POST",
data : data,
contentType: false,
cache : false,
processData: false,
url : "../../io/upload/"
}).done(function(rawData) {
$('.modal-header .close').click();
})
});
})
The problem is that you have your .save-logo click handler inside the .show-modal click handler. So every time the modal is shown, you attach another click handler to the .save-logo element. The code below should fix that problem:
$(".show-modal").click(function () {
$('.upload-modal').modal();
});
$(".save-logo").click(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
data: data,
contentType: false,
cache: false,
processData: false,
url: "../../io/upload/"
}).done(function (rawData) {
$('.modal-header .close').click();
})
});
$(function(){
var editUserId;
$('#edit-user-modal').on('show.bs.modal', function(e) {
editUserId = $(e.relatedTarget).data('userid');
});
$("#edit-user-form").submit(function(event) {
event.preventDefault();
var form = $(this);
var route = "/edit-user/" + editUserId;
if(validateEditUserForm()){
$.ajax({
type: "POST",
url: route,
data: form.serialize(),
success: function(Response)
{
if(Response){
console.log(Response.status);
console.log(Response.message);
if(Response.status == 'success'){
$('#adduser-alert-box').hide();
$("#add-user-form")[0].reset();
$('#adduser-success-box').show();
$('#adduser-success-box').html(Response.message);
}
else{
console.log('User could not be added');
$('#adduser-alert-box').show();
$('#adduser-alert-box').html(Response.message);
}
}
else{
console.log('No Response');
$('#adduser-alert-box').show();
$('#adduser-alert-box').html('No Response');
}
}
});
}
else{
console.log('Validation Error');
$('#adduser-alert-box').show();
$('#adduser-alert-box').html('Please Fill All the Fields Correctly');
}
});});
I faced the same issue and randomly i tried something .
so if you create a global variable to store the 'id' you pass from your button then store the id from the 'data' attribute of your button then it won't make multiple requests when clicked since you have declared the variable globally!
Hope it helps.

jquery ajax sucess data into facebox

UPDATE: The code if working I has some css issues.
I'm trying to put ajax data into facebox modal box, I have the following code but facebox modal box is not loading. Looking into firebug ajax is returning the correct data but i do not know how to pass that data to facebox.
$('a[rel*=facebox]').live("click", function() {
var ajaxpostID=$(this).parent().attr("id"); //Get entry ID
$.ajax({
url: 'http://www.someurl.com/ajax/facebox-ajax.php',
type: "POST",
data: ({
ajaxpostID: ajaxpostID
}),
success: function(data) {
$.facebox(data);
},
error: function() {
$.facebox('There was an error.');
}
});
});
Something like this worked for me:
//added some id to anchor tag and
$('a[id='some_anchor_id']').live("click", function() {
var ajaxpostID=$(this).parent().attr("id"); //Get entry ID
jQuery.facebox(function() {
var form_data = {
ajaxpostID: ajaxpostID
};
$.ajax({
url: "http://www.someurl.com/ajax/facebox-ajax.php",
type: 'POST',
data: form_data,
success: function(data) {
jQuery.facebox(data);
},
error: function() {
$.facebox('There was an error.');
}
)
});
})
})
Hope it works for you

Categories

Resources