Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.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 如何使用onClick写入文本文件?_Javascript_Php_Input_File Io_Onclick - Fatal编程技术网

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

Javascript 如何使用onClick写入文本文件?,javascript,php,input,file-io,onclick,Javascript,Php,Input,File Io,Onclick,我创建了一个简单的php/javascript/html页面 它的工作原理如下: 它从SQL查询中获取信息 它使用for来显示表中的所有信息 每行有两个按钮。每个按钮都调用一个函数,该函数在文本文件中写入消息以及该特定行上的信息 我的问题是: 每当我刷新页面,而不是等待我点击一个按钮,然后写消息,一切都写在文件中 我已经尝试将函数调用带到for循环之外,但没有解决问题。我相信,因为它们在循环中,所以每次我刷新页面时,两个按钮都“被点击” 我还尝试将类型从button更改为submit,但没有

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

它的工作原理如下:

  • 它从SQL查询中获取信息
  • 它使用
    for
    来显示表中的所有信息
  • 每行有两个按钮。每个按钮都调用一个函数,该函数在文本文件中写入消息以及该特定行上的信息
我的问题是: 每当我刷新页面,而不是等待我点击一个按钮,然后写消息,一切都写在文件中

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

我还尝试将类型从button更改为submit,但没有成功

下面是我正在使用的代码:

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


函数First_Message(){
}
函数第二条消息(){
}
我希望输出仅为文本文件上写入的一行消息,而这条消息是我根据单击这两个按钮中的任何一个来决定的

如果有人能告诉我我做错了什么,或者/或者告诉我目前使用onClick做什么的替代方案,我认为这就是问题所在,我会非常感激


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

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

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

您不能以这种方式混合使用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; ?>


您不能以这种方式混合使用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”
如果此答案对您有用,请将其标记为正确。谢谢。你所做的更新解决了我的问题。感谢您的关注和耐心。谢谢@G3ck。尽管您的代码可以正常工作,而且与我之前所想的相比,它看起来绝对是一种改进,但我的问题仍然是按钮仍然需要位于
for
循环中。每当我尝试这样做,它总是得到相同的值。我怎样才能使单击“text=“+my_text”时只获取某一行的信息?@Lucas Lisboa刚刚编辑了我答案中的代码。您可以将文本内联放置
value=“Clicked button”
如果此答案对您有用,请将其标记为正确。谢谢。你所做的更新解决了我的问题。谢谢你的关注和耐心。谢谢你的回答。似乎是这样的,另一个答案也指出我需要用ajax来解决这个问题。谢谢你的回答。似乎是这样,另一个答案也指出我需要使用ajax来解决这个问题。