var a = $('.level').find(":selected").attr('value');
var b = $('.category').find(":selected").attr('value');
var c = $('.topic').find(":selected").attr('value');
var d = $('.question').val();
$.ajax({
url: "http://localhost/2016/2016_02_15_Quiz/index.php/Admin/getthecompr/" + a + "/" + b + "/" + c,
type: "GET",
dataType: 'json',
data: '',
success: function(data) {
var id = data;
$.ajax({
url: "http://localhost/2016/2016_02_15_Quiz/index.php/Admin/insert4/" + id[0]['id_comprehens'] + "/" + d,
type: "GET",
dataType: 'json',
data: '',
success: function(data) {
alert("Success");
},
error: function() {
alert("Fail")
}
});
},
error: function() {
console.log('Paastoge');
}
});
});
I got trouble making this nested-JQuery-Ajax. In the first Ajax, I take the value of compre_id that I want to use in second Ajax. In the second Ajax, I will insert the id_comprhensive and question in MySQL table. The Problem is, inserting data to SQL table is success. But, instead of displaying "success", fail alert does appear. Is there something problem with this code ?
Related
Is it possible to work with a response from AJAX request in PHP? I am not really a JS dev so i am polling my hair out with this one.
I have sort of hacked this together:
var base_url = 'http://dev.local/westview/public';
$('select.child_id').change(function() {
var child_id = $('#child_id');
var dataString = 'child_id=' + child_id;
$.ajax({
type: "POST",
url: base_url + "/finance/payment-history",
data: dataString,
dataType: 'html',
success: function(html) {
alert(html);
},
});
return false;
});
The function appears to work ok, it gives me an alert with the correct data.
{"payments":[{"id":"19","child_id":"21","club":"Breakfast Club","term":"Half Term 3","amount":"15.00","pdate":"2015-02-25","notes":"","created_at":"2015-02-11 12:16:32","updated_at":"2015-02-11 12:16:32","starting_debt":"0","debt_start_date":"2015-01-05"},{"id":"20","child_id":"21","club":"After School Club","term":"Half Term 3","amount":"11.50","pdate":"2015-02-25","notes":"","created_at":"2015-02-11 12:16:49","updated_at":"2015-02-11 12:16:49","starting_debt":"0","debt_start_date":"2015-01-05"}]}
I need to be able output this to the user so that it is readable. A lot of guides I find describe replacing data but as it stands there is no data until a child_id is selected.. i then want it show the above data in a readable way.
I have no idea how to start working with the data in my view file(php).
Thanks
[EDIT]updated with working code:
var base_url = 'http://dev.local/westview/public';
$('select.child_id').change(function() {
var response = "";
var child_id = $('#child_id').val();
var dataString = 'child_id=' + child_id;
$.ajax({
type: "POST",
url: base_url + "/finance/payment-history",
data: dataString,
success: function(response) {
var json_obj = $.parseJSON(response);
var output = "<ul>";
for (i=0; i < json_obj.payments.length; i++)
{
var payment = json_obj.payments[i];
var date = moment(payment.pdate).format('Do MMM YYYY');
output += "<li>£" + payment.amount + " - " + date + " (" + payment.club + ")</li>";
}
output += "</ul>";
$('.history-section').html(output);
},
dataType: "html"
});
});
Do like this.
var data = $.parseJSON("your_json");
var output= "<ul>";
for (i=0; i < data.payments.length; i++){
output += "<li>" + data.payments[i].id + ", " + data.payments[i].child_id + "</li>";
}
output += "</ul>";
use
dataType: 'json',
instead
dataType: 'html',
and then use each to fetch the record from response in success function
Use $.parseJSON() For Convert Json Format Data To Array
Right code at sucess of ajax..
Like,
var data = $.parseJSON(html);
data in you get array format of responce
You need to use json_encode() in your php file to send the data back as an array
For example;
$myarray = array("data1"=>"value1","data2"=>"value2");
echo json_encode($myarray);
You can then access the data separately in the js file like this;
success: function(html) {
alert(html.data1);
alert(html.data2);
},
You also need to change the dataType to 'json'
$('input[name=\'product_attribute[' + attribute_row + '][name]\']').catcomplete({
delay: 0,
source: function(request, response) {
$.ajax({
url: 'index.php?route=catalog/attribute/autocomplete&token=<?php echo $token; ?>',
type: 'POST',
dataType: 'json',
data: 'filter_name=' + encodeURIComponent(request.term),
success: function(data) {
response($.map(data, function(item) {
return {
category: item.attribute_group,
label: item.name,
value: item.attribute_id
}
}));
}
});
},
select: function(event, ui) {
$('input[name=\'product_attribute[' + attribute_row + '][name]\']').attr('value', ui.item.label);
$('input[name=\'product_attribute[' + attribute_row + '][attribute_id]\']').attr('value', ui.item.value);
return false;
}
});
You Can map your json something like this
I need the content of the Variable data from this AJAX Request but in a PHP Variable.
Is that possible?
$.ajax({
type: 'POST',
async: false,
url: '/php/checkAccountData.php',
data: 'un=' + PasswordResetUsernameText + '&e=' + PasswordResetEmailText,
success: function(data){
var responseanswer = data;
alert(responseanswer);
setTimeout(function(){
$("img[name=UserNameEmailText]").fadeTo(500,0);
$("div[id=PasswordResetLoadingDiv]").fadeOut(500)}, 1500);
setTimeout(function(){
$("img[name=UserNameEmailComplete]").fadeTo(500,1)}, 2500);
setTimeout(function(){
$('MainFrame').fadeTo(500,1);
$('#LoginField').fadeOut(500, "swing")}, 7000000);
}
i have problem with my rate, my code like below
$(function(){
var href = jQuery(location).attr('href');
$('.rate-it').rating({
required: true,
callback: function(value, link){
$.ajax({
type: "POST",
url: "<? echo base_url('post/rate/'); ?>",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
success: function(msg){
$('.rate-it').attr('disabled', true);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
displayError();
}
});
},
how to set people to can not vote repeatedly?
i'm using codeigniter
$(function(){
var href = jQuery(location).attr('href');
$('.rate-it').rating({
required: true,
callback: function(value, link){
$.ajax({
type: "POST",
url: "<? echo base_url('post/rate/'); ?>",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
success: function(msg){
$('.rate-it').attr('disabled', true);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
displayError();
}
});
},
focus: function(value, link){
var tip = $('#rate-result');
tip[0].data = tip[0].data || tip.html();
tip.html(link.title || 'value: '+value);
},
blur: function(value, link){
var tip = $('#rate-result');
$('#rate-result').html(tip[0].data || '');
}
});
});
If you want someone not to be able to vote repeatedly, you have to disable the button AND check on the server side that this client didn't vote yet.
Otherwise one could intercept the ajax request and repeat it.
You can try it :
In firefox or chrome, browse to your site, type F12, go to the console menu and copy the following :
$.ajax({
type: "POST",
url: "[your url here]",
dataType: "json",
data: "&postlink=" + href + "&ratevalue=" + value,
});
Launching it with ctrl + enter, you send a vote request.
Now put that in a for loop, and you have tons of votes.
Globally, keep in mind that you never clean user input on the client side.
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.
}
I need to receive the status of vote using ajax and php and jquery. Following is my code :
var VoteStatus= GetStatus() ;
var ID = $('#ID').val();
function GetStatus() {
var res = '';
$.ajax({
type: "POST",
data: {VoteID:ID} ,
url: "/vote_status.php",
async: false,
success: function(result) { res=result; }
});
return res;
}
alert('Vote Status= ' + VoteStatus);
In my php file:
$VoteID = $_POST['VoteID'];
$Property = $_POST['Property'];
if ( $VoteID == 0 )
echo 'No Vote provided - Property = '. $Property;
exit;
The alert box shows: Vote Status = No Vote Provided
Please help.
I have posted the VoteID, but the php file doesn't seem to receive it.
Try the alert in here and check if its working
$.ajax({
type: "POST",
data: {"VoteID":ID} ,
url: "/vote_status.php",
async: false,
success: function(result) {
alert(result); }
});
The name of the POST variable needs to be in quotes, as in
data: {"VoteID":ID}
Try this and check jquery ajax manuals
$.ajax({
type: "POST",
data:"VoteID=" + ID +"&secondparam=" + secondvalue,
url: "/vote_status.php",
async: false,
success: function(result) { alert(result); }
});