Jump to content

is isset() necessary?


NickG21

Recommended Posts

hey, i am making a validation page for numerous forms all stored in the same session();
i was wondering if i use
<?php
session_start();
foreach($_POST as $key => $var) {
$_SESSION[$key] = $var;
}
?>
to assign all of my variables (which have different names for every form) is it necessary for me to say
if(isset($_POST['buttonname']))
blah blah blah
or can i just go right into my validation

if(empty($_POST[$key])){
blah blah
Link to comment
Share on other sites

sorry about the non-code tags, would it be possible for me to send each form action to validation.php and at the beginning use:

[code]<?php
session_start();
foreach($_POST as $key => $var) {
$_SESSION[$key] = $var;
} [/code]
in order to set the variables, check if the particular button for a particular form was set
[code]if(isset($_POST['SendMessage'])){[/code]
<--Validation code-->
then if the variables are processed correctly use a  [code]session_write_close();[/code] after the validation for each is complete and the user is sent to the next form?
Link to comment
Share on other sites

it is not necessary... after a submit, $_POST is an array of all the values that were submited, nothing more... but if you want only values that are filled in...

[code]
<?php
session_start(); 
foreach($_POST as $key => $var) if(!empty($_POST[$key])) $_SESSION[$key] = $var;
?>
[/code]
Link to comment
Share on other sites

im not looking for just variables that filled in, that was just a basic example, what i want to do is write different validations for different input variables, and if they all pass i want to load the "next" form in the window in which that one was just passed from:

[code]if(count($error) > 0){

header("Refresh: 3; URL= http://www..../ListInfo.php");
echo "<input type='hidden' value='' name='step'>";  <--- makes the initial page headerImage keep the ListInfo Form open instead of going on
echo "Please Correct Your Errors Before Proceeding";
$error[] = "Please Fix Errors and Re-Submit";

}ELSE{
include_once('headerImage.php'); <-- includes main page and increments the step by 1 to load the next form
}[/code]
i am just unable to pass more than one forms information variables into validation.php, i don't know how to go on with it.  that is why i wanted to just put certain validations for individual variables so that if they aren't set it won't go through that code but if they are it will only use that code.  this sounds confusing to me writing it so if you don't understand i don't blame you ill try to clean it up a little bit
Link to comment
Share on other sites

becareful validating form elements.

Using isset is a bit of a red herring - posting empty fields still SETS that var name in the postheaders.

empty will return true (ie will detect an empty var) if your var is set to 0 - you may want 0....

I tend to use type casting when validating forms - I find it helps.
Link to comment
Share on other sites

In addition to what ToonMariner just said, there are many acceptable methods of validating form elements, but I challenge you to come up with the [i]best[/i] for you. What I mean by that is this: don't settle for it just working. Try it out on a server with error reporting set to the highest level and make sure you don't have any errors or warnings appear. In function, you can check if(isset($_POST['submit'])) or if($_POST['submit']), but only the former one will be entirely sound. If you turn error reporting on full, you'll get the warning that you are referencing an undefined key 'submit' with the second. Will it work? Probably, but it's not the best practice. A proper understanding of all the available functions will lead you to your own solution on what works for you.

Good luck!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.