user6043723
user6043723

Reputation: 177

PHP AJAX - Pass a PHP value from one page to another using AJAX

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'];

Upvotes: 0

Views: 5000

Answers (3)

Akash Shah
Akash Shah

Reputation: 53

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.

Upvotes: 1

RJParikh
RJParikh

Reputation: 4166

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'];

Upvotes: 0

Govind
Govind

Reputation: 267

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

Upvotes: 0

Related Questions