1

So this is probably a fairly common problem, but I don't know how to solve it. I have 3 PHP pages. We'll call them A, B, and C. Page C is included in page B and inflates a div with a table.

I need to click a cell in my table on page A and grab 1 piece of data, store it in a variable, then send it to page C with AJAX but only after page B has loaded.

So this is what I have so far

jQuery

$('.selectTable td:first-child').on("click", function(e) {
    var title = $(this).text();
    console.log(title);
    window.location = 'pageB.php';
    $.ajax({
        url:'core/functions/pageC.php',
        type: 'post',
        data: {'title' : title}
    }).fail (function() {
        alert('error');
    }).always(function(data) {
        accountId = $.trim(data);
    });
});

Then this is how I call "page C" from "page B"

<div class="myDiv">
     <?php include 'core/functions/pageC.php'; ?>
</div>

Then inside pageC

if(isset($_POST['title'])) {
    ...inflate table, do stuff etc.
} else {
    echo 'could not load table';
}

So after I click any "td:first" table cell my alert dialog pops up and says "error" then page B loads and inside the div it has my message 'could not load table'. I console logged the variable, and it is being passed. I think the problem is I'm trying to pass Page C data before Page B has loaded. So when Page B does load it then loads page C again without the post variable being set. Then again I could be dead wrong, just a hunch.

So where am I going wrong? OR Should I be attempting this a different way?

Thanks for your time

i_me_mine
  • 1,435
  • 3
  • 20
  • 42
  • It looks like you're redirecting before the ajax call. Have you tried pushing `window.location` below your `$.ajax` method? – 13ruce1337 Mar 26 '14 at 20:45
  • On click td you wish than load page B after ajax call is this, or you dont need redirect? – gabrieloliveira Mar 26 '14 at 20:47
  • @13ruce1337 ya, same outcome. Furthermore if I remove the window.location. I don't get the alert. It posts just fine but obviously the page with the data wont be visible – i_me_mine Mar 26 '14 at 20:48
  • @i_me_mine what about in the `success:function(){}`? – 13ruce1337 Mar 26 '14 at 20:50
  • @13ruce1337 interesting. The alert dialog does not show, but the div still holds the message "could not load table". – i_me_mine Mar 26 '14 at 20:51
  • hmmm then there must be a better way to do this...I'm hoping some reputable character swoops in because I'm interested now. +1 – 13ruce1337 Mar 26 '14 at 21:00
  • @13ruce1337 lol, thanks.Ya I would assume this would be a fairly common practice. I just think I'm trying to do it wrong, as in square peg-round hole. – i_me_mine Mar 26 '14 at 21:03
  • 1
    Remember that http is stateless (the link below might be useful). As soon as you change location to PageB everything from PageA (including your ajax to PageC) is dead and gone. Anything you want to pass from A to C must be passed through B (via _REQUEST, or cookies or _SESSION) http://stackoverflow.com/questions/4913763/what-does-it-mean-when-they-say-http-is-stateless – Ken Mar 26 '14 at 22:12
  • @Ken That's exactly what I was afraid of. I'm working on a solution now where I pass the data to B, then because C is included in B, pass the data that way. Might sound confusing now, but it'll make sense when I post it. Thanks again for the confirmation. – i_me_mine Mar 26 '14 at 22:16
  • @i_me_mine You also need to remember that each page is delivered to the browser as a new-born entity - any server side/PHP computation has finished long before the page arrives. Maybe look at PageA calling a service PageB and rewriting itself with the response? Googling for jQuery/ajax examples should turn up lots of results. – Ken Mar 26 '14 at 22:29
  • 2
    @Ken thank you sir. I have already begun. – i_me_mine Mar 26 '14 at 23:16

0 Answers0