Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 按下按钮后写入文件_Javascript_Php_Html_Ajax - Fatal编程技术网

Javascript 按下按钮后写入文件

Javascript 按下按钮后写入文件,javascript,php,html,ajax,Javascript,Php,Html,Ajax,尝试在单击按钮时写入txt文件。我不知道怎么做,我的代码也不起作用。我开始觉得我可能需要AJAX?如果是这样,有人能给我一个正确方向的快速指针吗?请提前向我们表示感谢 这是我的密码: <?php //write to file on button event function buttonWrite() { $filename = 'button.txt'; @ $fp = fopen("messages/".$filename, 'wb'); if (!$fp) { echo

尝试在单击按钮时写入txt文件。我不知道怎么做,我的代码也不起作用。我开始觉得我可能需要AJAX?如果是这样,有人能给我一个正确方向的快速指针吗?请提前向我们表示感谢

这是我的密码:

<?php
 //write to file on button event

function buttonWrite()
{
$filename = 'button.txt';
@ $fp = fopen("messages/".$filename, 'wb');
if (!$fp)
{
    echo '<p><strong>Cannot generate message file</strong></p></body></html>';
    exit;
} 

$outputstring  = 'Hello, i have been generated by the button';

fwrite($fp, $outputstring);
}

?>

<html>

<head>
<title>Button Writer</title>
</head>

<body>
<button type="button" onclick="buttonWrite()">Write to File</button>
</body>

</html>

按钮书写器
写入文件
更新! 这些答案实际上都不管用。。不确定是不是我托管的服务器或什么,但是的,就是不工作。。有什么想法吗,伙计们

整个代码

Save.php

 <?php
         //write to file on button event

        function buttonWrite()
        {
        $filename = 'button.txt';
        $outputstring = "Hello, i have been generated by the button'\n";
        file_put_contents($filename, $outputstring, FILE_APPEND | LOCK_EX);
        }

        ?>
整个代码

Save.php

 <?php
         //write to file on button event

        function buttonWrite()
        {
        $filename = 'button.txt';
        $outputstring = "Hello, i have been generated by the button'\n";
        file_put_contents($filename, $outputstring, FILE_APPEND | LOCK_EX);
        }

        ?>

你做错了-javascript和php不能在HTML中同时执行。
您需要做的是创建:

1) “onclick”按钮事件,用于执行对服务器的AJAX请求。为了简单起见,我将在示例中使用

<button id="btn">SAVE FILE</button>

<script type="text/javascript">
  // bind click event to button
  $('#btn').click(function() {
    // send ajax request to save.php (using POST method)
    $.post('save.php', { text: "sample text", function(data) { 
      // output response to console
      console.log(data); 
    });
  });
</script>
保存文件
//将单击事件绑定到按钮
$('#btn')。单击(函数(){
//向save.php发送ajax请求(使用POST方法)
$.post('save.php',{text:“示例文本”,函数(数据){
//对控制台的输出响应
控制台日志(数据);
});
});
2) 创建save.php文件-它将负责将您的数据保存到该文件中

<?php 
  // saving sample text to file (it doesn't include validation!)
  file_put_contents('file.txt', $_POST['text']);

  die('file has been saved');
?>

你做错了-javascript和php不能在HTML中同时执行。
您需要做的是创建:

1) “onclick”按钮事件,它执行对服务器的AJAX请求。为了简单起见,我将在示例中使用它

<button id="btn">SAVE FILE</button>

<script type="text/javascript">
  // bind click event to button
  $('#btn').click(function() {
    // send ajax request to save.php (using POST method)
    $.post('save.php', { text: "sample text", function(data) { 
      // output response to console
      console.log(data); 
    });
  });
</script>
保存文件
//将单击事件绑定到按钮
$('#btn')。单击(函数(){
//向save.php发送ajax请求(使用POST方法)
$.post('save.php',{text:“示例文本”,函数(数据){
//对控制台的输出响应
控制台日志(数据);
});
});
2) 创建save.php文件-它将负责将您的数据保存到该文件中

<?php 
  // saving sample text to file (it doesn't include validation!)
  file_put_contents('file.txt', $_POST['text']);

  die('file has been saved');
?>

创建一个PHP文件。比如说“abc.PHP”。它包含:

<?PHP
$filename = 'button.txt';
@ $fp = fopen("messages/".$filename, 'wb');
if (!$fp)
{
    echo '<p><strong>Cannot generate message file</strong></p></body></html>';
    exit;
} 
else
{
$outputstring  = 'Hello, i have been generated by the button';
fwrite($fp, $outputstring);
Echo "Message inserted";
}
?>

HTML文件

<html>
<head>
<title>Button Writer</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script>
</head>

<body>
<button id="button1" type="button">Write to File</button>
</body>
<script type='text/javascript'>
$('#button1').click(function(){
 $.ajax({
 type: "POST",
 url: "abc.php",
 data: "",
 success: function(msg){
     alert(msg);
 },
 error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert("Some error occured");
 }
 });
});
</script>
</html>

