Reputation: 85
I have a trouble receiving data sent using ajax in a php file. It seems that ajax sends data, cause I get a success message, but php page shows me an error "Notice: Undefined index: ab".
Here is my jquery code for sending data using AJAX:
$('#download-btn').click(function(){
var ab="abc";
$.ajax({
url:'generate_url.php',
data:{'ab':ab},
type:'post',
success:function(data) {alert("sent");},
error: function(request, status, error) {alert(request,status);}
});
}
And that's how I extract data in a generate_url.php:
<?php
$a = $_POST['ab'];
echo $a;
?>
Thanks in advance for your help!
Upvotes: 1
Views: 4777
Reputation: 7096
You were missing ')' in your javascript code
This will work:
$('#download-btn').click(function () {
var ab = "abc";
$.ajax({
url: 'generate_url.php',
data: {'ab': ab},
type: 'post',
success: function (data) {
alert("sent");
},
error: function (request, status, error) {
alert(request, status);
}
});
});
Upvotes: 0
Reputation: 16122
You are missing the );
from your code. For simple POST
I'll advice you to use $.post
(there's nothing special about $.post
, it's a shorthand way of using $.ajax
for POST
requests.
$('#download-btn').on('click', function(){
var ab="abc";
$.post('generate_url.php', {'ab':ab}, function(data){
console.log(data);
});
});
Upvotes: 1
Reputation: 1621
<script type="text/javascript">
$(document).ready(function($) {
$('#download-btn').click(function(){
var ab='abc';
$.ajax({
url: 'generate_url.php',
type: 'POST',
data: {'ab':ab}
})
.done(function(respose) {
//todo
})
});
})
</script>
Upvotes: 0