javascript - need to pass a php array constructed from a txt file back to an ajax call -


I'm peating your head with it, now you're searching on tube and stackoverflow flow, similarly There is no issue with any luck to solve the situation, so I hope that if I ask directly then someone can help ...

Now to explain my problem ... I have a php page that has a selected field like text field which is javascript and Must distinguish between themselves through a combination of PHP. Now the selection field has a list of countries from which I get the value from this code:

var select = $ ("select [name = 'from_country'] option: selected") . Text ();

Where from_country is the name of the field selection.

Text field on the other hand is uncontrolled Jquery function with a keyboard, which is then considered to use the "select" variable value, which is considered to pass on the PHP function through an AJAX call , Which at the end will find a txt file with the appropriate name of the country from the value of the selected country and put one in the list of its cities and put it in an array which will be returned to the Javascript function later Education should make that Ajax call was made and available tags a variable the full automatic suitable names of the selected names to the text field that is used by a remote function was called populated country.

Now my issue is that if I try only with javascript and keep the available tugs variable as function I am the same function that lists all the functions as an array It works fine, but it is very ugly code and I want to clean it to bring those notepad files with php using AJAX call and then return them back to an array which is similar to the avaialbleTags variable Will be shaken.

After my last attempts to resolve this problem, the code looks like this:

javascript file:

  $ (document) .ready ( Function () {$ ("#tag") .keydown (function () {// need to come here code, which checks which country is selected and cities related to a suitable / selected country (you An AJAX // request will be sent to a PHP file, which will check the country and obtain the appropriate external file // Select Var option = $ ("[name = 'from_country'] to select the option used as an array to be used in the function: selected"). Text (); var availableTags = $ .ajax ( {Url: "find_cities_by_country.php"), data: {country_to_find: select}, datatype: "json", success: work (data) {return data;}}) // auto complete external function $ ("#tag" Autocomplete ({source: availableTags});});});  

PHP file: {

  $ country_to_find = $ _POST ["country_to_find"]; If (! $ Country_to_find)) reads the file with the name of the value passed from the // javascript function, and then adds that content to the $ data array, while the loop $ file_to_read = $ country_to_find ".text "; $ FH = FOPN ($ file_to_read, 'r'); {$ (FH)) {$ data []. = Fgets ($ fh); // "" & lt; br & gt; "}; Fclose ($ FH); // json_encode for javascript function The data should be given as an array that is // echo json_encode ($ data);}  

to this AJAX call on this function if I help to answer your question, some relevant Please forget to add information, please tell me, thx.

fixed code

  var availableTags = {}; $ .aex ({url: "find_cities_by_country.php", date : {Country_to_find: select}, datatype: "json", success: function (data) {availableTags = data;}});  

explanation

$. Ajax () Return data does not give you the data variable should be specified in the available tag variable.

Available tags was declared at the top, because it has an anonymous function near $ ("#tag") .keydown () method

Should be in context.

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -