1

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.

SofiaEO
  • 29
  • 6
  • can you show us the php code? – kscherrer Feb 14 '17 at 16:33
  • @Cashbee Noted. I just edited my question but here's the link once again (It has around 300lines) : https://raw.githubusercontent.com/SofiaEO/mypage/master/mycode.php – SofiaEO Feb 14 '17 at 17:00
  • please don't link to an external source of 300 lines. read: http://stackoverflow.com/help/mcve – Nanne Feb 14 '17 at 17:57
  • @Nanne hey, I see you are a regular here, whereas this is my first-ever question, so can you help me with this ? Do you have any idea on what I can possibly do ? – SofiaEO Feb 14 '17 at 22:38
  • Well, read that link -> the easiest thing for anyone willing to provide you with an answer is just to see what you do. So, as that link says, make an example that shows the behaviour, but doesn't have any other code. so a small html, js, php example with just a couple of lines each, that shows what you have, and then a question that explains what you want and what it is that doens't work. It's very easy to see that your php file contains all sort of specific code for your project that hasn't got anything to do with the question. But read that link, I added it deliberately :) – Nanne Feb 15 '17 at 08:09
  • @Nanne Well how nice of you to point out what is wrong with how I asked for help. I didn't know what to post and what not to post because as I was working on my problem I thought that the simple fact of having many forms cramped up in one page and maybe the many conditions I have could result in the data I am looking to retrieve to be lost. I will most certainly take notice of your remarks though my problem still remains but thank you anyways. – SofiaEO Feb 15 '17 at 08:21

1 Answers1

0
  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

Community
  • 1
  • 1
Alberto Favaro
  • 1,824
  • 3
  • 21
  • 46
  • I just tried out the solution you posted; got rid of the redirection and all but the thing is when I inject the success function's response onto the div element it just prints out html as plain text; what I want to retrieve is specifically the $_POST value not to be injected in an HTML element but to be used as a variable. – SofiaEO Feb 15 '17 at 08:39
  • Plus, the other part of the page that loads when $_POST is set doesn't show, instead I have my table in which I select a value to be posted and the plain HTML code right after.. – SofiaEO Feb 15 '17 at 08:46
  • For showing the html code below the "isset" use jquery in your success function. Find that code (ex: $(response).find('#mytable');) and after append the finded code where you want in your current html page. – Alberto Favaro Feb 15 '17 at 13:17
  • Probabily becouse in your data ajax, you submit only one parameter. You post in ajax only "selectedcoll" param, other params is empty. – Alberto Favaro Feb 15 '17 at 13:20
  • 1
    Thank you for your help; the comments, tips and link all helped me. – SofiaEO Feb 15 '17 at 17:15