Jump to content

populate 2nd select box with JQuery and PHP output JSON?


jarv

Recommended Posts

hi, I was going through this tutorial: http://www.electrictoolbox.com/json-data-jquery-php-mysql/

 

here is my HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
function populateFruitVariety() {
   
    $.getJSON('search-by.php', {fruitName:$('#fruitName').val()}, function(data) {

        var select = $('#fruitVariety');
        var options = select.attr('options');
        $('option', select).remove();

        $.each(data, function(index, array) {
            options[options.length] = new Option(array['variety']);
        });

    });

}

$(document).ready(function() {

populateFruitVariety();
$('#fruitName').change(function() {
	populateFruitVariety();
});

});
</script>
</head>

<body>
<form>
    Search by:
    <select name="name" id="fruitName">
        <option>Please Select</option>
	  <option id="Town" value="Town">Town</option>
        <option id="County" value="County">County</option>
    </select>
    Variety:
    <select name="variety" id="fruitVariety">
    </select>
</form>

</body>
</html>

 

here is my PHP

$dbhost = "xxx";
$dbname = "xxx";
$dblogin = "xxx";
$dbpass = "xxx";

function dbConnect() {
   global $dbhost;
   global $dbname;
   global $dblogin;
   global $dbpass;

   $db = mysql_connect($dbhost, $dblogin, $dbpass) or die("could not
connect to database: ".mysql_error());
   mysql_select_db($dbname) or die("could not select database");
   return $db;
}
function dbClose($db) {
   mysql_close($db);
}


// basis code ^^


$db = dbConnect();
$rows = array();
if(isset($_GET['Town'])) {
$query = "SELECT DISTINCT rsCounties FROM pubs"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo json_encode($rows);

}
}




if(isset($_GET['County'])) {
    $stmt = $pdo->prepare("SELECT rsCounty FROM pubs");
    $stmt->execute(array($_GET['rsCounty']));
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($rows);

 

Can someone please help? I have the JQuery working but not the PHP?!

here is a link to what I have at the moment: http://www.mypubspace.com/dashnew/index.html

thanks

I have the JQuery working but not the PHP?!

 

what is "not working" with the PHP???

 

errors displayed? errors logged? have you tried running the PHP via browser or command-line? have you tried mail()'ing output from the PHP during AJAX?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.