0

I have the ajax call:

$.ajax({
    url: '/articles/moveArticle/' + article.id,
    type: 'POST',
    dataType: 'json',
    success: function(result) {
    console.log(result);
         //something
    },
    error: function (result) {
    }
});

The php function that gets called by the ajax is:

function moveArticle ($articleId) {
  // move it
}

This works great but how do I send more than 1 parameter?

1
  • You must have some kind of framework in between that translates the ajax call to a function call. Otherwise you'd never end up with a nice argument for your function (you would have to obtain it from $_GET). Might be best if you provide that info as well. Commented Apr 9, 2014 at 12:07

3 Answers 3

2

You can create an object and then pass it in the data property of ajax call using JSON.stringify(data)

var data ={
    id : article.id,
    name : article.name // other parameters
} 

$.ajax({
    url: '/articles/moveArticle',
    type: 'POST',
    dataType: 'json',
    data = JSON.stringify(data),
    success: function(result) {
    console.log(result);
         //something
    },
    error: function (result) {
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

This is an incorrect answer for 2 reasons. It is incorrectly formatted because of the "=" sign and also jQuery does stringify an object by default, there is no need to call JSON.stringify, just pass an object to it as in my answer.
1

By the function moveArticle, I guess you use some php framework.
Without knowing which one - it is impossible to answer.
From the structure of the function - i assume that it uses routing - so it's not a POST request (that is why i ignored the use of data as others mentioned).

So, my best guess is:

$.ajax({
    url: '/articles/moveArticle/' + article.id + '/' + anotherParameter,
    type: 'POST',
    dataType: 'json',
    success: function(result) {
    console.log(result);
         //something
    },
    error: function (result) {
    }
});


function moveArticle ($articleId, $theOtherParameter) {
  // move it
}

1 Comment

You can omit type: 'POST' as ajax uses GET by default.
1

Use data parameter in your ajax call.

https://api.jquery.com/jQuery.ajax/

EDIT

As I read other answers I have decided to enhance my answer.

dataType parameter as stated in jQuery documentation stands for:

The type of data that you're expecting back from the server.

Which means you are expecting JSON data from the server. If you set dataType to json it does NOT mean you are sending a JSON object to the server.

There is a parameter in ajax call that you have set, it is type. This parameter states how you are going to send the data to server. You have used a POST method. It means that anything set in the data is accessible by PHP from the $_POST array

$.ajax({
    url: '/articles/moveArticle/' + article.id,
    type: 'POST',
    dataType: 'json',
    data: {
        something: 'Something that can be accessed by PHP as $_POST["something"]',
    },
    success: function(result) {
    console.log(result);
         //something
    },
    error: function (result) {
    }
});

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.