how to pass javascript callback response into php -


how pass callback javascript response php if let's i'm in single php file ?. here's sample snippet of e.g test.php

<html> <head> <script> var _somestuff = _somestuff || [];             (function () {                 _somestuff.push(['id', blahblah], ['setapikey',   "blahblah"]                  );             var d = document, g = d.createelement('script'), s = d.getelementsbytagname('script')[0];             g.type = 'text/javascript';             g.defer = true;             g.async = true;             g.src = 'blahblah.js';             s.parentnode.insertbefore(g, s);         })();                function mycallback(response)             {                  if (response !== undefined) {                     var data = json.parse(response);                     var item_ids = data.items;                     console.log(item_ids);                 }             }      _somestuff.push(['blahblah',"001124","blahblah","mycallback"]); </script> </head> <body> </body> </html>   

what want happen is, display data or item_ids inside html body using php.. how that?, i'm in single page, don't want use ajax or whatever,is possible ?

if php , js in same page

  • you need use ajax (to send post same page)
  • in order make php respond data not same whole document need put exit; in last line of php code
  • (your php code has @ top of same document.)

pseudo:

<?php     if ($_server['request_method'] === 'post') {        if(isset($_post["somedata"])) {            // stuff somedata            // return /*anything need*/; // return collected ajax success        }     }     exit; // prevents ajax return whole document (with html etc) ?> <html> <head> <script>      // js ajax post somedata php (use same url)      // on success use returned data php's `return` </script> </head> <body> </body> </html>   

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -