duancuisan2503 2017-02-14 08:31
浏览 23
已采纳

使用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条回答 默认 最新

  • duanpasi6287 2017-02-14 09:54
    关注
    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部