如何使用ajax和php从select选项中获取值

时间:2022-11-27 16:55:33

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.

我有两个选择框选项,城市和地区。当我选择城市时,我使用ajax go获取地区名称。对于这个过程,我已经成功了,但是当我单击submit时,我得到了错误消息Undefined index: district,正如这张图片所示,您可以看到结果图像。

Here is my ajax code:

以下是我的ajax代码:

 $(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:

这是HTML代码:

   

 <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代码:

<?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;
	}
	
 }
?>

5 个解决方案

#1


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>

#2


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

我认为这种方法不太干净,您可以使用json格式创建一个返回值的ajax请求,然后在选择标记中添加resultats。

new Option(text, value);

#3


1  

please define name one time only :

请只定义一次名称:

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

#4


1  

In the HTML you have declared name attributes 2 times:

在HTML中,你已经声明了2次名称属性:

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

Please replace with:

请替换为:

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

#5


-1  

   

 <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


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>

#2


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

我认为这种方法不太干净,您可以使用json格式创建一个返回值的ajax请求,然后在选择标记中添加resultats。

new Option(text, value);

#3


1  

please define name one time only :

请只定义一次名称:

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

#4


1  

In the HTML you have declared name attributes 2 times:

在HTML中,你已经声明了2次名称属性:

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

Please replace with:

请替换为:

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

#5


-1  

   

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