JQuery Ajax Internal Server Error with PHP Call

I’m quite new to this stuff and so far, I could get it working when I would pass only 1 $_POST variable.

However in this case, I’m trying to pass 2 variables and for some reason I’m getting an Internal Server Error as result. Now I have done some trace and it’s definitely not an SQL error. I’m sure the Error stems from the Ajax part, where I’m passing the POST but I can’t figure out what I’m doing wrong. Could anyone help with this?

Here’s my Ajax code:

$('.class_checkbox').on('click',function(){
    var split = this.id.split('_');
    var catID = split[0];
    var myData = 'catToInsert='+ $id; //build a post data structure
    var categoryId = 'category='+catID;

    jQuery.ajax({
        type: "POST", // HTTP method POST or GET
        url: "includes/category.php", //Where to make Ajax calls
        dataType:"html", // Data type, HTML, json etc.
        data:{myData: myData, categoryId: categoryId}, //Form variables
        success:function(response){
             alert(response);
        },
        error:function (xhr, ajaxOptions, thrownError){
            //On error, we alert user
            alert(thrownError);
        }
    });
    $(this).toggleClass('checked').prev().prop('checked',$(this).is('.checked'));

});       

And here is my PHP:

<?php 
    include_once('config.php');
    $id = $_POST['catToInsert'];
    $catID = $_POST['category'];


    //Create PDO Object
    $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD );
    //Set Error Handling for PDO
    $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    //Query

    $sql = "SELECT category_id from product_category WHERE product_id=$id";

    //Prepare Statement
    $stmt = $con->prepare($sql);
    $stmt->execute();

    $result = $stmt->fetchAll();
    $array = array_map('current', $result);

    echo $id;
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s