eamon1234
eamon1234

Reputation: 1585

Passing variable from Ajax to Php

I've looked at all the previous examples but still no dice, and I have a basic php question.

The example is here. I want to be able to click on one table and have the options appear like below:

Example

Explicitly declaring the table name in this code works:

if($_GET['action'] == 'getOptions'){
        $category = $_GET['category'];
        $query = "SELECT `COLUMN_NAME` 
                    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
                    WHERE `TABLE_SCHEMA`='headfirstjson' AND 
                    **`TABLE_NAME`='AmericanOilProduction'**";

        $result = db_connection($query);
        //echo $result;
        $Options = array();

        while ($row = mysql_fetch_array($result)) {                
                $Options[] = $row;
        }
        echo json_encode(array("Options" => $Options));
        exit;
}

This combination of passing in the variable by AJAX does not:

AJAX:

function getOptions(category){
var category = category.value
$.ajax({
    url: "getData.php?action=getOptions",
    type: "GET",
    dataType:"json",
    data: {category:category},
    success: function(json){
        $.each(json.Options,function(){
        var option = "<option>"+this.COLUMN_NAME+"</option>"
        $('#options').append(option)
    });  
    }
});
}

PHP:

if($_GET['action'] == 'getOptions'){
        **$category = $_GET['category']**;
        $query = "SELECT `COLUMN_NAME` 
                    FROM `INFORMATION_SCHEMA`.`COLUMNS` 
                    WHERE `TABLE_SCHEMA`='headfirstjson' AND 
                    `TABLE_NAME`='**.$category.**'";

        $result = db_connection($query);
        //echo $result;
        $Options = array();

        while ($row = mysql_fetch_array($result)) {                
                $Options[] = $row;
        }
        echo json_encode(array("Options" => $Options));
        exit;
    }

It would be great if someone could help me out! Thanks.

Upvotes: 0

Views: 816

Answers (2)

Buggabill
Buggabill

Reputation: 13911

You are attempting to concatenate $category with the periods surrounding it. The double quotes will expand the variable making the table name '.AmericanOilProduction.'. Change the code to look like this removing the concatenation operators. They are not needed here. You should sanitize your input too...

if($_GET['action'] == 'getOptions'){
    $category = $_GET['category'];
    $query = "SELECT `COLUMN_NAME` 
                FROM `INFORMATION_SCHEMA`.`COLUMNS` 
                WHERE `TABLE_SCHEMA`='headfirstjson' AND 
                `TABLE_NAME`='$category'";

    $result = db_connection($query);
    //echo $result;
    $Options = array();

    while ($row = mysql_fetch_array($result)) {                
            $Options[] = $row;
    }
    echo json_encode(array("Options" => $Options));
    exit;
}

Upvotes: 3

Alnitak
Alnitak

Reputation: 340045

I think jQuery is getting confused by the presence of URL parameters both in the URL itself, and in the data: option.

Try putting your action parameter into the data: option instead:

data: {
   category: category,
   action: 'getOptions'
},

Upvotes: 1

Related Questions