javascript - On ajax success set variable and use the variable in jquery outside ajax -
aim set (change) html hidden input field value 0 after successful execution of ajax.
at first decided on ajax success define variable , latter use outside ajax.
but read (jquery - how use return value of ajax call outside ajax call) impossible.
need find solution.
below code
ajax (as example)
<script language="javascript" type="text/javascript"> function ajax_post(){ if (window.xmlhttprequest) { var hr = new xmlhttprequest(); } else { var hr = new activexobject("microsoft.xmlhttp"); } var url = "_autosave.php"; var sabt = document.getelementbyid("date_day1").value; var prao = document.getelementbyid("amount1").value; var vars = "date_day1="+sabt+"&amount1="+prao; hr.open("post", url, true); hr.setrequestheader("content-type", "application/x-www-form-urlencoded"); hr.onreadystatechange = function() { if(hr.readystate == 4 && hr.status == 200) { var return_data = hr.responsetext; document.getelementbyid("status").innerhtml = return_data;
here decided define variable use outside ajax. read not work because script runs on variable , reaches outside ajax variable before ajax variable set.
var ajax_post_success = 1;
remaining part of code
} } hr.send(vars); // execute request document.getelementbyid("status").innerhtml = "processing..."; } </script>
then latter (if ajax success set value 0)
<input type="hidden" name="is_row_changed1" id="is_row_changed1" value="" > <script> $(document).ready(function() { if(ajax_post_success == 1) { document.getelementbyid('is_row_changed1').value = 0; } }); </script>
aim of following.
i plan use table 10 rows , 19 input fields in each row.
if user enters in of fields, value of hidden input field changes 1 (this ok).
then ajax insert/update user input.
after successful insert/update set hidden field value 0.
on each php insert/update execution check if hidden field value 1. if value 1 insert/update row. if value 0 nothing row. insert/update rows hidden field value 1.
that aim of question.
what solution?
i don't think need have variable, can change value of hidden field is_row_changed1
0 in onreadystatechange
callback of ajax method
hr.onreadystatechange = function() { if (hr.readystate == 4 && hr.status == 200) { var return_data = hr.responsetext; document.getelementbyid("status").innerhtml = return_data; document.getelementbyid('is_row_changed1').value = 0; } }
i suggest using jquery ajax like
function ajax_post() { var sabt = $('#date_day1').val(); var prao = $('#amount1').val(); $.ajax({ url : "_autosave.php", data : { date_day1 : sabt, amount1 : prao } }).done(function(html) { $('#status').html(html); $('#is_row_changed1').val(0); }); }
Comments
Post a Comment