1

my ajax is:

add = 'request';
full_val = 'barrack obama';
$.ajax({
        url: 'plugins/add_friend.php',
        data: full_val+'='+add,
        success: function(data)
        {

        }
        });

if the javascript variable value changes depending on the conditions, then how will i $_GET[] the variable full_val? I want it to be something like:

$_GET[full_val]

is there a way to pass the variables of javascript to php?

0

5 Answers 5

2

If you want the literal index full_val, then use it in the query string:

data: 'full_val='+add,

So that in PHP, you'd be able to use $_GET['full_val']

Alternatively, you could also put an object in that field:

data: {full_val: full_val, add: add},

Here is the description:

data
Type: PlainObject or String or Array
Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests.

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

1 Comment

@monkeycoder sure man no prob, we're all noob when we first time use something :) im glad this helped
1

Your data just has the values and does not have the keys. See below for how to pass the keys and values.

add = 'request';
full_val = 'barrack obama';
$.ajax({
    url: 'plugins/add_friend.php',
    data: {full_val:full_val,add:add},
    success: function(data)
    {

    }
});

In your PHP use $_GET['full_val'];

Comments

1

send your values with data parameter correctly and add type:get to define GET method

add = 'request';
full_val = 'barrack obama';
$.ajax({
  type: "get",
  url: 'plugins/add_friend.php',
  data: {'full_val':full_val,'add':add},
  success: function(data) {

  }
});

Then you will get values on php :-

use $_GET['full_val'] rather $_GET[full_val]

Comments

0

Try Using this

add = 'request';
full_val = 'barrack obama';
$.ajax({
        url: 'add.php',
        data: 'full_val='+add,
        success: function(data)
        {
           alert(data);
        }
        });

Comments

0

You can use the .get() method. The variables you can set using an object literal. For example:

$.get("/plugins/add_friend.php", {
    add:  "request",
    full: "Barrack Obama"
}).done(function(data) {
    console.log("Status:", data.status);
    console.log("Received:", data.received);
});

And the PHP could be done something like this:

$add  = filter_input(INPUT_GET, 'add', FILTER_SANITIZE_STRING);
$full = filter_input(INPUT_GET, 'full', FILTER_SANITIZE_STRING);
echo json_encode(array(
    'status': 'OK',
    'received': "{$add} and {$full}",
));

Comments

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.