Page 1 of 1

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

Posted: Fri Aug 01, 2014 3:27 am
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.

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

Posted: Fri Aug 01, 2014 3:39 am
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).

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

Posted: Sat Aug 02, 2014 7:17 am
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.

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

Posted: Wed Aug 06, 2014 2:23 am
by wendyj
I have started giving my submit buttons a name attribute now.