Jack Maessen Jack Maessen - 3 months ago 5
Ajax Question

how to distinguish 2 submits from each other in php with $_POST

I am having a form in which 2 inputs like below:

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


I want to achieve this:

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


How can i do that?

Update:
My form:

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


The ajax call:

$(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';
}


The echo does not appear, so the problem is i think in the AJAX call?

Answer

Maybe you can little change you code like this:

<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>     
Comments