kingnutter Posted June 5, 2010 Share Posted June 5, 2010 Hi everyone, In the code below, the first "alert(prev_track);" works fine, but the second doesn't. I want to use it in the response to the add_track_to_db post later but the value just seems to disappear. Can anyone tell me how to retain the variable to use it later in my script? Many thanks, KN <script type="text/javascript" charset="utf-8"> $('input[type="button"]').click(function(event) { var $target = $(event.target); var moj_id = $('#moj_id').val(); var track_no = $('#track_no').val(); var track_title = $('#track_title').val(); var track_artist = $('#track_artist').val(); var track_asinuk = $('#track_asinuk').val(); var track_asinus = $('#track_asinus').val(); $.post("../includes/get_order.php", { 'moj_id' : moj_id, 'track_no' : track_no, }, function(data){ var prev_track= data.prev_track; // John alert(prev_track); // THIS ONE WORKS }, "json"); alert(prev_track); // THIS ONE DOESN'T $.post('../includes/add_track_to_db.php', { 'moj_id' : moj_id, 'track_no' : track_no, 'track_title' : track_title, 'track_artist' : track_artist, 'track_asinuk' : track_asinuk, 'track_asinus' : track_asinus, }, function(r) { console.log("hello" + prev_track); // NO RESPONSE HERE $('<div class="overlay"></div>') .appendTo('#form_block') .fadeIn(1000, function() { // SCRIPT STOPS $('<li>' + r + '</li>') //.hide() .appendTo("#tracks li:eq('+prev_track+')")//('#tracks') .show(1000, function() { if ($target.is('#submit1')) { $('#form_block').slideUp(1000, function() { $('.overlay').hide(); }); }; if ($target.is('#submit2')) { $('.overlay').fadeOut(1000); }; // clears form document.getElementById('form').reset(); }); }); }); return false; // disable submit click }); </script> Link to comment https://forums.phpfreaks.com/topic/203948-how-do-i-use-a-json-callback-variable-later-in-my-script/ Share on other sites More sharing options...
dabaR Posted June 5, 2010 Share Posted June 5, 2010 I'd try var prev_track; on the top, then just prev_track= data.prev_track; later in the anon. function. Hi everyone, In the code below, the first "alert(prev_track);" works fine, but the second doesn't. I want to use it in the response to the add_track_to_db post later but the value just seems to disappear. Can anyone tell me how to retain the variable to use it later in my script? Many thanks, KN <script type="text/javascript" charset="utf-8"> $('input[type="button"]').click(function(event) { var $target = $(event.target); var moj_id = $('#moj_id').val(); var track_no = $('#track_no').val(); var track_title = $('#track_title').val(); var track_artist = $('#track_artist').val(); var track_asinuk = $('#track_asinuk').val(); var track_asinus = $('#track_asinus').val(); $.post("../includes/get_order.php", { 'moj_id' : moj_id, 'track_no' : track_no, }, function(data){ var prev_track= data.prev_track; // John alert(prev_track); // THIS ONE WORKS }, "json"); alert(prev_track); // THIS ONE DOESN'T $.post('../includes/add_track_to_db.php', { 'moj_id' : moj_id, 'track_no' : track_no, 'track_title' : track_title, 'track_artist' : track_artist, 'track_asinuk' : track_asinuk, 'track_asinus' : track_asinus, }, function(r) { console.log("hello" + prev_track); // NO RESPONSE HERE $('<div class="overlay"></div>') .appendTo('#form_block') .fadeIn(1000, function() { // SCRIPT STOPS $('<li>' + r + '</li>') //.hide() .appendTo("#tracks li:eq('+prev_track+')")//('#tracks') .show(1000, function() { if ($target.is('#submit1')) { $('#form_block').slideUp(1000, function() { $('.overlay').hide(); }); }; if ($target.is('#submit2')) { $('.overlay').fadeOut(1000); }; // clears form document.getElementById('form').reset(); }); }); }); return false; // disable submit click }); </script> Link to comment https://forums.phpfreaks.com/topic/203948-how-do-i-use-a-json-callback-variable-later-in-my-script/#findComment-1068205 Share on other sites More sharing options...
kingnutter Posted June 5, 2010 Author Share Posted June 5, 2010 Cheers I'll give that a go. By "at the top" do you mean where I list the other variables? And also, where is the anon.function? Link to comment https://forums.phpfreaks.com/topic/203948-how-do-i-use-a-json-callback-variable-later-in-my-script/#findComment-1068260 Share on other sites More sharing options...
kingnutter Posted June 5, 2010 Author Share Posted June 5, 2010 For anyone following this thread, I have sorted this problem by embedding the second .post in the first, so that the "json" type is just before the "return false;". Link to comment https://forums.phpfreaks.com/topic/203948-how-do-i-use-a-json-callback-variable-later-in-my-script/#findComment-1068338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.