【发布时间】: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