使用AJAX帖子而不是表单,但从未满足$ _POST的条件

I'm currently trying to send data to the same page using the selected value in a table row. The user selection gets highlighted and then when the button's onsubmit event is fired, the script is run.

I'm trying to retrieve the $_POST data on the same page, in order for the user to see a new form that they need to fill out and not showing the previous table anymore, but I never get to the inside of an if statement that checks if it isset lik this:

isset($_POST['xx'])

I really don't know what I'm doing wrong here. This is my code so far:

$(".mypost").on("click",function(){
    $.ajax({
        url: "../ajout-correctifs/",
        type: "POST",
        data: { selectedcoll: $("#MyTable1 tr.selected input").val()},
        success: function(response){    
            window.location = "../ajout-correctifs/";
            //alert(response);
        },
        error: function(){
            alert("POST METHOD ERROR : DATA NOT POSTED - IMMINENT PAGE RELOAD..");
            window.location = "../ajout-correctifs/";
        }
    });
});

Additional Information:

  • the $("#MyTable1 tr.selected input") is what helps me retrieve whatever the user has selected.
  • the alert("response") displays html code

EDIT : Here is my php file :

https://raw.githubusercontent.com/SofiaEO/mypage/master/mycode.php

EDIT : SOLUTION in case someone else has the same issue

I deleted the url value + I used $('body').html(response) to print the page.

  1. Check ajax "url" param.
  2. In your success function you have used "window.location". You can not do this because after the redirect you lose the "data" of ajax post and is always false the condition in the "new" redirected page: "isset($_POST['xx'])".

So for avoid this NOT REDIRECT after an ajax response. Remove window.location in your success function;

Visit this: Ajax tutorial for post and get