I'd like to send series["id"+i] to write.php. (This code is in another ajax request.)
Any idea?
series = new Object();
$(xml_node).find("Series").each(function (i) {
series["id" + i] = $(this).find("seriesid").text();
series["name" + i] = $(this).find("SeriesName").text();
series["banner" + i] = $(this).find("banner").text();
table += '<tr<td>' + series["banner" + i] + '</td>' + '<td>' + series["name" + i] +
'</td>' + '<td>' + '<button>Add show</button>' + '</td>' + '</tr>';
});
$('button').click(function addseries() {
$.ajax({
type: "POST",
url: 'write.php',
data: series["id" + i],
success: function (data) {
console.log(data);
}
});
});
...'<button data-id="' + i +'">Add show</button>'..
$('button').click(function addseries(e){
var i = $(e.currentTarget).attr('data-id');
$.ajax({
type: "POST",
url: 'write.php',
data: i,
success: function(data) {
console.log(data);
}
});
UPDATE:
data: {id: i}
or
data: 'id=' + i
You can get the event of what triggered the ajax call,
for example
$('button').click(function addseries(e){
console.log($(e.target));
}
where $(e.target) is the jQuery object that triggered the call.
You can also use $(this)(referring to the jQuery object that called the function).
so in our example,
$('button').click(function addseries(e){
console.log( $(this).attr('id') );
});
Related
i cant load data to edit dropdown menu chain from saved data with AJAX
all tutorial i tried search just about input chain dropdown menu autoselect AJAX to input, i need to show saved data in form EDIT
<script type="text/javascript">
$(document).ready(function() {
$("#provinsi").append('<option value="">Pilih</option>');
$("#kabupaten").html('');
$("#kecamatan").html('');
$("#kelurahan").html('');
$("#kabupaten").append('<option value="">Pilih</option>');
$("#kecamatan").append('<option value="">Pilih</option>');
$("#kelurahan").append('<option value="">Pilih</option>');
url = 'ajax/get_provinsi.php';
$.ajax({ url:
url,
type: 'GET',
dataType: 'json',
success: function(result) {
for (var i = 0; i < result.length; i++)
$("#provinsi").append('<option value="' + result[i].id_prov + '">' + result[i].nama + '</option>');
}
});
});
$("#provinsi").change(function(){
var id_prov = $("#provinsi").val();
var url = 'ajax/get_kabupaten.php?id_prov=' + id_prov;
$("#kabupaten").html(''); $("#kecamatan").html('');
$("#kelurahan").html(''); $("#kabupaten").append('<option value="18">Pilih</option>');
$("#kecamatan").append('<option value="">Pilih</option>');
$("#kelurahan").append('<option value="">Pilih</option>');
$.ajax({
url : url,
type: 'GET',
dataType : 'json',
success : function(result){
for(var i = 0; i < result.length; i++)
$("#kabupaten").append('<option value="'+ result[i].id_kab +'">' + result[i].nama + '</option>');
}
});
});
$("#kabupaten").change(function(){
var id_kab = $("#kabupaten").val();
var url = 'ajax/get_kecamatan.php?id_kab=' + id_kab;
$("#kecamatan").html(''); $("#kelurahan").html('');
$("#kecamatan").append('<option value="">Pilih</option>');
$("#kelurahan").append('<option value="">Pilih</option>');
$.ajax({
url : url,
type: 'GET',
dataType : 'json',
success : function(result){
for(var i = 0; i < result.length; i++)
$("#kecamatan").append('<option value="'+ result[i].id_kec +'">' + result[i].nama + '</option>');
}
});
});
$("#kecamatan").change(function(){
var id_kec = $("#kecamatan").val();
var url = 'ajax/get_kelurahan.php?id_kec=' + id_kec; $("#kelurahan").html('');
$("#kelurahan").append('<option value="">Pilih</option>');
$.ajax({
url : url,
type: 'GET',
dataType : 'json',
success : function(result){
for(var i = 0; i < result.length; i++)
$("#kelurahan").append('<option value="'+ result[i].id_kel +'">' + result[i].nama + '</option>');
}
});
});
</script>
i want to make dorpdown chained (selected) automaticly to view same dato from save before
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
All that I am trying is to iterate the data I have in the database using the Jquery each method, but couldn't figure when I am losing it all. Can someone look through this piece of code and tell me what I am doing wrong ?
$(function(){
var output = $("#folderOne");
$.ajax({
url: 'http://......',
dataType: 'json',
// data: {action: 'output'},
success: function(data, status){
console.log(data);
$.each(data, function(index, value) {
if (value.f_id == 1) {
var cards = "<div class='thumb'>" + value.f_id +
"</div><br>" + "<p>" + value.title + "</p> ";
}
output.append(cards);
});
},
I am Creating Inline editing webpage for my company
here i write jquery code for sending data to config.php file and save data there
Here is my jquery code:
<script src="<?=base_url()?>application/views/blackline/clients/jquery.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$('div.auto').click(function(){
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
$(this).addClass('ajax');
$(this).html('<input id="editbox" size="'+$(this).text().length+'" type="text" value="' + $(this).text() + '">');
$('#editbox').focus();
}
);
$('div.auto').keydown(function(event){
arr = $(this).attr('name').split(" ");
if(event.which == 13)
{
$.ajax({
type: "POST",
url:"/con.php",
data: "value="+$('.ajax input').val()+"&field="+arr[1]+"&table="+arr[0]+"&row="+arr[2],
success: function(data){
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
}});
}});
$('#editbox').live('blur',function(){
$('.ajax').html($('.ajax input').val());
$('.ajax').removeClass('ajax');
});
});
</script>
Every thing is working fine the only thing which I want to improve is when user changed data the new data should be highlighted for few seconds with yellow background
Try like this
$.ajax({
type: "POST",
url: "/pms/config.php",
data: "value=" + $('.ajax input').val() + "&field=" + arr[1] + "&table=" + arr[0] + "&row=" + arr[2],
success: function (data) {
$('.ajax').html('<span class="highlight" >' + $('.ajax input').val() + '</span>');
setTimeout(function () {
$(".higlight").removeClass("higlight");
}, 1000);
}
});
In your css , add this code
.highlight {
background-color: yellow;
}
Edit
$.ajax({
type: "POST",
url: "/pms/config.php",
data: "value=" + $('.ajax input').val() + "&field=" + arr[1] + "&table=" + arr[0] + "&row=" + arr[2],
success: function (data) {
$('.ajax').html($('.ajax input').val());
$(".ajax").css("background-color","yellow");
$(".ajax").addClass("higlight");
setTimeout(function () {
$(".ajax").removeAttr("style");
$(".higlight").removeClass("higlight");
}, 1000);
}
});
Use setTimeout() which calls a function or evaluates an expression after a specified number of milliseconds like shown below in the ajax response.
$('.ajax').show();
$('.ajax').html('<span style="background-color:yellow;">'+$('.ajax input').val()+'</span>');
setTimeout(function() { $('.ajax').hide(); }, 1000);
I have seen some other posts in Stackoverflow which were related. Tried that code but it did not work out for me.
actually i have a database with 2 images of an actress in my MYSQL database. I am generating JSON data using PHP and it works fine.
Link for JSON data
I am trying to parse it with Javascript as shown in this fiddle
Direct Parsing Fiddle Link
var json = [{
"id": "1",
"url": "http:\/\/i.imgur.com\/J8yqh3y.jpg"
}, {
"id": "2",
"url": "http:\/\/i.imgur.com\/WNx9i2c.jpg"
}];
var nazriya = json;
$.each(nazriya, function (index, nazriya_nazim) {
$('#url-list').append('<li>' +
'<h4>' + nazriya_nazim.url + '</h4>' +
'</li>');
});
and it works fine.
But if i try to parse it from my PHP file located in my domain. It does not display anything. It shows blank page.
FIDDLE Link : Parsing JSON data on PHP File
type: "POST",
dataType: 'json',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (data) {
alert("1");
//var obj = jQuery.parseJSON(idata);
var json = JSON.parse(data);
$.each(json, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
}
I did not write all these code on my own. Browsed Stack and found solutions. But stuck up at the last step (parsing from a PHP file located in my server).
As said by #DaGLiMiOuX tried it in a separate HTML document.
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script>
$.ajax({
type: "POST",
dataType: 'jsonp',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (data) {
alert("1");
var json = data;
$.each(data, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
},
error: function(jqXHR, status, errorText) {
alert('Status code: ' + jqXHR.status +
'\nStatus text: ' + status + '\nError thrown: ' + errorText);
}
});
</script>
</head>
<body>
<ul id="url-list"></ul>
</body>
Now also it shows the same error.
In your client side specified the jsonpcallback as below
$.ajax({
type: "POST",
dataType: 'jsonp',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
jsonpCallback: function(data){
alert('generate a specified jsonp callback');
return "jsonpCall";
},
success: function (data) {
alert("1");
var json = data;
$.each(data, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
},
error: function(jqXHR, status, errorText) {
alert('Status code: ' + jqXHR.status +
'\nStatus text: ' + status + '\nError thrown: ' + errorText);
}
});
In http://chipap.com/apps/nazriya_nazim/test1.php
<?php
$callback = $_GET["callback"]; // return "jsonpCall" that was specified in jsonpCallback ajax with jsonp
$json = '[{"id":"1","url":"http:\/\/i.imgur.com\/J8yqh3y.jpg"},{"id":"2","url":"http:\/\/i.imgur.com\/WNx9i2c.jpg"}]' ;
echo $callback.'('. $json.')' ;
?>
You can understand much better about jsonp at at http://en.wikipedia.org/wiki/JSONP
http://jsfiddle.net/channainfo/wn5bz/
1) that's just an extract of a code, not a compiling function. The complete code would be
$.ajax({
type: "POST",
dataType: 'json',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (obj) {
alert("1");
$.each(obj, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
}
});
2) you need to import jQuery (you don't do it in the fiddle)
XMLHttpRequest cannot load
http://chipap.com/apps/nazriya_nazim/test1.php. Origin
http://fiddle.jshell.net is not allowed by
Access-Control-Allow-Origin.
You have to handle ALWAYS posible errors.
Check this demo.
This should work, but you got Access-Control-Allow-Origin error. This is caused because your dataType was incorrect. Try this configuration for your ajax call:
$.ajax({
type: "POST",
dataType: 'jsonp',
url: "http://chipap.com/apps/nazriya_nazim/test1.php",
success: function (data) {
alert("1");
var json = data;
$.each(data, function (index, nazriya) {
$('#url-list').append('<li>' +
'<h4>' + nazriya.url + '</h4>' +
'</li>');
});
},
error: function(jqXHR, status, errorText) {
alert('Status code: ' + jqXHR.status +
'\nStatus text: ' + status + '\nError thrown: ' + errorText);
}
});
NOTE: You get in the demo an alert as if it were an error, but your status code is 200 (check status codes). Try it in your project. Maybe JsFiddle it's not allowing to return data from external servers.