Javascript 如何使用php从url中传递到html页面的参数创建文本文件?
我需要我的页面bargraph.html获取参数,如Javascript 如何使用php从url中传递到html页面的参数创建文本文件?,javascript,php,html,Javascript,Php,Html,我需要我的页面bargraph.html获取参数,如../bargraph.html?di=xxxx&mn=yyyy,并使用php脚本将di和mn的值保存在名为cred.txt的文本文件中。我用于bargraph.html的代码是 名为cred.txt的文件没有在cred/目录中创建,我也没有收到任何错误。我做错了什么 如果同样的事情可以使用JavaScript来完成,我将使用JavaScript而不是php来完成。您可以这样做 $mobile_num = $_GET["mn"]; $dev
../bargraph.html?di=xxxx&mn=yyyy
,并使用php脚本将di
和mn
的值保存在名为cred.txt的文本文件中。我用于bargraph.html的代码是
名为cred.txt的文件没有在cred/目录中创建,我也没有收到任何错误。我做错了什么
如果同样的事情可以使用JavaScript来完成,我将使用JavaScript而不是php来完成。您可以这样做
$mobile_num = $_GET["mn"];
$device_id = $_GET["di"];
$file_name = "cred.txt";
$path = getcwd();
$location = $path.'/cred/'.$file_name;
$text = $device_id."\n".$mobile_num;
$my_file = fopen($location, "w") or die("Unable to open file!");
fwrite($my_file, $text);
echo "response submitted successfully!";
fclose($my_file);
您需要授予cred文件夹的写入权限 问题引用了
bargraph.html
——您的php代码可能不在该页面上,而是一个单独的脚本?如果是这样的话,那么如果您要使用异常处理来尝试跟踪问题,它可能会有所帮助。而且,我总是发现使用完整路径比使用相对路径更成功
<?php
if( isset( $_GET['mn'], $_GET['di'] ) ){
try{
$filename='cred.txt';
$mobile_num=filter_input( INPUT_GET, 'mn', FILTER_SANITIZE_STRING );
$device_id=filter_input( INPUT_GET, 'di', FILTER_SANITIZE_STRING );
/*
I have always found it is best to use a full path rather than relative
Change `path/to/` to the appropriate path
*/
$path=$_SERVER['DOCUMENT_ROOT'] . '/path/to/cred';
/* If the path does not exist, warn user */
if( !realpath( $path ) ){
throw new Exception( sprintf( 'Unable to find path: %s', $path ) );
}
/* Can the chosen directory be read? */
if( is_readable( $path ) && is_writable( $path ) ){
$file=$path . '/' . $filename;
#$text=$mobile_num . PHP_EOL . $mobile_num . PHP_EOL;
/* I think this is probably what you intended? */
$text=$device_id . PHP_EOL . $mobile_num . PHP_EOL;
$status=file_put_contents( $file, $text, FILE_APPEND | FILE_TEXT );
throw new Exception( $status ? sprintf('All good! Saved %s',$file) : sprintf('Error - unable to save %s',$file) );
} else {
/*
should set permissions if reading/writing of target folder failed
chmod($path,0777); etc
*/
throw new Exception( sprintf( 'The path %s is either not readable or writable',$path ));
}
}catch( Exception $e ){
exit( $e->getMessage() );
}
}
?>
问题是…?你没有说明问题是什么。正在按预期位置创建文件吗?是空的吗?是否有任何错误?没有,文件未创建,我也没有收到任何错误。是否查看浏览器控制台以查找错误。使用“开发者工具”查看错误。添加
error\u报告(E\u ALL)
在php脚本的开头,您可能没有错误报告启用码。在我看到$path
之前,我发现即使echo也不能在php标记中工作。我认为代码还可以,但问题在于在html页面中包含php代码:/GET或POST如何提交值,变量是否得到响应?使用GET提交值。没有回音不起作用。我认为javascript也可以用于同样的目的,但我不知道如何在JS中编码。如果您也发布如何将数据提交到上述代码中,这样我们就可以建立中间连接。php在html页面中不起作用。使用javascript也可以实现同样的效果??如果页面名为bargraph.html
-请注意html
扩展名,那么它将无法工作,除非您对.htaccess
文件进行了一些欺骗,以迫使PHP将html文件作为PHP处理。您可以使用javascript(ajax或fetch)实现同样的功能
<?php
if( isset( $_GET['mn'], $_GET['di'] ) ){
try{
$filename='cred.txt';
$mobile_num=filter_input( INPUT_GET, 'mn', FILTER_SANITIZE_STRING );
$device_id=filter_input( INPUT_GET, 'di', FILTER_SANITIZE_STRING );
/*
I have always found it is best to use a full path rather than relative
Change `path/to/` to the appropriate path
*/
$path=$_SERVER['DOCUMENT_ROOT'] . '/path/to/cred';
/* If the path does not exist, warn user */
if( !realpath( $path ) ){
throw new Exception( sprintf( 'Unable to find path: %s', $path ) );
}
/* Can the chosen directory be read? */
if( is_readable( $path ) && is_writable( $path ) ){
$file=$path . '/' . $filename;
#$text=$mobile_num . PHP_EOL . $mobile_num . PHP_EOL;
/* I think this is probably what you intended? */
$text=$device_id . PHP_EOL . $mobile_num . PHP_EOL;
$status=file_put_contents( $file, $text, FILE_APPEND | FILE_TEXT );
throw new Exception( $status ? sprintf('All good! Saved %s',$file) : sprintf('Error - unable to save %s',$file) );
} else {
/*
should set permissions if reading/writing of target folder failed
chmod($path,0777); etc
*/
throw new Exception( sprintf( 'The path %s is either not readable or writable',$path ));
}
}catch( Exception $e ){
exit( $e->getMessage() );
}
}
?>
<html>
<head>
<title>ajax-store credentials</title>
</head>
<body>
<form id='bg'>
<input type='text' name='mn' id='mn' placeholder='Mobile number: eg 0141 353 3874' />
<input type='text' name='di' id='di' placeholder='Device ID: eg yellow banana' />
<input type='button' id='bttn' value='Go' />
</form>
<script>
document.getElementById('bttn').onclick=function(e){
var mn=document.getElementById('mn').value;
var di=document.getElementById('di').value;
if( mn != '' && di != '' ){
var xhr=new XMLHttpRequest();
xhr.onload=function(r){
document.getElementById('status').innerHTML=this.response;
};
xhr.onerror=function(r){
document.getElementById('status').innerHTML=err.message;
};
xhr.open('GET','?mn='+mn+'&di='+di,true);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send();
}
}
</script>
<div id='status'></div>
</body>
</html>