Ask coding questions

← Back to all posts
Global variable.
Baconman321 (81)

I am making a feedback form, and I have it set up so that in submission1.php, it will gather the variable from the textarea from the index.php. It then pushes it into a variable. I then include submission1 in seefeedback.php but when I try to print it out I get NULL. What am I doing wrong?

Answered by blonkm (22) [earned 5 cycles]
View Answer
Commentshotnewtop
blonkm (22)

Every php page you run is a process on it's own which does not have variables.

$feedbacka=var_dump($feedback);

will never work in seefeedback.php. The variable $feedback is not set because the page has no idea it exists.
You need a way to communicate between pages. This can be done through cookies as you tried, but it's better to either use:

  • the url (as in ?feedback=...)
  • a hidden field in a form, and using JavaScript to submit the form.
  • a session variable $_SESSION['feedback'] = ...

also test all your code using a php linter, e.g. https://phpcodechecker.com/

Baconman321 (81)

@blonkm Thanks. Right now, I'm just going to make everything on a single page, ignore the other things. It will push it into an array, and with the cookies, when I reload the page to login, it will still have it. Although I can think of some flaws now. Can you point them out? Also, I don't think session works. Or I need to know how to use it. Also, what do you mean by URL? like what would I store in it? Also, I want to get everyone's feedback and store it in a single array.
After what you said, I realize I don't think I can do it, since every time the page reloads, only that feedback that they submitted is entered, is there a way I can cross-session, like take something from someone's session and add it into my own session so I can see everyone's? Thanks.

blonkm (22)

@Baconman321 What you need for that is a database. That seems too far ahead for you right now. I would stick to one page and get everything working and then start thinking about learning about a database.
Remember that a web page is just a page. Users connecting to it will not magically know about other users. A database is a separate entity that can connect to your web server.

USERS <===> WEB SERVER <===> DATABASE

Users will never see the database. The Web server has a really short memory, and can only work with one user at a time.

Baconman321 (81)

@blonkm Ok, thanks. I'm going to try databases in the future (maybe now, since it seems somewhat simple). Thanks for the help. In the meantime, I guess I will learn more about web development.

Egghead21 (6)

you can put the input section in the main file, but store the input in a different file:

config.personinput = input('')

sorry- I only really do pyhton

Egghead21 (6)

Maybe you need to put the data into a list. Could you print out the feedback after I submit it?

BTW i love phantom forces

Baconman321 (81)

@Egghead21 If I submit the feedback, all it does is just print NULL. That's why I'm pushing it into a variable, because the global variable only holds the information temporarily, or so it seems. I am trying to access it and hold it in a variable.
I need to access a variable from another folder using include, but it's not letting me access the variables.

Egghead21 (6)

@Baconman321 Global variables don't allow you to pass information between folders. Create a new folder, and make the variable there

folder1's name will be config.
So in folder 1(with the variable):
feedback = ''

and then in your input section:
config.feedback = input(': ')

This would set config.feedback- the feedback variable from config- to the input. Then, you can display it by using-
print(config.feedback)

This should work.

Baconman321 (81)

@Egghead21 This is php right? The config.feedback looks a lot like javascript. Also, where would I put the input section? Like in the index.php?