如何使用php选择按钮并进入数据库?

时间:2010-11-17 08:22:04

标签: php html

这是我目前的代码..

<form action="js.php" method="post">
<input type="button" id='approve' value="Yes" onclick="a()" class="approve" />
<input type="button" id='reject' value="No" onclick="r()" class="reject"/>
<input type="submit" name="submitted" value="TRUE" /> 
</form>

这是我的表单,页面标题是js.php ..

我希望如果用户点击任何按钮,按钮的名称应该在数据库中输入到标题响应下的响应表中。

我该怎么做?谢谢..

我做了一些PHP部分......

<?php

require_once('connect.php');

if(isset($_POST['submitted']))
{
    //if on button click

    mysqli_select_db($connect,"check");

    //entering query


}
    ?>

这是我用来输入数据的内容

<?php

require_once('connect.php');


    //if on button click

    mysqli_select_db($connect,"pubd");

    //entering query
    if ($_POST['action'] == 'approve') {
    // user clicked Yes
    $yes = "insert into response (button) values ('approve')";
    $yesr =  mysqli_query($connect,$yes) or die(mysqli_error($connect));
} else if ($_POST['action'] == 'reject') {
        $no = "insert into response (button) values ('reject')";
    $nor =  mysqli_query($connect,$no) or die(mysqli_error($connect));
}



    ?>
<form action="js.php" method="post">
<input type="button" id='approve' name="action" value="Approve" onclick="a()" class="approve" />
<input type="button" id='reject' value="Reject" name="action" onclick="r()" class="reject"/>

</form>

1 个答案:

答案 0 :(得分:0)

如果你只是在寻找答案,如何检查点击了哪个按钮,这里是:

HTML:

<input type="submit" id='approve' value="Yes" name="action" class="approve" />
<input type="submit" id='reject' value="No" name="action" class="reject"/>

PHP:

if ($_POST['action'] == 'Yes') {
    // user clicked Yes
} else if ($_POST['action'] == 'No') {
    // user clicked No
}