如何使用 onClick 写入文本文件?

How to write to text file using onClick?

我创建了一个简单的 php/javascript/html 页面。

它是这样工作的:

我的问题: 每当我刷新页面,而不是等待我点击一个按钮然后写消息,一切都被写入文件。

我已经尝试将函数调用带到 for 循环之外,但这并没有解决。我相信,因为它们在循环内,每次我刷新页面时,两个按钮 "are clicked".

我也试过把类型从按钮改成提交,但是没用。

这是我正在使用的代码:

<?php   
for($i=0;$i<count($aux);$i++){
?>
 <tr>
 <td><?php echo $i+1; // Simple counter, to have everything in order ?></td>
 <td><?php echo $aux[$i]['NAME']; // Displays the name?></td>
 <td><?php echo $aux[$i]['INFO']; // Display some information?></td>

<script>

function First_Message(){
 <?php  // If the first button is pressed, then this function is called
 $contents .= "Message: ".trim($aux[$i]['NAME'])." message #1 ".date("H:i:s");
 $success = file_put_contents("file.txt", $contents);
 ?>
 }
</script>

<script>
function Second_Message(){
 <?php  If the second button is pressed, then this function is called
 $contents .= "Please,: ".trim($aux[$i]['NAME'])." message #2 ".date("H:i:s");
 $success = file_put_contents("file.txt", $contents);
 ?>
 }
</script>

<!-- Creates the first button -->

<td><input type="button" value="message_1" name = "balcao" onClick = "First_Message()" /></td>

<!-- Creates the second button -->

<td><input type="button" value="message_2" name = "balanca" onClick = 
"Second_Message()" /></td>

</tr>
<?php   
} // End Loop
?>

我希望输出只是写入文本文件的一行消息,而这条消息是我根据点击这两个按钮中的任何一个按钮决定的。

如果有人能告诉我我做错了什么 or/and 告诉我我目前正在使用 onClick 做的事情的替代方法,我认为这是问题所在,我将不胜感激。

如果需要,请随时询问更多信息。

这不可能。 Php在服务器端执行,而javascript(和html)在浏览器端执行。 您必须从浏览器(例如 javascript,在 ajax 中)发送到服务器,并在 php.

中处理它

不能以这种方式混合使用javascript和php功能。

我想到的第一个解决方案是一个简单的 ajax 查询。

什么是 ajax?

AJAX is a technique for creating fast and dynamic web pages. AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

简单代码:

<?php
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest' && isset($_POST['text'])):
    $output = '';
    $filename = 'file.txt';
    if (!file_exists($filename)):
        $myfile = fopen($filename, "w");
    endif;
    file_put_contents($filename, $_POST['text']);   
    $output = array("File content created");
    die(json_encode($output));
else:
?>

<?php
for ($x = 0; $x <= 10; $x++):
?>
    <button id="button" type="button" value="Clicked button <?php echo $x; ?>">A am button <?php echo $x; ?></button>
<?php
endfor;
?>

<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>     
<script>
$(document).on('click','#button',function(e) {
    var my_text = $(this).val();

    $.ajax({
        type: "POST",
        dataType:"json",
        url: window.location.href,
        data: "text=" + my_text,
        beforeSend: function (data) {
            alert('Sending');
        },
        success: function (data) 
        {
            alert('Success');

        },
        error: function (data) 
        {
            alert('Error');
        }
    });     
});
</script>
<?php endif; ?>