1

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();
        $.ajax({
            url: "search.php",
            method: "post",
            data: {search_id:search_id},
            success: function(data){
                $("#district").html(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>

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

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

Here is PHP code:

<?php 
if(isset($_POST['submit'])){
	echo $_POST['city'];
	echo $_POST['district'];
}

 ?>

//ajax request
<?php 
if(isset($_POST['search_id'])){
	$search_id = $database->escape_string($_POST['search_id']);

	if(!empty($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;
	}
	
 }
?>

3
  • Nothing with ajax and PHP but you have a problem in HTML with select case, you have "name" twice Commented Apr 29, 2016 at 7:47
  • Why you have got double name=""" parameter in this line: <select name="distrcit" name="district" id="district"> Commented Apr 29, 2016 at 7:47
  • oh my god, I really didn't check the name. now it work! Thank you much :) Lorenzo and Pes Commented Apr 29, 2016 at 7:53

5 Answers 5

1

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

so make it:

<select name="district" id="district">
    <option>Select District</option>
</select>
Sign up to request clarification or add additional context in comments.

1 Comment

Oh dear, it work. I'm so mistake, but anyway thank you. very thank for that :)
1

I think this way is not very clean, you may create an ajax request returning values in json format and then append resultats in the select tag using

new Option(text, value);

Comments

1

please define name one time only :

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

Comments

1

In the HTML you have declared name attributes 2 times:

<select name="distrcit" name="district" id="district">

Please replace with:

<select name="district" id="district">

Comments

0

   

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

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

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

1 Comment

Please support your answer with explanation. Apparently code only options are not very useful.

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.