1

POST method return undefined value, but GET method working fine. I even tried $.post(url, data, success); << $.post even worse GET and POST can't work.

<input type="submit" value="submit"  id="btnUpload">


$('#btnUpload').click(function(){       
$.ajax({  
    type: 'POST',  
    url: 'ajax.php',
    data: { 'action': 'scan_email' },
    success: function(response) {
        alert(response);
        }
    });

ajax.php

<?php echo $_POST['action'];?>
1
  • what do you have on your form action?can you add html mark up? Commented Apr 20, 2016 at 2:22

2 Answers 2

1

Use method instead of type:

Code:

<input type="submit" value="submit"  id="btnUpload">

$('#btnUpload').click(function(){       
    $.ajax({  
    method: 'POST',  
    url: 'ajax.php',
    data: { 'action': 'scan_email' },
    success: function(response) {
       alert(response);
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

It works. Why method instead of type? any different method, type, datatype?
Actually, type is an alias of method but it matters with jQuery version.
I see. I see all tutorials and stack overflow answer all almost are using type.
0

Added the jquery library

<input type="button" value="submit" id="btnUpload">

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){ 

    $('#btnUpload').on('click', function(){ 

        $.ajax({
                type: "POST",  
                url: "login.php",  
                data: { 'action':'scan_email'},
                success: function(theResponse) {

                    // Output from ajax.php
                    alert(theResponse); // comment this

                    }  
            });
    });
});                 
</script>

2 Comments

I have added. otherwise it can't call php and return undefined value. Thanks sir.
yeah. Need to check the library version also, it matters with the events. Use 'on' instead 'click' which works better for dynamically added elements.

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.