0

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?

4
  • 2
    selectroute is the name that you're sending not travelling-from Commented Mar 11, 2015 at 16:43
  • also GET should be used to get data, use POST to send data. Commented Mar 11, 2015 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. Commented Mar 11, 2015 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. Commented Mar 12, 2015 at 1:34

2 Answers 2

3

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']

Sign up to request clarification or add additional context in comments.

9 Comments

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 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.
@Sarah92, you should undo the edit you made a few minutes ago as well.
how do I undo my edit as I do not have the original code I posted anymore?
@Sarah92, you can view the edit history and copy and paste from the previous version.
|
0

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>

4 Comments

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...:(
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?
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
Haha, you are of course right. I'll update the example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.