chrsgrffth
chrsgrffth

Reputation: 88

jQuery to execute PHP MySQL query

Here's what I've got. I'm trying to have jquery run a mysql query.

This is my PHP:

<select name="kingdom" id="kingdom" >
    <option value="standard">-- Kingdom --</option>
        <?php
            $the_d = $_POST['d'];
            $filter = mysql_query("SELECT DISTINCT sci_kingdom FROM tbl_lifedata WHERE sci_domain = '$the_d'");
                while($row = mysql_fetch_array($filter, MYSQL_NUM))
                    {
                        $row['name'];
                        //echo "<option value='$row[0]'>$row[0]</option>";
                    }
        ?>
</select>

And my jQuery:

$('#domain').change(function () {

    var selectval = $('#domain').val();

    $.post("search.php", {
        d: selectval
    }, function (data) {
        $('.result').html(data);
    });
});

Right now I would just like to have jQuery spit out the values of the mysql result. When I have that working, I can have them populate a select box. What I get now is the html of search.php but nothing to do with the mysql query.

Upvotes: 2

Views: 9136

Answers (1)

Eswar Rajesh Pinapala
Eswar Rajesh Pinapala

Reputation: 4911

You should actually print what you are retrieving from the DB.

<select name="kingdom" id="kingdom" >
                <option value="standard">-- Kingdom --</option>
                <?php 
                $the_d = mysql_real_escape_string($_POST['d']);//thanks to @Mansfield
                $filter = mysql_query("SELECT DISTINCT sci_kingdom FROM tbl_lifedata WHERE sci_domain = '".$the_d."'");
                while($row = mysql_fetch_array($filter, MYSQL_NUM))
                {
                    echo "<option value='$row[0]'>$row[0]</option>";

                }

                ?>
            </select>


 //or you can also use mysql_fetch_array($filter, MYSQL_ASSOC) if you want to print $row['name']

Updated answer:

<script src="jquery.min.js"></script>
<input type='text' class="domain" id="domain"/>
<div class="result" id="result"></div>
<script>
$('#domain').change(function() {

            var selectval = $('#domain').val();

            $.post(
                "test.php", 
                { d : selectval },
                function(data) {
                    $('.result').html(data);//my bad should be displaying data retrenched
                }
                );
});
</script>

///test.php contents:

<?php
print $the_d = mysql_real_escape_string($_POST['d']);// see if the scripts are working fine until here, if so, then see below.
//test your mysql result by doing a print_r($row.)    
?>

post the result of print_r($row);

Upvotes: 2

Related Questions