1

I have a JAVASCRIPT array that looks like this:

postarray['min_price'] = 120000;
postarray['max_price'] = 150000;

I'm trying to pass this to an AJAX call via jQuery .post function so that the .PHP file gets it in this format:

$_REQUEST['query']['min_price'] = 120000;
$_REQUEST['query']['max_price'] = 150000;

So far I've tried:

$.post("ajax_findproperties.php", {query: postarray},
        function(data){
            // processing function with JSON result
        }
    ,'json');

But I've had no luck. I even tried changing the var postarray to query and then tried query.serialize() in place of the bracketed variable block, but with no luck either.

When I check my status on Firebug, the AJAX call has absolutely no POST vars set whatsoever - complete blank.

3
  • It is probably possible, but why not just query_min_price, query_max_price? Commented Jan 7, 2012 at 21:54
  • Because it's easier on my PHP side if all this is unified within an array. The original form had name="query[min_price]" in there... Trying to not change too much. Commented Jan 7, 2012 at 21:56
  • Also an odd note: when I alert postarray.length, it returns 0. But when I do a for(x in postarray) loop and alert the x and postarray[x], the variables & array keys come up just fine. What's up with that? Commented Jan 7, 2012 at 22:01

3 Answers 3

2

The javascript array is not an array, it's an object. Define it before:

var postarray = {};
postarray['min_price'] = 120000;
postarray['max_price'] = 150000;

or replace with:

var postarray = {
    min_price: 120000,
    max_price: 150000
};

Now the JSON.stringify works:

alert(JSON.stringify(postarray));

Also see this example.

But this object should also be send without JSON.stringify():

$.post("ajax_findproperties.php", {query: postarray}, ... );
Sign up to request clarification or add additional context in comments.

1 Comment

Best answer so far! I'm trying it, so far it shows positive signs.. just a few more tweaks to make sure it's all correct...
0

Have you tried converting it with JSON.stringify(); and then doing a json_decode(...); in the PHP script?

5 Comments

I tried JSON.stringify(postarray) and that returned [] only, no strings or vars whatsoever.
You should probably change it into a proper object rather than an array: query.min_price = 120000; and query.max_price = 150000;. If you insist on keeping it in array format, rather than object format, you have to tell jQuery about it by adding brackets to the variable name: {'query[]': postarray}, although I'm not sure that will handle non-integer indices properly.
In that case, how do I generate the query.min_price part from the form data? They are not pre-set. They are determined by the name of the input field being used, and more or less may be added dynamically. query.min_price, query.max_price, query.other_fieldname, query.more_fieldnames etc. etc. How do I loop that based on the <input> field attributes?
You can use var inputs = document.getElementById('your-form-id').getElementsByTagName('input'); and then perform a for loop with this check: if (inputs[i].name && /^query\[/.test(inputs[i].name)) { ... }.
Yes I knkow how to do a loop, but if I'm using an object style like query.[name], how do I assign it? Let say fieldname = min_price is set, and I want to automatically translate this into query.min_price.. do I just do query.'fieldname' and this becomes query.min_price object?
0

Try this solution : add [] to your query key

$.post("ajax_findproperties.php", { 'query[]': postarray },
    function(data) { },
    'json');

Source : http://api.jquery.com/jQuery.post/#example-2

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.