Pass javascript variables into PHP use AJAX -


i have 1 javascript variable , want pass php file. search , find use ajax don't know how use ! here code . wrong ? .js file :

var message1 = message.message;          var jq = document.createelement('script');         jq.src = "https://code.jquery.com/jquery-1.10.2.js";         document.queryselector('head').appendchild(jq);              $(document).ready(function() {                $.ajax({                         type: "post",                         url: 'http://localhost/a.php',                         data: { newmessages : message1 },                         success: function(data)                         {                             alert("success!");                         }                     });          }); 

my a.php file :

<?php   if(isset($_post['newmessages']))   {       $uid = $_post['newmessages'];       echo $uid;   } ?> 

listen onload event of asynchronously loaded script , execute function in callback function.[ref]

try this:

function loadscript(src, callback) {   var s,     r,     t;   r = false;   s = document.createelement('script');   s.type = 'text/javascript';   s.src = src;   s.onload = s.onreadystatechange = function() {     if (!r && (!this.readystate || this.readystate == 'complete')) {       r = true;       callback();     }   };   t = document.getelementsbytagname('script')[0];   t.parentnode.insertbefore(s, t); }  var message1 = "my message"; loadscript('https://code.jquery.com/jquery-1.10.2.js', function() {    $(document).ready(function() {     $.ajax({       type: "post",       url: 'http://localhost/a.php',       data: {         newmessages: message1       },       success: function(data) {         alert("success!");       }     });   }); }) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -