Passing variables to another page


I have hit the brick wall. I am running this code on 2 pages.

[php]$selectbegindate = $_POST[‘begindate’];
$selectenddate = $_POST[‘enddate’]; // End date
$begindate = new DateTime($selectbegindate);
$enddate = new DateTime($selectenddate);[/php]

Begin Date:
End date:

First page: I can submit a begin date and an end date through an HTML form and my page works as expected, running SQL statements, producing charts, etc. My issue comes when I navigate to another page, which does have the same code and html form so the user can submit another date query if needed, I get the following:

Notice: Undefined index: begindate in C:\xampp\htdocs\dashboard\index2.php on line 10

Notice: Undefined index: enddate in C:\xampp\htdocs\dashboard\index2.php on line 11

Line 10 and 11 being the first 2 lines in the first code above.

I’m sure this is an easy fix but I can not, for the life of me, figure it out.

I have tried understanding isset and isempty but not having any luck applying it to what I need. I also read that I need to store the dates in a session. Tried that several different ways to no avail.

Thanks for any help.


If you need to use the values in multiple pages put the data into SESSION variables.


This is what I have tried since I posted earlier. I’m sure I’ve got something screwed up.

Each page at the top:
$selectbegindate = $_POST[‘begindate’];
$selectenddate = $_POST[‘enddate’]; // End date
$begindate = new DateTime($selectbegindate);
$enddate = new DateTime($selectenddate);[/php]

Form on each page:

<form action = "index.php" method="post"><!-- Begin date select form --> Begin Date: <input type="date" id="begindate" name="begindate"> End date: <input type="date" id="enddate" name="enddate"> <input type="submit" name="submit" value="Submit"> </form

Below the form:
if(isset($submit)) {
$_SESSION[“begindate”] = $_GET[“begindate”];
$_SESSION[“enddate”] = $_GET[“enddate”];
?> [/php]

My error when I go to a link on the second page through a link.

Notice: Undefined variable: begindate in C:\xampp\htdocs\dashboard\sessionstest.php on line 14


Re-reading, if I understand correctly, You actually only need one page that you can keep changing the dates to get a new result. In that case you dont need sessions. Also, remove the form action completely from your code.

Psuedo code
include form.php

//Process form and display results


I apologize. This is on several pages. Each page is for several different categories that I am searching/grouping with charts.

If I do the search on one page, view my results, then decide I want to go to another page in through a link, I would like the begindate and enddate to be passed on the page I am going to.

Complicated and stupid.


If the only difference between these several pages is category and/or other search filters, STOP. You need to make one single page that lets you select via appropriate form controls whatever it is you are currently hard-coding onto the different pages. This will reduce the amount of code you have to create, test, and maintain and make it easier to make corrections and changes since the code will only exist once - Don’t Repeat Yourself - DRY.

Next, you need to use a get method form, since you are determining what data will be gotten and displayed on the page, which is the intended use of URLs. This will cause the begin and end dates and any other search filters to be carried in the URL from page request to page request,. If you do need to then navigate to other pages, you would build the navigation links starting with any existing get parameters, plus whatever navigation parameter(s) you using now when producing the links (see php’s http_build_query() for building the query string part of URLs, starting with the existing $_GET data, modifying it, then producing a link.)