Ajax发布参数PHP

Why it doesn't work?... Script at main file.

$('#savedocx').submit(function(){
        $.ajax({
            type: "POST",
            url: "savedocx.php",
            data: {q:'1',b:'2'},
            success: function(){
                alert("YESdocs");
            }
        });
    });

And php file "savedocx.php":

    <?php 
if (isset($_POST['q'])){
    echo "Yep!";
}
else {
    echo "Error!";
}
?>

Always error=( In other files i used ajax without problem, but here....

All scripts at main php file:

$(function(){
$('#registerform').submit(function(e){
        e.preventDefault();
        var m_method=$(this).attr('method');
        var m_action=$(this).attr('action');
        var m_data=$(this).serialize();
        $.ajax({
            type: m_method,
            url: m_action,
            data: m_data,
            success: function(result){
                $('#tablereport').html(result);
            }
        });
    });
$('#savexlsx').submit(function(e){
        e.preventDefault();
        var m_method=$(this).attr('method');
        var m_action=$(this).attr('action');
        var m_data=$(this).serialize();
        $.ajax({
            type: m_method,
            url: m_action,
            data: {q:'1',b:'2'},
            cache:false,
            success: function(){
                alert("YESxlsx");
            }
        });
    });
$('#savedocx').submit(function(){
        $.ajax({
            type: "POST",
            url: "savedocx.php",
            data: {q:'1',b:'2'},
            success: function(){
                alert("YESdocs");
            }
        });
    });
});

Your ajax call works fine and also your form. If you check the headers in network tab you will see that on submit it send the q,b data to the php file. Due to the fact that after submit, you get the alert, and when you click ok the network response disappears you can not see the 'Yep!' but i tried create another form that does not refresh with a simple button:

    <html>
<head>
    <title>Autocomplete</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>

<form id="savedocx">
    <input type="button" value="Refresh Page" >
</form>

</body>
</html>
<script>
    $('#savedocx').on('click',function(){
        $.ajax({
            type: "POST",
            url: "test2.php",

            data: {q:'1',b:'2'},
            success: function(){
                alert("YESdocs");
            }
        });
    });
</script>

So when i click the button the only thing that happens is tirgger the ajax call that hits the php file (which is the same as yours).

Checking the response from network tab i get the correct one:

http://prntscr.com/gyz017

I am assuming that savedocx.php is not a sibling of main.php

Change:

url: "savedocx.php",

into:

url: "/absolute/path/to/savedocx.php",

Additionally, check out https://stackoverflow.com/a/21617685/2191572 if you want to debug AJAX calls like a pro.


Try this to lessen the pain of your AJAX

$.ajax({
    type: "POST",
    url: "savedocx.php",
    data: {q:'1',b:'2'},
    success: function( outputFromServer ){
        alert("Success: "+outputFromServer);
    },
    error: function( jqXHR, textStatus, errorThrown ){
        alert("Failed: "+jqXHR+" :: "+textStatus+" :: "+errorThrown);
    }
});

jQuery.ajax() docs are superbly written so don't ignore those.

The answer will find! When i create html-element at second php file (plug-in), i must create ajax-script at THIS file, not at main. Thx all for help.