2

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.

3
  • 2
    Note that your code has a SQL injection vulnerability on line 5. Commented Jun 21, 2012 at 1:35
  • 2
    You are not outputting the values from the query. You merely have $row['name'] in the fetch loop. You're using numeric keys in the fetch, so $row['name'] wouldn't be set anyway. Commented Jun 21, 2012 at 1:35
  • Were you able to print $_POST['d'] ?? Commented Jun 21, 2012 at 2:32

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for reminding me about the real escape string! What do I need to do to my jQuery to get it to populate? Right now, after these changes, it still isn't working.
Yes! It comes up with the value of the select onchange. What can I do from here??
How would I do that? How can I get the results from the query and use them to populate the select box?
Hmm..the only thing that is happening is the $('.result').html(selectval) is appearing, nothing from the mysql query appears
My bad, I was testing this and I left the selectval like that, it should update the result div with data but not selectval
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.