0

我有一个看起来像这样的 HTML 表单:

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

Item1 S:<input value="remove" name="remove1S" type="submit" />
Item2 M:<input value="remove" name="remove1M" type="submit" />

</form>

和 PHP:

$file = fopen("stock.txt", "a+");
//when "remove1S" is clicked on
fwrite($file,"Removed one");
//when "remove1M" is clicked on
fwrite($file,"Removed two");

我该怎么做我在评论中所说的?我确定它正在使用类似的$_POST['remove1S']东西

if (__onClick__ $_POST['remove1S'])
{fwrite($file,"Removed one");
}
4

3 回答 3

0

isset()呢?它会检查是否$_POST['remove1S']存在。$_POST如果您已提交表单,则此密钥将存在。

if (isset($_POST['remove1S']))
{
    fwrite($file,"Removed one");
}
于 2013-09-15T16:31:47.013 回答
0

要在操作之间进行分隔,请使用 empty()isset()检查单击按钮的值是否不为空,然后单击它,如果您想在不刷新页面的情况下执行此操作,则需要AJAX.    

    $file = fopen("stock.txt", "a+");

    if (!empty($_POST['remove1S']))
    {
        fwrite($file,"Removed one");
    }
    else {
        //remove1M is clicked
        fwrite($file,"Removed two");

    }

或者 :

    $file = fopen("stock.txt", "a+");

    if (isset($_POST['remove1S']))
    {
        //button remove1S is clicked

        fwrite($file,"Removed one");
    }
    else {
        //remove1M is clicked
        fwrite($file,"Removed two");

    }
于 2013-09-15T16:35:17.903 回答
0

您可以更改按钮的 onClick 事件以包含 PHP 代码,例如:

<input value="remove" name="remove1S" type="submit" onClick="<?php fwrite($file,"Removed one");?>"/>
于 2013-09-15T16:44:09.813 回答