michaelfurey Posted April 18, 2017 Share Posted April 18, 2017 I am trying to echo a javascript variable with php but it doesn't work. That's my code. <script> $.ajax( { url: 'dendax_trans.php', type: 'POST', dataType: 'text', data: {latitude: '7.15588546', longitude: '81.5659984458'}, }); </script> <?php $lat = $_REQUEST['latitude']; $lon = $_REQUEST['longitude']; echo 'latitude- '.$lat . ', longitude- ' . $lon; ?> dendax_trans.php is the name of the php file and the javascript code is located into that php file. The result is unfortunately that: latitude- , longitude- Someone have any idea? Quote Link to comment Share on other sites More sharing options...
requinix Posted April 18, 2017 Share Posted April 18, 2017 What does var_dump(ini_get("request_order"), ini_get("variables_order"));show? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 18, 2017 Share Posted April 18, 2017 You are using Jquery(!) to place some value into the POST array that will be submitted by the form. So - have you actually created those fields in the html form? Perhaps a couple of 'hidden' input tags with the appropriate name= attributes? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 18, 2017 Share Posted April 18, 2017 the point of using ajax is so that the page isn't refreshed due to the request. your code is probably (assuming that the jquery library is being loaded) submitting the data, which will cause the entire page to be returned as a response to the ajax request. however, since your ajax code isn't doing anything with the returned response, nothing happens. the output you are seeing when the page is first loaded is what the data values are at that point in time. you would need to add some javascript inside the .ajax method call to do something with the response and your php code would need to detect the post request and ONLY return the expected result, not the entire page (you may want to consider using two separate pages until you understand how ajax works.) 1 Quote Link to comment Share on other sites More sharing options...
michaelfurey Posted April 18, 2017 Author Share Posted April 18, 2017 What does var_dump(ini_get("request_order"), ini_get("variables_order"));show? string(2) "GP" string(4) "GPCS" Quote Link to comment Share on other sites More sharing options...
requinix Posted April 18, 2017 Share Posted April 18, 2017 Yeah, forget what I said. Try this as a demo: <?php if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && $_SERVER['REQUEST_METHOD'] == 'POST') { // using $_REQUEST is generally discouraged by the PHP community // $_GET or $_POST specifically are recommended instead $lat = $_POST['latitude']; $lon = $_POST['longitude']; echo 'latitude-' . $lat . ', longitude-' . $lon; exit; // stop executing } ?> <html> <body> <script type="text/javascript"> $.ajax( { url: <?=json_encode($_SERVER['REQUEST_URI'])?>, type: 'POST', dataType: 'text', data: {latitude: '7.15588546', longitude: '81.5659984458'}, success: function(data) { alert(data); } }); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.