0

I would like to pass a php variable onto another php page (category-get-secondary.php) through Ajax. I have the following code:

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>')
            }
        });
    }

On category-get-secondary.php I want to get the value: -

$postPrimaryCat = $_POST['primary_cat'];
$categoryType = $_POST['category-select'];

$postPrimaryCat is transferred. Now I want to transfer the value for $categoryType.

I would like to pass the PHP variable 'category-select'. This is what I tried to transfer the value:

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

and to get the value I'm using

$getCategorySet = $_POST['category-select'];

but the value doesn't seem to be transfereed

Updated with full code:

main-page.php

<script src="https://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script>

</script>
<script>


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

    function selectCountry(val) {
        $("#search-box").val(val);
        $("#suggesstion-box").hide();
    }
</script>

<?php

//Get category set from URL bar
$_POST['categoryselect'] = 'premium';

category-get-secondary.php

<?php
//Insert value to ajax
$postCatType = $_POST['categoryselect'];
3
  • Use this : data:{primary_cat:val,category-select:cat_val} Commented Apr 8, 2016 at 10:13
  • The value 'category-select' still does not get outputted. To transfer it from the first page I am using $_POST['category_select'] = 'private'; Commented Apr 8, 2016 at 10:22
  • @user6043723, Try to change your variable from category-select to category_select Commented Apr 8, 2016 at 10:44

3 Answers 3

1

Valid Characters

In general JavaScript, variable/function names can't contain -. They can only contain letters, $, and _ (Underscore)

So... change data:'primary_cat='+val+'&category-select='+cat_val, to this data:'primary_cat='+val+'&category_select='+cat_val,

Hope it solves your issue.

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

9 Comments

Hi. I tried that method too... but it didn't work either
Change this $getCategorySet = $_POST['category-select']; to $getCategorySet = $_POST['category_select'];
Hi, no it didn't unfortuantely
It is a bit confusing what you want to do but as per what i understand you want to pass "premium" as categoryselect to another page. Use this: data:'primary_cat='+val+'&categoryselect=premium',
How can I pass the php string premium as a php string, as the value `premium' may change.
|
0
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>')
            }
        });
    }

1 Comment

The value is not being output in the category-get-secondary.php To transfer it from the first page I am using $_POST['category_select'] = 'private';
0

Change data and add below data line in your code. Ajax

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

PHP

$postPrimaryCat = $_POST['primary_cat'];
$categoryType = $_POST['category-select'];

2 Comments

try to change "category-select" to "category_select". @user6043723
Hi. How do I initialise the primary value to send it through the AJAX. I tried $_POST['category-select'] = 'private';

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.