我如何调用if语句PHP中的按钮

时间:2014-02-09 06:32:58

标签: php html button post if-statement

我是新手我在如何调用if语句中的按钮时遇到一些问题。我正在尝试在用户日志上工作,当您单击按钮时,它将在用户日志上输出。

但是如何调用>> 我的if语句中的VIEW按钮,以便它在userlog.txt中的fwrite

if($_POST['submit'] = ")内的

位于搜索按钮和添加编辑删除按钮中。你明白了吗?

<body background="images.jpg">
<?php
session_start();

if($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a><br>";
echo '<FORM METHOD="LINK" ACTION="mydata4.php">
<INPUT TYPE="submit" VALUE="Edit/Delete/add">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="mydata2.php">
<INPUT TYPE="submit" VALUE="View" id="viewbutton">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Search">
</FORM>';
}
else
{
die("You must be logged in!");
}


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

3 个答案:

答案 0 :(得分:1)

使用

if(isset($_POST))

而不是

if($_POST['submit'] = ")

答案 1 :(得分:0)

您可以为提交按钮设置相同的名称 ...

<input type="submit" name="submit" value="View" />
<input type="submit" name="submit" value="Search" />

在您的PHP中,您只需按照提及的那样使用

if( $_POST["submit"] == "View" )
...
if( $_POST["submit"] == "Search" )
...

还有一件事是,如果你想在php中使用method="post",你必须在<form>标记中使用$_POST

<form method="post" action="....php">

答案 2 :(得分:0)

试试这个......你在if条件下做错了。

<body background="images.jpg">
<?php
session_start();

if($_SESSION['username'])
{
echo "Welcome, ".$_SESSION['username']."!<br><a href='logout.php'>Logout</a><br>";
echo '<FORM METHOD="LINK" ACTION="mydata4.php">
<INPUT TYPE="submit" VALUE="Edit/Delete/add">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="mydata2.php">
<INPUT TYPE="submit" VALUE="View" id="viewbutton">
</FORM>';
echo '<FORM METHOD="LINK" ACTION="display_data.php">
<INPUT TYPE="submit" VALUE="Search" name="submit" id="submit">
</FORM>';
}
else
{
die("You must be logged in!");
}


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