1

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!

10
  • It looks like it should work. What does var_dump($_POST); show? Commented Dec 8, 2016 at 20:30
  • How are you seeing what's in the PHP page? The success function doesn't display data. Commented Dec 8, 2016 at 20:32
  • 5
    You're missing the ) at the end of the function. Commented Dec 8, 2016 at 20:32
  • success:function(data) { alert("sent...next see the data..."); alert(data); }, is one way, but you need to find your javascript console. what browser are you using? Commented Dec 8, 2016 at 20:34
  • @WEBjuju I know that's how he could see it, but I wonder what he's actually doing. My suspicion is that he's looking at a different page, and expecting the variable to stick around. Commented Dec 8, 2016 at 20:38

4 Answers 4

1

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);
    }); 
});
Sign up to request clarification or add additional context in comments.

Comments

1

You are missing ); at the end of the ajax code.

Comments

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

1 Comment

why document instead of button click?
0

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);
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.