简体   繁体   English

如何解决Ajax身份不明的索引

[英]how to troubleshoot ajax unidentified index

I have looked over my code and it appears that city_id is set by ajax and I can't see why I am still getting undefined index. 我看了看我的代码,看来city_id是由ajax设置的,我看不到为什么我仍然得到未定义的索引。 I've looked at this for too many hours now. 我已经看了太多小时了。 Please help me find the reason for this. 请帮助我找到原因。

edit to add ad. 修改以添加广告。 info.: this script populates selection boxes for country,state,city,zipcode from mysql db. 信息:此脚本填充来自mysql db的国家,州,城市,邮政编码的选择框。 all are working except the last zipcode box. 除最后一个邮政编码框外,所有其他功能都在工作。 if i set city_id explicitly in ajaxData.php it works, that's why i know i'm missing something. 如果我在ajaxData.php中显式设置city_id,则可以正常工作,这就是为什么我知道我缺少某些东西的原因。

address.php 地址.php

</select>
<select name="city_id" id="city_id">
<option value="">Select state first</option>
</select>

<select name="zipcode" id="zipcode">
<option value="">Select city first</option>

<script>

$('#city_id').on('change',function(){
var cityID = $(this).val();
if(cityID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:{city_id: cityID},
//data format changed as per suggestion.
dataType:'html',

success:function(html){
$('#zipcode').html(html);
console.log(html);
}
});

}else{

$('#zipcode').html('<option value="">Select city first</option>');
}
});
</script>

ajaxData.php ajaxData.php

<?php
$city_id=$_POST['city_id'];
if(isset($_POST["city_id"]) && !empty($_POST["city_id"])){

//Get all zipcode data
$query =mysqli_query($conn, "SELECT * FROM zipcodes WHERE city_name = 
'$city_id' AND status = 1 ORDER BY zipcode ASC");
$num_rows = mysqli_num_rows($query);
if($num_rows > 0){
echo '<option value="">Select zipcode</option>';
while($row = $query->fetch_assoc()){

echo '<option value="'.$row['zipcode'].'">'.$row['zipcode'].'</option>';

}
}elseif($num_rows = 0){

echo '<option value="">Zipcode not available</option>';

}}
?>

I think you are setting the data wrong on your ajax request. 我认为您在ajax请求中设置的数据有误。 Try setting it as an object like : 尝试将其设置为类似的对象:

    $.ajax({
      type:'POST',
      url:'ajaxData.php',
      data:{city_id :cityID},
      dataType:'html',

The issue here is the way you post your data; 这里的问题是您发布数据的方式; you are doing the following in your AJAX; 您正在AJAX中执行以下操作;

data:'city_id='+cityID,

You need; 你需要;

data: { city_id: cityID },

This is because the data is an object of references, not a query string 这是因为数据是引用的对象,而不是查询字符串

For all intent and purpose if you wanted to use the query string, you could have; 出于所有意图和目的,如果您想使用查询字符串,则可以使用;

url:'ajaxData.php?city_id=' + cityID,

However, I personally advise against this as it could be piggybacked onto but this is not definitely going to happen 但是,我个人不建议这样做,因为它可能会附带出现,但是这绝对不会发生

You can try this 你可以试试这个

$.post('ajaxData.php', { city_id: cityID })
  .success(function(html) {
    ...
  });

Do not use data as string, you will have to escape all the values. 不要将数据用作字符串,您必须转义所有值。 You should always debug the received values on PHP level. 您应该始终在PHP级别调试接收到的值。 At least you can always dump POST content and see what you exactly received. 至少您始终可以转储POST内容并查看您收到的内容。

Do not forget the user values when you are sending them to database or you application would be pretty easily hackeable. 将用户值发送到数据库时,不要忘记它们,否则应用程序很容易被黑客入侵。

after some deeper troubleshooting i found out my javascript variable "cityID" was equal to "11". 经过更深入的故障排除后,我发现我的JavaScript变量“ cityID”等于“ 11”。 i'm not sure why that would be undefined. 我不确定为什么这将是不确定的。 11 represented the cityid which was the selection option's value not the display text which was "anchorage". 11代表的是城市ID,它是选择选项的值,而不是显示文本,是“锚地”。 i changed mysql statement so that city_name was the option value and then my zipcode selection box populated correctly. 我更改了mysql语句,使city_name为选项值,然后正确填充了我的邮政编码选择框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM