0

So i currently have 6 files being used in my project index.html index.php functions.php leap_year.php time.php submit.php.

I collect information in index.html using

<form action ="submit.php" method="post">    
<!-- inside index.html-->
<select name="day">
    <option value=1>1</option>
    <option value=2>2</option>
</select>

which is sent to submit.php by using $_POST

// inside submit.php
global $day;
$day = $_POST["day"] ;
global $type;
$type = $_POST["type"];
header("Location: /index.php");
// I have not included the file path here but it works
// post MUST be used.

there are no issues with the code above and all variables are accessible and usable in submit.php Now we need day and all other variables to be used inside of index.php so i have tried using include

// inside of index.php
include "submit.php";
if($type==="leaper"){
    header("Location:leap_year.php"); 
}

this causes an undefined index error on line 3 (the if statement) playing around and testing with things like is_null confirms theres is no longer data in type or it was not passed on. I need all of my variables available in all of my files. I thought that using include would make this possible but it does not seem to work. I've also tried using session but i have the same result.

I've also tried PHP, getting variable from another php-file and this was of no help. I need the data that the user enters to go across multiple files

4
  • 2
    You're misunderstanding how the web works. The calls to submit.php and index.php are done in separate HTTP requests to the server. Web apps are essentially stateless which means that all information is lost between requests, unless you write code to store it somewhere and then retrieve it again. Basically each time the PHP script executes, it might as well be the first time it ever executed - it has no memory of previous requests. So including "submit.php" in index.php doesn't work because it's not the same instance of submit.php as the one which collected the data. Commented Jun 8, 2020 at 17:09
  • 2
    What you need to do is store the data somewhere in between requests. If it's just temporary, only needing to be held while the current user is in their current usage session, then store it in-memory in the server's Session variables. If the data needs to be held longer term then the most common option for that is using a database, although for very simple / infrequently-used data you could also store it in a text file. Then, when you need the data in another script which is running as part of another request, you can retrieve it from the relevant data store. This is how all web apps work. Commented Jun 8, 2020 at 17:10
  • Can you show me an example of how this is done? or would be done? I've tried session but i must have not done so correctly. Commented Jun 8, 2020 at 17:13
  • 1
    There are already quite a lot of basic examples of using the PHP Session available online if you search. I don't really need to repeat the same tutorial here. e.g. this one will get you started: tutorialrepublic.com/php-tutorial/php-sessions.php . If you have a specific problem once you've followed that and tried to apply it to your own code, then you'd have to show us the code and explain the problem in more detail. Commented Jun 8, 2020 at 17:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.