【问题标题】:how can i run my code inside the ifstatement when i click the button [closed]当我单击按钮时,如何在 if 语句中运行我的代码[关闭]
【发布时间】:2014-02-18 05:26:53
【问题描述】:

大家好,我在这段代码中需要帮助,我想做一个用户日志,输出为 .txt 我的问题是当我单击按钮时。示例 VIEW 按钮。下面的if语句不起作用..不知道为什么..

我正在尝试调用此代码

echo '<FORM METHOD="POST" ACTION="mydata2.php">
<INPUT TYPE="submit" name="submit" value="View">
</FORM>'; 

if( isset($_POST['submit']) && ($_POST['submit'] == "View") )
{   
$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username";
fwrite($fh, "$stringData".PHP_EOL);
$stringData = "Clicked View Button $date";
fwrite($fh, "$stringData".PHP_EOL);
fclose($fh);
}

抱歉英语不好.. 当您单击查看按钮时,我想运行上面的代码。你们明白我的意思了吗> ;/我不明白.. 它不起作用

【问题讨论】:

  • 上面的PHP代码是来自mydata2.php还是其他文件?
  • 什么不起作用? if 语句有效吗?是否尝试写入文件并失败?
  • 首先想到的是:JavaScript 执行客户端,PHP 执行服务器端。您可能想查看带有FILE_APPEND 标志的file_put_contents
  • 是的,先生 mydata2.php 是另一个文件。我想制作一个日志,在我转到 mydata2.php 之前,if 语句将起作用,以便将其打印在 userlog.txt
  • 点击按钮视图时没有写入文件

标签: php button logging if-statement


【解决方案1】:

试试这个

file1.php

echo '<from method="POST" action="mydata2.php">
<input type="submit" name="submit" value="View">
</form>'; 

mydata2.php

在这个$username 没有定义你应该定义它。

if( isset($_POST['submit']) && ($_POST['submit'] == "View") )
{   
   $username = "your_username"; // define it

  $date=date("Y-m-d H:i:s");
  $updatefile = "userlogs.txt";
  $fh = fopen($updatefile, 'a') or die("can't open file");
  $stringData = "User: $username";
  fwrite($fh, $stringData.PHP_EOL);
  $stringData = "Clicked View Button $date";
  fwrite($fh, $stringData.PHP_EOL);
  fclose($fh);
}

【讨论】:

  • 是的!非常感谢先生!!这对我有很大帮助:)))))
【解决方案2】:

我已经尝试过这段代码,它正在工作。您可能会丢失您的数据。我有硬编码的数据。

 <?php

 echo '<FORM METHOD="POST" ACTION="mydata2.php">
 <INPUT TYPE="submit" name="submit" value="View">
 </FORM>'; 

if( isset($_POST['submit']) && ($_POST['submit'] == "View") )
{

    $username = 'test';   
    $date = date('Y-m-d');
    $stringData = 'String data';
    $date=date("Y-m-d H:i:s");
    $updatefile = "userlogs.txt";
    $fh = fopen($updatefile, 'a') or die("can't open file");
    $stringData = "User: $username";
    fwrite($fh, "$stringData".PHP_EOL);
    $stringData = "Clicked View Button $date";
    fwrite($fh, "$stringData".PHP_EOL);
    fclose($fh);
}

【讨论】:

    【解决方案3】:

    这是经过测试并且工作正常..请参阅我所做的更改。

    <?php
      echo '<form method="POST" action="">
            <input type="submit" name="submit" value="View">
            </form>'; 
    
    if( isset($_POST['submit']) && ($_POST['submit'] == "View") )
    {   
     $date=date("Y-m-d H:i:s");
     $updatefile = "userlogs.txt";
     $fh = fopen($updatefile, 'a') or die("can't open file");
     $stringData = "User: ";
     fwrite($fh, "$stringData".PHP_EOL);
     $stringData = "Clicked View Button $date";
     fwrite($fh, "$stringData".PHP_EOL);
     fclose($fh);
    }
    ?>
    

    【讨论】:

    • 谢谢你的回答先生:)
    猜你喜欢
    • 2023-03-12
    • 2020-10-23
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多