0

HTML, AJAX and PHP included below. Before introducing AJAX, everything functions (form tags and PHP processing values removed from HTML below).

The drop-down (categories) is populated from a MySQL query. When the user selects an option, I want to pass the ID via ajax to a PHP script (index.php) to run a MySQL query to populate another drop-down (sub-categories).

The Chrome console log indicates that ajax is passing the ID correctly.

Firebug also shows it passing and that the URL is correct (index.php?business_category_id=ajax-passed value). If the GET variable is being passed, and my PHP script is looking for it, why is the script not responding? Why is it not receiving the value? I cannot echo it, so I know it's not being received.

The ajax script is in js/script.js, index.php (my controller) is in the root, and the page with the html (buy-a-biz.php) is in the root and included in the php script (see below).

If anyone can help, I'd appreciate it very much. I am new to using jQuery ajax.

HTML.

<select name="category" id="business-category">
    <option value="all_categories">Select category</option>
    <?php foreach ($categories as $category): ?>
        <option value="<?php htmlout($category['id']); ?>"><?php htmlout($category['name']); ?></option>
    <?php endforeach; ?>
</select> 

AJAX. I experimented using $.get and $.post also.

$(document).ready(function(){

    $("#business-category").change(function(){
        var category_id = $(this).val();
        console.log(category_id);

        $.ajax({
            type: 'GET',
            url: 'index.php',
            data: { business_category_id: category_id },
            success: function(category_id){
                $("#result").html(category_id + ' submitted successfully!');
            }
        });
    });
});

PHP.

if(isset($_GET['business_category_id'])){ 

    $category_id = htmlspecialchars($_GET['business_category_id']);

    include 'includes/dbconnect.php';

    try {
        $sql = "SELECT * FROM sub_category
                WHERE category_id = :category_id";
        $s = $db->prepare($sql);
        $s->bindValue(":category_id", $category_id);
        $s->execute();

        while($row = $s->fetch(PDO::FETCH_ASSOC)){
            $sub_categories[] = array(
                'id' => $row['id'],
                'category_id' => $row['category_id'],
                'name' => $row['name']
            );
        }

        $sql2 = "SELECT * FROM category";
        $s2 = $db->prepare($sql2);
        $s2->execute();

        while($row  = $s2->fetch(PDO::FETCH_ASSOC)){
            $categories[] = array(
                'id' => $row['id'],
                'name' => $row['name'],
            );
        }   
    } 
    catch (PDOException $e) {
        $errMsg = "Error fetching data" . $e->getMessage();
        include 'error.html.php';
        exit();
    }

    include 'buy-a-biz.php';
    exit();

}
Jonathan Eustace
  • 2,469
  • 12
  • 31
  • 54
JimB814
  • 510
  • 8
  • 24
  • Possible duplicate of [Passing POST data from Javascript(jquery) to php problems?](http://stackoverflow.com/questions/4191948/passing-post-data-from-javascriptjquery-to-php-problems) – Ivan Barayev May 08 '16 at 13:02
  • @IvanBarayev Thanks, but I was unable to develop a solution from this post. – JimB814 May 08 '16 at 13:33

1 Answers1

1

You are passing a done callback to $.ajax. You should either name this callback success

$.ajax({
    type: 'GET',
    url: 'index.php',
    data: { business_category_id: category_id },
    success: function(category_id){
            $("#result").html(category_id + ' submitted successfully!');
        }
    });

or invoke done on the promise returned by $.ajax:

$.ajax({
    type: 'GET',
    url: 'index.php',
    data: { business_category_id: category_id },
}).done(function(category_id) { 
    $("#result").html(category_id + ' submitted successfully!'); 
});
wero
  • 32,544
  • 3
  • 59
  • 84
  • Thanks for your reply. Using success doesn't make it work. I experimented with success and done and learned that my message will display only if I use success. My php script @index.php is still not receiving the data. – JimB814 May 08 '16 at 13:35
  • I edited my code above to reflect using success. As written, this code generates an error. It displays the message if the category_id parameter is removed -- function() ... instead of function(category_id).... – JimB814 May 08 '16 at 13:45