Sopheak Parklin Sopheak Parklin - 4 days ago 6
jQuery Question

How can i get value from select option using ajax and php

I have 2 select box option, city and district. I use ajax go get district name when I choose on the city. For this process I have succeed but when I click submit I get the error message Undefined index: district ,as this picture you can see
result image.

Here is my ajax code:

$(document).ready(function($) {
$("#city").change(function() {
var search_id = $(this).val();
url: "search.php",
method: "post",
data: {search_id:search_id},
success: function(data){

Here is HTML code:

<form action="" method="post">
select city:
<select name="city" id="city">
<option value="1">Phnom Penh</option>
<option value="2">Kampong Soam</option>
<option value="3">Siem Reap</option>

select district:
<select name="distrcit" name="district" id="district">
<option>Select District</option>

<input type="submit" name="submit">

Here is PHP code:

echo $_POST['city'];
echo $_POST['district'];


//ajax request
$search_id = $database->escape_string($_POST['search_id']);

// $output = array();
$sql = "SELECT * FROM district WHERE ref_id = '$search_id'";
$districts = District::find_this_query($sql);
foreach($districts as $district){
// echo $district->id;
// echo $district->district_name;
$output .= "<option value='{$district->id}'>{$district->district_name}</option>";

echo $output;



You have set name twice in select box. assign name only once:

so make it:

<select name="district" id="district">
    <option>Select District</option>