Javascript 需要将从txt文件构造的php数组传递回ajax调用
我用这个来敲我的头已经有一段时间了,现在在you tube和stackoverflow上探索类似的情况,以找到解决这个问题的方法,但没有任何运气,所以我希望如果我直接询问,有人能帮助我 现在来解释我的问题…我有一个php页面,它有一个选择字段和一个文本字段,它们应该使用javascript和php的组合相互作用。现在,select字段有一个国家列表,我使用以下代码从中获取值: var select=$select[name='from_country']选项:selected.text 其中from_country是选择字段的名称 另一方面,文本字段从一个keydown匿名jquery函数触发,然后该函数应该使用select变量值通过ajax调用将其传递给PHP函数,然后PHP函数应该从所选国家值中找到一个具有适当国家名称的txt文件,并包含其城市列表和输入它被转换成一个数组,然后返回到进行ajax调用的javascript函数,并填充一个名为availableTags的变量,该变量由远程函数使用所选国家的适当名称自动完成文本字段 现在我的问题是,如果我只在javascript中尝试,并将availableTags变量与同一个函数中的一个函数相等,该函数以数组的形式获取该函数中列出的所有值,则效果良好,但是这是非常难看的代码,我想通过使用ajax调用来清理它,用php获取这些记事本文件,然后将它们返回到一个数组中,该数组将被封装到AvailableTags变量中 下面是我上次尝试解决这些问题后代码的样子: javascript文件:Javascript 需要将从txt文件构造的php数组传递回ajax调用,javascript,php,jquery,ajax,arrays,Javascript,Php,Jquery,Ajax,Arrays,我用这个来敲我的头已经有一段时间了,现在在you tube和stackoverflow上探索类似的情况,以找到解决这个问题的方法,但没有任何运气,所以我希望如果我直接询问,有人能帮助我 现在来解释我的问题…我有一个php页面,它有一个选择字段和一个文本字段,它们应该使用javascript和php的组合相互作用。现在,select字段有一个国家列表,我使用以下代码从中获取值: var select=$select[name='from_country']选项:selected.text 其中fr
$(document).ready(function(){
$( "#tag" ).keydown(function(){
//here needs to come code that checks which country is selected and fetch an appropriate //array of cities corresponding to the selected country(you will need to send an ajax //request to a PHP file which will check the country and fetch the appropriate external file //with tags to be used as an array to be used in the autocomplete function below!!
var select = $("select[name='from_country'] option:selected").text();
var availableTags = $.ajax({
url: "find_cities_by_country.php",
data: {country_to_find : select},
dataType: "json",
success: function(data){
return data;
}
});
//the autocomplete is the external function
$( "#tag" ).autocomplete({
source: availableTags
});
$( "#tags" ).autocomplete({
source: availableTags
});
});
});
PHP文件:
$country_to_find = $_POST ["country_to_find"];
if(!empty($country_to_find)){
//reads the file with the name of the value passed from the javascript function
//and then appends it's contents to the $data[] array in the while loop
$file_to_read = $country_to_find.".txt";
$fh = fopen ($file_to_read, 'r');
while(!feof($fh))
{
$data[] .= fgets($fh);//."<br>";
}
fclose($fh);
//json_encode should pass the data as an array to the javascript function that made
//the ajax call to this function
echo json_encode($data);
}
如果我忘记添加一些相关信息来帮助您回答我的问题,请告诉我,谢谢。修复代码
解释
$.ajax返回,而不是响应数据。您应该将解析后的JSON数据变量分配给availableTags变量
availableTags在顶部声明,因为它需要在传递给$tag.keydown方法的匿名函数的上下文中。Hey,可能我不清楚,但我希望返回的数据是一个非键关联值数组,封装在availableTags变量中,该变量应该是一个var数据返回值数组,当用户开始键入时,外部函数将其用作标记的引用。我没有使用对象…我所需要的是返回的数据被传输到一个数组中,该数组等于availableTags,应该是这样的:availableTags=[value1,value2,value3]也许我在这里尝试的是不可能的,但我需要有人告诉我,因为我对这件事非常困惑,而且根据我目前对这件事的了解,我不认为我自己能弄明白…在这里发布你从PHP传递到浏览器的JSON结果-我将重写代码,使其成为一个数组
var availableTags = {};
$.ajax({
url: "find_cities_by_country.php",
data: {country_to_find : select},
dataType: "json",
success: function(data){
availableTags = data;
}
});