0

I've tried to get some values with button through jquery into php. Which i've done like this.

HTML

<button class="button" name="reject" value="<?php echo $row['mail']; ?>" type="submit">reject</button>

Jquery

$(document).ready(function(){
    $('.button').click(function(){     

        var url      = window.location.href;
        var whichBtn = $(this).attr("name");
        var mail = $(this).attr("value");

        var ajaxurl = url,
        data =  {'mail': mail, 'whichBtn' : whichBtn};

        $.post(ajaxurl, data, function (response) {
            // Response div goes here.   
            alert(whichBtn);
            alert("action performed successfully");
        });
    });
});

PHP

if(isset($_POST['mail']))
     echo $_POST['mail'];

Well, the thing is that POST[mail] is not set and I don't have clue why.. Could you help?

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
  • 1
    Any error ? What is being sent over server ? Did you check network-tab ? – Rayon Aug 24 '16 at 17:12
  • basic debugging: `var_dump($_SERVER['REQUEST_METHOD'], $_POST)` and see what's arriving. if it's NOT a post, or your _POST is empty, then you at least have an idea of where to start looking. – Marc B Aug 24 '16 at 17:12

1 Answers1

1

You need to add event.preventDefault() to your click handler. Since it is a submit button it is navigating away from the page before your Javascript gets executed. Try this:

$(document).ready(function(){
    $('.button').click(function(event){     
        event.preventDefault();
        var url      = window.location.href;
        var whichBtn = $(this).attr("name");
        var mail = $(this).attr("value");

        var ajaxurl = url,
        data =  {'mail': mail, 'whichBtn' : whichBtn};

        $.post(ajaxurl, data, function (response) {
            // Response div goes here.   
            alert(whichBtn);
            alert("action performed successfully");
        });
    });
});
nick.graziano
  • 677
  • 6
  • 12