Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my code: In A Function:

var daaa=document.getElementById('da').value= year+ "-" +month+ "-" +day;
    document.form1.bookingsession.focus();
var coords = {
    "lat": daaa
};
 $.get('bs.php', coords, function () {
    alert('data sent');
});

and my php code is:

<?php
$output = isset($_GET['lat']) ? $_GET['lat'] : 0;
print("$output");
?>

And when i print $output i get the value as 0, but actually have to get the value which is on tha variable daaa. Let me know where i made mistake.... Thank you in advance

share|improve this question
Break it down to the simplest request. Open developer tools console: $.get('bs.php', { lat: 50 }); - what does the request return? – Matt Stone Mar 20 at 10:27
It still return the value 0, even after i replaced my coding as $.get('bs.php', { lat: 50 }); – Ganesh Mar 20 at 11:04

3 Answers

Try to change

var daaa=document.getElementById('da').value= year+ "-" +month+ "-" +day;

to

var daaa=document.getElementById('da').value + "-" + year+ "-" +month+ "-" +day;

Anyway in such cases yours "number one" action should be check if variable is really set on client side. You can do it through alert(daaa) or console.log(daaa);

share|improve this answer
I want to send the value of variable daaa to server side to perform some validation... – Ganesh Mar 20 at 10:50

change this

var coords = {
"lat": daaa
};

to

var coords = {
lat: daaa
};

you misplaced the double quotes.

http://api.jquery.com/jQuery.get/

example from jquery

//$.get("test.php", { name: "John", time: "2pm" } );
share|improve this answer
I replace the quotes but still getting the same error, thank you for your help sir... – Ganesh Mar 20 at 10:22
@Ganesh How are you accessing bs.php? Means directly opening bs.php in browser. – Yogesh Suthar Mar 20 at 10:26
S sir by directly opening it in browser for checking its value... – Ganesh Mar 20 at 10:41
1  
Clearly the OP wants to send the value of the variable daaa to the server, so changing it to "daaa" would not really help. I don't know though it he question was changed after you answered. – Felix Kling Mar 20 at 10:42
By directly opening the bs.php in browser it will always give you 0 output. In your code you are sending ajax request to bs.php, so you can get the value of daaa from its response. And also see the edited answer. – Yogesh Suthar Mar 20 at 10:47
show 1 more comment

First of all do the action @coramba mentioned,

then modify your javascript code like this:

$.get('bs.php', coords, function (dataThatPhpPrints) {
    alert(dataThatPhpPrints);
});

..you will see that it actually returns the value that you have sent to PHP script and PHP scripts returns the print of that.

In order to assure yourself that your scripts prints something modify PHP code to like below:

<?php
$output = isset($_GET['lat']) ? $_GET['lat'] : 0;
print("$output - is response from the PHP!!!");
?>

Now when you run yor javascript, you will get an alert message like

ActualValueOfLATYouSentToPHP - is response from the PHP!!!

Bear in mind, if you want to test your PHP whether it works or not, you cant just simply open it in browser and wait for some magic to happen, as it uses $_GET to get some value meaning you need to provide it as a QueryString with the key being 'lat' :

yourhost/bs.php?lat=someTestValue
share|improve this answer
i get the value of daaa in alert box inaddition i receive some script that i return for validation is also shown in the alert, why that script is return on that alert? Thank u for your help sir... – Ganesh Mar 21 at 5:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.