按钮书写器
写入文件
$('#按钮1')。单击(函数(){
$.ajax({
类型:“POST”,
网址:“abc.php”,
数据:“,
成功:功能(msg){
警报(msg);
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
警报(“发生了一些错误”);
}
});
});

创建一个PHP文件。比如说“abc.PHP”。它包含:

<?PHP
$filename = 'button.txt';
@ $fp = fopen("messages/".$filename, 'wb');
if (!$fp)
{
    echo '<p><strong>Cannot generate message file</strong></p></body></html>';
    exit;
} 
else
{
$outputstring  = 'Hello, i have been generated by the button';
fwrite($fp, $outputstring);
Echo "Message inserted";
}
?>

HTML文件

<html>
<head>
<title>Button Writer</title>
<script type='text/javascript' src='//code.jquery.com/jquery-1.10.1.js'></script>
</head>

<body>
<button id="button1" type="button">Write to File</button>
</body>
<script type='text/javascript'>
$('#button1').click(function(){
 $.ajax({
 type: "POST",
 url: "abc.php",
 data: "",
 success: function(msg){
     alert(msg);
 },
 error: function(XMLHttpRequest, textStatus, errorThrown) {
    alert("Some error occured");
 }
 });
});
</script>
</html>

按钮书写器
写入文件
$('#按钮1')。单击(函数(){
$.ajax({
类型:“POST”,
网址:“abc.php”,
数据:“,
成功:功能(msg){
警报(msg);
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
警报(“发生了一些错误”);
}
});
});


Ps.我对AJAX一无所知..所以请不要让它变得太困难您不能从HTML调用PHP函数。正如您所说的,您需要对PHP文件进行AJAX调用,这将反过来超越代码。JavaScript是前端脚本,因此它无法访问服务器上的文件;因此,是的,您确实需要对PHP脚本进行AJAX调用,这样就可以了这是你的工作。这里有一个初学者:jQuery ajax一直是实现ajax功能的最简单的方法,你可以参考这个官方的jQuery链接,Ps.Im对ajax是完全陌生的。所以请不要太难。你不能从HTML调用PHP函数。正如你所说,你需要对PHP文件进行ajax调用,这将反过来,code.JavaScript是前端脚本,因此它无法访问服务器上的文件;因此,是的,您确实需要对php脚本进行ajax调用,这将为您完成这项工作。以下是一个入门课程:jQuery ajax始终是实现ajax功能的最简单方法,您可以参考此官方jQuery链接,js函数o在哪里n单击callback Descripted?那么我应该把文件放在哪里?我向上帝发誓,服务器出了点问题,因为我按了按钮,什么都没发生!:(等等,你需要安装java才能正常运行?因为我在mates PC上,它没有java,我刚刚意识到..可能是这样吗?我把它改成基本上使用HTML提交按钮,你能试试吗?点击回调的js函数描述在哪里?那么我把文件放在哪里?我向上帝发誓,它出了问题服务器之类的,因为我按了按钮,什么也没发生(等等,你需要安装java才能正常运行?因为我的电脑上没有java,我刚刚意识到..可能是这样吗?我把它改成基本上使用HTML提交按钮,你能试试吗?不起作用?知道为什么吗?我需要更改服务器上的权限吗\n什么都没有..我不在我的主电脑上,所以我不能运行WAMP,因此我FTPing到服务器并直接运行..因此没有错误,但是我单击按钮,死亡消息甚至没有执行,这表明它没有使服务器无法工作?知道原因吗?我需要更改服务器上的权限吗\n什么都没有..我不在我的主PC上,因此我无法运行WAMP,因此我FTPing到服务器并直接运行它。.所以没有错误,但是我点击按钮,死亡消息甚至没有执行,这表明它没有走那么远