0

I have some difficulties to understand how to build a set of data values that I can use in an Ajax call (using jQuery). I have a function that generates some data values that I want to use in the Ajax call. In the code below I have simplified the get_data function to just return a string of data entries:

// get_data() will return a string:
// --> stringA: 'This is A', stringB: 'This is B', stringC: 'This is C'
function get_data() {
    var data = "";
    data += "stringA: 'This is A',";
    data += "stringB: 'This is B',";
    data += "stringC: 'This is C'";
    return data;
}

var my_variables = get_data();

$.ajax({
        url: "my_ajax_handler.php",
        data: {
            firstString: "Hello World!", 
            my_variables  // I would like this to expand to: stringA: 'This is A', stringB: 'This...etc'
        },
        type: "POST",
        success: function(result){ 
            document.write(result);
        }
    });

This will obviously not work, since the my_variables is a clean string and won't be treated as three separate data values. In the my_ajax_handler I have the following code:

<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>

This will result in the following output:

Array
(
    [firstString] => Hello World!
    [my_variables] => stringA: 'This is A',stringB: 'This is B',stringC: 'This is C'
)

I.e. the data values are treated as one single string. What do I need to do to get the following output?

Array
(
    [firstString] => Hello World!
    [stringA] => This is A
    [stringB] => This is B
    [stringC] => This is C
)
4
  • Make data like this : data: {"firstString" : "Hello World!", "stringA" : "This is A", "stringB" : "This is B","stringC" : "This is C"},. No need of get_data() in that case Commented Feb 18, 2021 at 9:09
  • 3
    If you have control over the getData() function, change it to return an object instead of a string. Commented Feb 18, 2021 at 9:10
  • 1
    Also, if this is a pure JS problem, please don't tag it with PHP. Otherwise, please share more details about how PHP is involved Commented Feb 18, 2021 at 9:19
  • @NicoHaase, You're right... I removed the PHP tag. It wasn't necessary to use that tag :) Commented Feb 18, 2021 at 9:27

2 Answers 2

1

try to use this:

function get_data(data) {
    data['stringA'] = "This is A";
    data['stringB'] = "This is B";
    data['stringC'] = "This is C";
    return data;
}

var myPostData = {};

myPostData['firstString'] = "Hello World!";
myPostData = get_data(myPostData);

$.ajax({
    url: "my_ajax_handler.php",
    data: myPostData,
    type: "POST",
    success: function(result){
        document.write(result);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

I think the best solution is to send the data this way, any value within a cell in an array

function get_data() {
   var data = [];
    data['stringA'] = 'This is A';
    data['stringB'] = 'This is B';
    data['stringC'] = 'This is C';
    return data;
}

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.