Have to put isset post at top of .php pages for code to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
wendyj
Forum Newbie
Posts: 12
Joined: Fri Aug 01, 2014 3:14 am
Location: South Africa

Have to put isset post at top of .php pages for code to work

Post by wendyj »

This is my first post ever at a PHP forum. I have been learning PHP for a few weeks now and I have a question.

I am working through my manual (PHP / MySQL for the Absolute Beginner) and everytime I create a form within my PHP code, I need to add a line of code like the one below for every variable that I have on my page otherwise the form doesn't work.

Code: Select all

if(isset($_POST['die'])){ $die = $_POST['die']; }
The form method that I am using is:

Code: Select all

<form method="post">
In the manual, the author does not use 'isset'. The way that I discovered that using 'isset' above would work is that I did an internet search, and well, when I put it in my file, it worked.

The author of the book does not need to put this code in his files to work, and I am not sure if this is standard practice or not.

I am working on my live server which has the latest PHP, etc - they are quite jacked up.

Any help with this would be appreciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Have to put isset post at top of .php pages for code to

Post by requinix »

It depends on you and on the rest of the code around it.

Using isset will "guarantee" that you're doing things right. At least the things that have to do with getting form values. Not using isset means you start putting your trust in the person using your site and that they aren't trying to screw around with your form - as bad as it sounds, generally any potential problems with that will be caught when you validate the form data, leaving you with PHP complaining about undefined offsets.

There should be at least one isset to check that the form was submitted with something resembling good data. Like checking for the presence of the submit button (which, if given a name attribute, will also show up in $_POST).
User avatar
wendyj
Forum Newbie
Posts: 12
Joined: Fri Aug 01, 2014 3:14 am
Location: South Africa

Re: Have to put isset post at top of .php pages for code to

Post by wendyj »

Thank you Requinix, some of that is a bit over my head but I'm sure will sink in as time goes by.
User avatar
wendyj
Forum Newbie
Posts: 12
Joined: Fri Aug 01, 2014 3:14 am
Location: South Africa

Re: Have to put isset post at top of .php pages for code to

Post by wendyj »

I have started giving my submit buttons a name attribute now.
Post Reply