0

I have the following jQuery that passes variables onto a PHP form:

function getSecondaryCat(val) {
    $.ajax({
        type: "POST",
        url: "category-get-secondary.php",
        data:'primary_cat='+val,
        success: function(data){
            $("#secondary_cat").html(data);
            $("#tertiary_cat").html('<option value="">Select specific category</option>')
        }
    });
}

The above passes the data onto to a PHP function

//Get primary category value
$postPrimaryCat = $_POST['primary_cat'];

Now I want to pass another variable called category_type how do I add it in my jQuery above?

3
  • 2
    data: { primary_cat: val, category_type: val2 } Commented Apr 7, 2016 at 13:35
  • Does nobody EVER Read The Manual Commented Apr 7, 2016 at 13:38
  • use this data:'primary_cat='+val+'&category_type='+type Commented Apr 7, 2016 at 13:43

2 Answers 2

2

Simple way.

Using array into data parameters.

data: {primary_cat:val,category_type:cat_var}

Complete example,

function getSecondaryCat(val, cat_var) {
$.ajax({
    type: "POST",
    url: "category-get-secondary.php",
    data: {primary_cat:val,category_type:cat_var},
    success: function(data){
        $("#secondary_cat").html(data);
        $("#tertiary_cat").html('<option value="">Select specific category</option>')
    }
});
}
Sign up to request clarification or add additional context in comments.

1 Comment

But pretty much covered by @Daan in his comment
0

Hey you can try like this:

data parameter used to pass the values, so you can append values here as in the url

function getSecondaryCat(val) {
        $.ajax({
            type: "POST",
            url: "category-get-secondary.php",
            data:'primary_cat='+val+'&category_type='+cat_val,
            success: function(data){
                $("#secondary_cat").html(data);
                $("#tertiary_cat").html('<option value="">Select specific category</option>')
            }
        });
    }

2 Comments

Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO that may find this question and be reading your answer.
I have no experience with jQuery and I was stuck. That is my I asked the question in the first place.

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.