duancuisan2503 2017-02-14 16: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 17: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

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

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型