如何使用$ _POST在php中区分2个提交

时间:2016-08-03 22:48:41

标签: php jquery ajax forms

我有一个表格,其中有两个输入如下:

<input class="view" type="submit" name="viewonly" value="View">
<input class="submit" type="submit" name="vote" value="Vote">

我想实现这个目标:

if(hit on input with name 'viewonly') {
echo 'you choose viewonly';
}
elseif(hit on input with name 'vote') {
echo 'you choose vote';
}

我该怎么做?

更新 我的表格:

<form method="post" id="quickpoll">
.........
<input class="view" type="submit" name="quickpollsubmit" value="View">
<input class="quickpollsubmit" type="submit" name="quickpollsubmit" value="Vote">
   </form>

ajax电话:

$(document).on('submit', '#quickpoll', function()
{
var data = $(this).serialize();
$.ajax({

    type : 'POST',
    url  : 'quickpoll_vote.php',
    data : data,
    success :  function(data)
               {                        
                    $("#quickpoll").fadeOut(500).hide(function()
                    {
                        $(".quickpollwrapper").fadeIn(500).show(function()
                        {
                            $(".quickpollwrapper").html(data);

                        });


                    });

               }
    });
    return false;
});

quickpoll_vote.php

if($_POST)
{

    if($_POST['quickpollsubmit'] == 'Vote') {
    echo 'you did vote and not view only';
    }

回声没有出现,所以问题是我想在AJAX调用中?

1 个答案:

答案 0 :(得分:3)

也许你几乎没有像这样改变你的代码:

<input class="view myclass" type="button" name="viewonly" value="View">
<input class="submit myclass" type="button" name="vote" value="Vote">
<script language="javascript">
     $('.myclass').click(function() {
         clickbuttonname = $(this).attr('name');
         var data = $(this).serialize()+'&clickbuttonname='+clickbuttonname;
         .....
     })
</script>