-1

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?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jack Maessen
  • 1,780
  • 4
  • 19
  • 51
  • 1
    if both buttons have the same name, then `$_POST['btnName']` will have the value of whichever one was clicked. I also believe that only whichever button was clicked would be in the `$_POST` data if they have different names. Been a while since I've dealt with this. – Jonathan Kuhn Aug 03 '16 at 22:51
  • use `isset()` on a conditional – Funk Forty Niner Aug 03 '16 at 22:52
  • 1
    Kuhn is right. Give them the same name and check for different value. – developerwjk Aug 03 '16 at 22:54
  • I tried it with the same name and different values but without result; problem is in the AJAX call? Post above updated – Jack Maessen Aug 03 '16 at 23:15
  • @JackMaessen I posted something below before you posted your full/real code. If this is a jquery issue, I am so not the guy for this. I work serverside mostly. If what I posted below isn't what you were looking for, please tell me so I can delete it and avoiding possible downvotes. – Funk Forty Niner Aug 03 '16 at 23:19
  • @Fred unfortunately, i tried your option as well but also without result – Jack Maessen Aug 03 '16 at 23:20
  • @JackMaessen No worries. I deleted it. Good luck ;-) wish I could have been of more help. – Funk Forty Niner Aug 03 '16 at 23:23

1 Answers1

3

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>     
Kazakh
  • 104
  • 5