Question

I have set up the following:

  1. Database class ($db)
  2. Pagination class ($paginator)

I am attempting to write a basic system to let me administrate pages. I have a page "page_manager.php" in which I include both my database class (database.php) and my pagination class (paginate.php).

In my pagination class I have a function which echoes my SQL data. I've come up with a way to echo an HTML < select > element with the necessary IDs, which allows me to successfully echo the corresponding results (10 per page), based on the value of the < select > element. So, "1" will echo the first 10 results in the database, "2" will echo from 11-20, "3" will echo from 21-30, etc., etc..

I have added an onChange event to the < select > element which will copy its value (using "this.value") to a hidden form field. I then submit this form using document.getElementById().submit();

This will then add the $_GET variable to the URL, so the URL becomes ".../?pagenumber_form=X". However, when I try to grab this value back from the URL, the $_GET['pagenumber_form'] is empty.

Some code:

<span style='font-family: tahoma; font-size: 10pt;'>Page #</span>
    <select id="page_number_selection" 
    onchange='javascript: document.getElementById("pagenumber_form").value = this.value;
                          document.getElementById("pagenumber").submit();'>
    <?php
        for($i = 1; $i <= $this->num_pages; $i++)
            echo"<option id='" . $i . "'>" . $i . "</option>";      
    ?>
    </select>

<form name="pagenumber" id="pagenumber" action="" method="get">
    <input type="text" name="pagenumber_form" id="pagenumber_form" />
</form>

So, I've tried using $_POST as well, but the same thing happens. I want to use $_GET, for a couple of reasons: it's easier to see what is happening with my values and the data I'm using doesn't need to be secure.

To recap: the $_GET variable is being added to the URL when I change the < select > element, and the corresponding value gets added to the URL as: ".../?pagenumber_form=X", but when I try to use the value in PHP, for example...

$page_number = $_GET['pagenumber_form'];

... I get a NULL value. :-(

Can anybody help me out please? Thank you.

EDIT:

I've just made a discovery. If I move my print_r($_GET) to my main index page, then the superglobals are returning as expected. My site structure is like this:

index.php - JavaScript buttons use AJAX HTTP requests to include the "responseText" as the .innerHTML of my main < div >. The "responseText" is the contents of the page itself, in this case page_manager.php, which in turn includes pagination.php.

So in other words, my site is built from PHP includes, which doesn't seem to be compatible with HTTP superglobals.

Any idea how I can get around this problem? Thank you :-).

+------------------------------------------------------------------+

I can't answer my own posts, so:

The problem is not solved, but has been worked around.

I am certainly not very knowledgeable when it comes to PHP, but I am of the impression that using AJAX requests to include a PHP file in a document, which itself includes other PHP files, is not a good idea. The problem, I believe, was being caused because PHP is executed before the document is loaded in to the browser. Therefore, dynamically including a PHP file in a document will result in the improper working of said file due to the fact that PHP must be executed by the server before the page is rendered, and not after.

As such, I have stopped using AJAX for my site and am simply using good old PHP instead. I don't know enough to carry on using the AJAX requests, so that's an end to that problem.

Thanks to those who replied.

Was it helpful?

Solution

You need to re-pass the superglobals to the AJAX calls. So where you would make a request to pagination.php you need to make it to pagination.php?pagenumber_form=<?php echo $_GET['pagenumber_form']; ?>.

OTHER TIPS

the corresponding value gets added to the URL as: ".../pagenumber_form=X

You might wanna try

.../?pagenumber_form=X

Included files can access superglobals just fine (which is what makes them super). What can't be done is to access variables from one request in another. It isn't that clear what your code is doing (since the question doesn't include a proper minimal test case–a complete, concise, representative sample), but it sounds like loading a single page involves multiple requests, and only the first of these is given the form data. Each AJAX request involves a separate HTTP request, and (because HTTP is supposed to be stateless) has different request data, so any request that isn't explicitly given the data won't have access to it. After a request is handled, all data the script has access to is discarded. This is why if you need data to exist across requests, you need some form of persistence, such as sessions (which you should be careful of, in order not to break the HTTP stateless model) or databases.

Some of the difficulty may lie in a confusion over exactly what happens server-side, what happens client-side, what happens between the two and in what order it all happens. Before you go further, read up on HTTP (a web search should reveal countless documents on the topic). You can use debuggers (e.g. Firebug, XDebug+a client, Wireshark, Live HTTP Headers) to peer at what's happening as it happens.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top