Can I read a txt file and automatically fill in php to implement this function? I have a 1.txt text & nbsp; which contains the following content: & nbsp; 001-Beijing & nbsp; 002-Shanghai, and then on the Php page, the & lt; input & gt; Box & lt; input & nbsp; type = whether the txt file can be read and filled automatically
Can php implement this function?
I have a 1.txt text with the following content:
001-Beijing
002-Shanghai
On the Php page Box
Can I enter 001 in the number box?
Automatically fill in the corresponding region Beijing in the area box
Or through ajax?
Can I implement the two methods?
What should we do if it can be implemented?
Thank you.
------ Solution --------------------
1. read the database. 2.
If you really want to implement it, use get_content_file () to read the text content to the string, then cut the string and store it in the array, and promise the array cyclically (set it to html format)
------ Solution --------------------
Ajax implementation
------ Solution --------------------
In fact, it is best to directly implement js. First, you can generate an array js for the region, and then operate on the array.
------ Solution --------------------
My js is very bad.
For example, generate a region js
Area [1] = 'Beijing ';
Area [2] = 'Shanghai ';
......
Then, when the input box loses focus, the value of the current text is obtained and set to area [value].
------ Solution --------------------
Reference:
Reference: 1. it is best to read the database. 2.
If you really want to implement it, use get_content_file () to read the text content to the string, then cut the string and store it in the array, and promise the array cyclically (set it to html format)
① Import the corresponding data text into the database? For example, create a table named "area" to obtain two fields: "number" and "area ".
How can this problem be solved? In addition, if there are too many text data, import the data ......
I didn't ask you to write the text to the database and read it again. I suggest you directly store the data into the database and read it from the database. do not store it in text.
If you need to save the text as well, you can use the function to read it and make the split loop and output it on the PHP page.
------ Solution --------------------
There is no difficulty in this kind of things. it is a problem of passing values through ajax and then taking values.
You can store data files or databases. Yes.
------ Solution --------------------
If you want to take the appropriate time, you can use ajax to process the page. if you only need to generate a php page, I will post the code for you:
1.txt file:
-001-Beijing
-002-Shenzhen
2. php processing page
$ Str = file_get_contents ("1.txt"); // read the object
$ Array = explode ("-", $ str); // cut the content, which exists in the array
$ Count = count ($ array );
// Print the array cyclically in html format
For ($ I = 0; $ I <$ count-1; $ I = $ I + 2)
{
Echo"
";
Echo"
";
}
------ Solution --------------------
Ajax can be easily implemented ..
------ Solution --------------------
At the end of this post, xuzuning edited at 19:21:25 on January 1,. you don't need to use AJAX. just put the data in js.
Complete test code
$ S = <TXT
001-Beijing
002-Shanghai
TXT;
File_put_contents('0001.txt ', $ s );
// Construct the simulation data
$ Fn = '0001.txt ';
Foreach (file ($ fn, FILE_IGNORE_NEW_LINES) as $ r ){
$ T = explode ('-', $ r );
$ Res [] = "'$ t [0]': '$ t [1]'";
}
$ T = join (',', $ res );
Echo <JS
Script
Var dict ={{$ t }};
Function foo (){
Var tag = document. getElementById ('number ');
If (dict [tag. value]! = Undefined) document. getElementById ('region'). value = dict [tag. value];
Else document. getElementById ('area '). value = 'unknown ';
}
Script