【问题标题】:How to write to text file using onClick?如何使用 onClick 写入文本文件?
【发布时间】:2019-05-17 16:39:48
【问题描述】:

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

它是这样工作的:

  • 它从 SQL 查询中获取信息;
  • 它使用for 来显示表格上的所有信息;
  • 每行有两个按钮。每个按钮都会调用一个函数,该函数会在文本文件中写入一条消息以及该特定行的信息。

我的问题: 每当我刷新页面时,不是等待我单击按钮然后写入消息,而是将所有内容都写入文件中。

我已经尝试将函数调用带到for 循环之外,但这并没有解决。我相信,因为它们在循环中,所以每次我刷新页面时,两个按钮都会“被点击”。

我也尝试将类型从按钮更改为提交,但没有成功。

这是我正在使用的代码:

<?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
?>

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

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

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

【问题讨论】:

    标签: javascript php input file-io onclick


    【解决方案1】:

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

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

    什么是ajax?

    AJAX 是一种用于创建快速和动态网页的技术。 AJAX 允许通过在后台与服务器交换少量数据来异步更新网页。这意味着可以更新网页的某些部分,而无需重新加载整个页面。

    简单代码:

    <?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; ?>
    

    【讨论】:

    • 谢谢@G3ck。尽管您的代码有效,并且看起来绝对比我所考虑的有所改进,但我的问题仍然是按钮仍然需要位于 for 循环内。每当我尝试这样做时,它总是得到相同的值。我怎样才能使单击“text =”+ my_text 时仅获取某一行的信息?
    • @Lucas Lisboa 刚刚在我的答案中编辑了代码。您可以将文本内联value="Clicked button &lt;?php echo $x; ?&gt;"
    • 如果此答案对您有用,请标记为正确。
    • 谢谢。您所做的更新解决了我的问题。感谢您的关注和耐心。
    【解决方案2】:

    这是不可能的。 PHP 在服务器端执行,而 javascript(和 html)在浏览器端执行。 您必须从浏览器(例如使用 javascript,在 ajax 中)发送到服务器,并在 php 中处理它。

    【讨论】:

    • 感谢您的回答。好像是这样的,另外一个回答也指出我需要用ajax来解决这种情况。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 2012-03-04
    • 2013-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多