如何使用 php 从 Javascript 获取变量来编写 txt 文件
How to write a txt file by using php which variable is get from Javascript
我想写一个txt格式的文件
从纬度,经度变为
纬度
经度
如何实现?
html:
<form method = "post" name = "searchbar">
<input type="text" name="search" id="SearchBar" placeholder="input a ip">
<br>
<button type="button" onclick="getLocation()">get ip</button>
<button type="submit" name = "writeip" id="id_Writeip">submit</button>
</form>
php:
if(array_key_exists('writeip', $_POST)) {
writeip();}
function writeip(){
$myfile = fopen("testing.txt", "w") or die("Unable to open file!");
$txt = $_POST["search"];
fwrite($myfile, $txt);
fclose($myfile);}
脚本:
<script>
function getLocation()
{
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position)
{
document.getElementById("SearchBar").value = position.coords.latitude + ", " + position.coords.longitude;
}
</script>
如果我没理解错的话,您需要查看该值是否 posted 作为提交表单的一部分。在那种情况下,它可以像这样检查。
if(isset($_POST['search'])) {
// You also noted you want the cords on new lines.
$search = $_POST["search"];
$data = explode(", ", $search);
$cords = implode("\n", $data);
writeSearch($cords);
}
function writeSearch($data) {
$myfile = fopen("testing.txt", "w") or die("Unable to open file!");
fwrite($myfile, $data);
fclose($myfile);
}
在这种情况下,我们检查 post 是否包含搜索值,然后调用 writeSearch
函数。然后将我们检查的值写入文件。
我想写一个txt格式的文件 从纬度,经度变为 纬度 经度
如何实现?
html:
<form method = "post" name = "searchbar">
<input type="text" name="search" id="SearchBar" placeholder="input a ip">
<br>
<button type="button" onclick="getLocation()">get ip</button>
<button type="submit" name = "writeip" id="id_Writeip">submit</button>
</form>
php:
if(array_key_exists('writeip', $_POST)) {
writeip();}
function writeip(){
$myfile = fopen("testing.txt", "w") or die("Unable to open file!");
$txt = $_POST["search"];
fwrite($myfile, $txt);
fclose($myfile);}
脚本:
<script>
function getLocation()
{
navigator.geolocation.getCurrentPosition(showPosition);
}
function showPosition(position)
{
document.getElementById("SearchBar").value = position.coords.latitude + ", " + position.coords.longitude;
}
</script>
如果我没理解错的话,您需要查看该值是否 posted 作为提交表单的一部分。在那种情况下,它可以像这样检查。
if(isset($_POST['search'])) {
// You also noted you want the cords on new lines.
$search = $_POST["search"];
$data = explode(", ", $search);
$cords = implode("\n", $data);
writeSearch($cords);
}
function writeSearch($data) {
$myfile = fopen("testing.txt", "w") or die("Unable to open file!");
fwrite($myfile, $data);
fclose($myfile);
}
在这种情况下,我们检查 post 是否包含搜索值,然后调用 writeSearch
函数。然后将我们检查的值写入文件。