0

This is such a simple question that I'm afraid I'm missing something basic, so feel free to shame me, but I'm stumped.

My goal: make a "continue" button that reloads the same page, but with an incremented ?Page= parameter that's visible to GET.

My current approach: define the new URL that should be loaded (this bit works)

$_SESSION['NextPage'] = $_SESSION['Page']+1; 
$_SESSION['NewURL'] = $_SESSION['CurrentFile'] . '?Page=' . $_SESSION['NextPage'];
// N.B: $_SESSION['CurrentFile'] just specifies the URL of the current page.
// Note also that there should ALWAYS be a ?Page= parameter in the URL, even if it evaluates to NaN or something weird.

Then, later on, a function is called that runs this code and creates a submit button. The button is there, as expected, but it doesn't WORK as expected: code & details below.

echo "<tr><td><form action='" . $_SESSION['NewURL'] . "' method='GET'> <input type='submit' value='Continue'></form> </td></tr>";

The problem: When the form submits, it doesn't append any ?Page= parameters to the URL, either in Chrome or Safari. Is this because the Page parameter isn't specified inside this form?? If so, there must be a way around that, right? $_SESSION['NextPage'] is a session variable, for crying out loud! Not to mention that when I echo $_SESSION['NewURL'] inside the function, the info is accessible there, ?Page=2 and all. It's just not pushed to the URL. Unfortunately, the page is set up to display different content based on the value of $_GET['Page'], and now there's nothing there to control that process.

If I've just made a syntax error or something I'll be embarrassed but relieved. I just can't find it, and don't know where else to turn. Thanks!

Sébastien
  • 11,860
  • 11
  • 58
  • 78
user2411121
  • 41
  • 1
  • 10

1 Answers1

0

If you want to pass a variable via a form (GET or POST) the simple way then just use a hidden form input

<input type="hidden" name="page" value="<?php echo $_SESSION['NextPage']; ?>">

Your form becomes:

echo "<tr><td>
<form action='" . $_SESSION['NewURL'] . "' method='GET'>
  <input type='hidden' name='page' value='" . $_SESSION['NextPage'] . "'>
  <input type='submit' value='Continue'>
</form>
</td></tr>";
Sébastien
  • 11,860
  • 11
  • 58
  • 78
  • Thanks for this clever idea. Unfortunately, it doesn't solve the issue. Now at least I get a "?" at the end of the URL, but still nothing else. This is weird, right? – user2411121 Oct 09 '13 at 21:53
  • Change `type='hidden'` to `type='text'`, that will show you the value. If it is empty then `$_SESSION['NextPage']` is not properly set. Or else try typing a value directly in the input like "abc" your query string should contain `?page=abc` – Sébastien Oct 09 '13 at 21:55
  • My bad! I reloaded the wrong file. Yes, this solution works- thank you! For my own education- would you mind explaining just WHY my original setup didn't work? – user2411121 Oct 09 '13 at 21:59
  • I did a little searching: read the answer to this question http://stackoverflow.com/questions/732371/what-happens-if-the-action-field-in-a-form-has-parameters – Sébastien Oct 09 '13 at 22:07
  • Again, thank you. I searched long and hard as well, but never came across that post. – user2411121 Oct 09 '13 at 22:16