Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i am trying to create a dynamic dropdown menu for my website to add address like state, city, area and postcode. i am using code is working when i try one by one preview in live browser but it does not work when i try to use java-script its not show the data in dropdown menucan you plese tell me where i am making mistake

it is index.php

<head>
</head>
<script src="scripts/jquery.js" type="text/javascript"></script>
<script src="scripts/scripts.js" type="text/javascript"></script>

<body>
<h1>address</h1>
<hr/>
<label>please select state</label>
<select id="slctstate"></select>
<br />
<br />
<label>please select city</label>
<select id="slctcity"></select>
</body>
</head>

it is script.js

$(document).ready(function() {
$.getJSON("get_stat.php", success = function(data){
    var options = "";
    for(var i = 0; i < data.lenght; i++)
        {
        options += "<option value='" + data[i].toLowerCase() + "'>" + data[i] +      "</option>";
        }
        $("#sltstate").append(options);
});
$("#slctstate").change(function(){
    $.getJSON("get_city.php?state=" +$(this).value(), success = function(data){
    var options = "";
    for(var i = 0; i < data.lenght; i++)
        {
        options += "<option value='" + data[i].toLowerCase() + "'>" + data[i] + "</option>";
        }
        $("#slctcity").append(options);
    });
});

});

it is get_state.php

<?php 
require "Connections/dbopen.php";

$query = "SELECT state_name FROM states";
$data = mysqli_query($conn, $query);

$states = array();

while ($row = mysqli_fetch_array($data))
{
array_push($states, $row["state_name"]);    
}
echo json_encode($states);

require "Connections/dbclose.php";

?>

and hers is get_city.php

<?php 
require "Connections/dbopen.php";
if(isset($_GET["$state"]));
{
$state = $_GET["state"];
$query = "SELECT city_name FROM city
INNER JOIN states ON
city.state_id=states.state_id
WHERE state_name LIKE '{$state}'"; 

$data = mysqli_query($conn, $query);
$city = array();
while ($row = mysqli_fetch_array($data))
{
array_push($city, $row["city_name"]);   
}
echo json_encode($city);
require "Connections/dbclose.php";
}

?>

but at the final step i did not get any value in my drop down can any one please help me thanks

share|improve this question
    
in script.js, length is misspelled in for(var i = 0; i < data.lenght; i++) (both times used) –  Sean 20 hours ago
    
also, id="slctstate" != #sltstate –  Sean 20 hours ago
    
also $.getJSON("get_stat.php", != get_state.php –  Sean 20 hours ago
    
change success = function(data){ to function(data){ in script.js, and if(isset($_GET["$state"])) to if(isset($_GET["state"])) in get_city.php, –  Sean 19 hours ago

1 Answer 1

You might want to make the following changes:

  • change lenght to length
  • change success = function(data) to function(data)
  • change isset($_GET["$state"]) to isset($_GET["state"])
  • refer to http://php.net/manual/en/pdostatement.fetch.php for fetching data in an easier way(optional if the first 3 changes dont work)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.