Reputation: 1
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?
Upvotes: 0
Views: 61
Reputation: 678
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");
});
});
});
Upvotes: 1