-1

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");
            }
        });
    });
});
OulinaArt
  • 324
  • 4
  • 13
  • 1
    What error are you getting? What do mean by "doesn't work"? – Jay Blanchard Oct 18 '17 at 14:18
  • I want get echo "Yep!". But always returns "Error!" – OulinaArt Oct 18 '17 at 14:23
  • this code should work just fine. Error must be due to something else. Post full code of `savedocx` – mega6382 Oct 18 '17 at 14:23
  • it's full savedox.php. At main file i have form and table, which i fiil up with other ajax-loader with report.php file. Ajax works good at this function (from main to report.php). BUT when i use ajax at this script, it cannt give parameters at php..I understand, that i have error, but where?( – OulinaArt Oct 18 '17 at 14:30
  • When i posted one parameter at first ajax-loader and took result, can i use other ajax-loader at similar page with other parameters? – OulinaArt Oct 18 '17 at 14:35
  • What does your HTML look like? – brombeer Oct 18 '17 at 14:41

3 Answers3

0

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

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • Thak u, but it didn't help me... Can you tell me, why i can send one ajax-call and it works! but i cann't send other parameters other ajax at other .php file? main file is one for both ajax-call... – OulinaArt Oct 18 '17 at 14:53
  • What do you mean other parameters. If you want to send the data in the right way through ajax you must set :dataType for example 'json' and then inside the data you can pass the data you want to send. – pr1nc3 Oct 18 '17 at 14:57
  • i cann't give you all files=(( they are very big...and i use at this moment main php file (with 3 ajax-call) and 3 php files, which takes parameters...i call first ajax, take result, good. i call other ajax and it cann't give parameters to my second php file... – OulinaArt Oct 18 '17 at 15:03
  • i cann't take even one parameter...strange problem=((( – OulinaArt Oct 18 '17 at 15:07
  • I am pretty sure the problem is the "submit" in your ajax. The page refresh so you lose all the data that you would have send. Why don't you do it like in the example i gave you and put an anchor so when the user clicks the button something will happen but not refresh the page. – pr1nc3 Oct 18 '17 at 15:11
  • i canged to 'on(click)' but it didn't help me... $('#registerform').submit(function(e)...) - works good. but $('#savexlsx').submit(function(e)...) or $('#savedocx').submit(function(e)...) don't work – OulinaArt Oct 18 '17 at 15:13
  • Change also the html code. If you see in my html i removed the button type submit with a normal button. Check my html code also. – pr1nc3 Oct 18 '17 at 15:16
  • http://rgho.st/8z2ZTSWWz can you look at these files?=( i cann't understand, what i do wrong?... button and onclick didn't help...i tried find solution six hours=(( – OulinaArt Oct 18 '17 at 15:24
  • Just from a fast review try to remove the / from id and name. You try to use them as escape characters but maybe they interfere with the jquery selector. I am referring in the echoing part you have that you create the html element. – pr1nc3 Oct 18 '17 at 15:34
0

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.

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
0

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.

OulinaArt
  • 324
  • 4
  • 13