Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to pass a Javascript variable to a PHP file using AJAX.

I have the below Javascript code;

<script type="text/javascript">
var route_id = 'travelling-from'; //Route ID

$('#'+route_id).change(function(e) {
    //Grab the chosen value on route change
    var selectroute = $(this).val();

    $.ajax({
        type: "GET",
        url: 'ajax-getvalues.php',
        data: { selectroute : selectroute }
    });
});
</script>

In my ajax-getvalues.php, I have;

$selectroute = mysqli_real_escape_string($connection, $_GET['travelling-from']);

When I try to use $selectroute, it seems to be empty.

Do I need to add something else in order for this to work? Or have I gone wrong at some point?

share|improve this question
2  
selectroute is the name that you're sending not travelling-from –  Musa Mar 11 at 16:43
    
also GET should be used to get data, use POST to send data. –  LJ_1102 Mar 11 at 16:53
1  
POST should be used when the client is sending data that will change the server's state (like a database update). GET may or may not be appropriate here as we have no idea what ajax-getvalues.php does. –  robbmj Mar 11 at 16:57
    
@robbmj you're right, I somehow was assuming this would update session data but it may aswell be a simple search query. –  LJ_1102 Mar 12 at 1:34

2 Answers 2

up vote 3 down vote accepted

When I try to use $selectroute, it seems to be empty

The AJAX request will be sent to ajax-getvalues.php with the query string:

?selectroute=somevalue

In PHP you are trying the get the value of a parameter called travelling-from, this parameter does not exist in the query string.

You need to change selectroute to travelling-from

$.ajax({
    type: "GET",
    url: 'ajax-getvalues.php?travelling-from=' + encodeURIComponent(selectroute)
});

Or of you prefer:

$.ajax({
    type: "GET",
    url: 'ajax-getvalues.php',
    data: {"travelling-from": encodeURIComponent(selectroute)}
});

This produces the query string ?travelling-from=somevalue which can now be accessed with $_GET['travelling-from']

share|improve this answer
    
Thanks for your suggestion, I added in the encodeURIComponent and I thought this had solved it but I am still having the same issue - it seems the selection value is being sent but not received by the PHP file - that is my guess anyway..? –  Sarah92 Mar 12 at 16:15
    
@Sarah92 my answer addressed your original question (why you couldn't get the value of travelling-from in PHP). Since then you have modified the question such that you are not asking the same question any more. I suggest that you roll-back to the original question and start a new one for this new issue. –  robbmj Mar 12 at 16:20
    
Ok I will start a new question thanks :) –  Sarah92 Mar 12 at 16:21
    
@Sarah92, you should undo the edit you made a few minutes ago as well. –  robbmj Mar 12 at 16:22
    
how do I undo my edit as I do not have the original code I posted anymore? –  Sarah92 Mar 12 at 16:24

In your example the key should be route_id instead of selectroute

<script type="text/javascript">
var route_id = 'travelling-from'; //Route ID

$('#'+route_id).change(function(e) { //Grab the chosen value on route change var selectroute = $(this).val();

var data = {};
data[route_id] = selectroute;
$.ajax({
                type: "GET",
                url: 'ajax-getvalues.php',
                data: data }
            }); </script>
share|improve this answer
    
Thanks for your response - I tried this suggestion and had no luck. It seems the value is being sent but not received in the PHP file...:( –  Sarah92 Mar 12 at 16:14
    
That's weird, just to be sure, you verified via console that that AJAX request is correct, right? But $_GET['travelling-from'] should then do the trick on the PHP side of things. Is there any proxy or something in between which might swallow the HTTP param? –  SGD Mar 12 at 16:57
    
Your solution plainly does not work route_id is interpreted literally as the value route_id which would create a query string in the form of ajax-getvalues.php?route_id=somevalue –  robbmj Mar 13 at 20:05
    
Haha, you are of course right. I'll update the example. –  SGD Mar 13 at 20:13

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.