Jump to content

echo javascript variable with php


michaelfurey

Recommended Posts

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.)

  • Like 1
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.