Skip to content
Advertisement

Using JS id in dropdown – works from text file but not from a php variable string

I am using an HHTML form with a dropdown which is populated from a JS script through id.

JavaScript

The JS script to create the id “ctags” is as follows using a .txt file

JavaScript

This dropdown populates and works as should, but I want to rather use a PHP SELECT statement to retrieve the information every time and generate a fresh list for the towns, as the information does change periodically. I have created the PHP SELECT section and it generates a PHP variable which is a string, that is identical to the txt file. I have checked this through console.log. So the PHP code to take the array returned from the SELECT statement is as follows. I have added str_replace to make sure that the string is identical to the txt file and $towns_dropdown is the array returned from the SELECT statement.

JavaScript

I then change my JS to create the id “ctags” as follows using a PHP variable instead of the txt file.

JavaScript

If I read results in console.log it displays the string, but the dropdown doesn’t populate. If I get the PHP create to create a new file from the PHP variable and then call that file (file.txt) in the JS script as follows:-

JavaScript

it populates the dropdown correctly

So JS calls the new file file.txt and it works, as in populating the dropdown.

So, in conclusion, I don’t seem to be able to get a PHP variable(which is a string) to get the dropdown to populate…….

Advertisement

Answer

I managed to get it working using the PHP variable as follows:-

JavaScript

and the JS script line:-

JavaScript

@CBroe, you were correct, I was getting confused in the JSON shennanigans, once I saw through that it was pretty straight forward.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement