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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.