Jump to content

isset dilemma


dsp77

Recommended Posts

//If the form is submitted
if(isset($_POST['submit'])) {

//Check to make sure that the nume field is not empty
if(trim($_POST['nume']) == '') {
	$hasErrorNume = true;
} else {
	$nume = trim($_POST['nume']);
}

//Check to make sure that the prenume field is not empty
if(trim($_POST['prenume']) == '') {
	$hasErrorPrenume = true;
} else {
	$prenume = trim($_POST['prenume']);
}

//Check to make sure that the organizatie field is not empty
if(trim($_POST['organizatie']) == '') {
	$hasErrorOrganizatie = true;
} else {
	$organizatie = trim($_POST['organizatie']);
}

//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email_1']) == '')  {
	$hasErrorEmail_1 = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email_1']))) {
	$hasErrorEmail_1_e = true;
} else {
	$email = trim($_POST['email_1']);
}
//If there is no error, send the email
if(!isset($hasErrorNume)) {
	$emailTo = 'youremail@yoursite.com'; //Put your own email address here
	$body = "Name: $name \n\nEmail: $email \n\nSubject: $nume \n\nComments:\n $prenume";
	$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email_1;

	mail($emailTo, $subject, $body, $headers);
	$emailSent = true;
}
}

Link to comment
Share on other sites

Nothing jumps out at me as being wrong.  That said, what I like to do in these cases is use one variable to represent both the error case and the filtered value if validation checks out.  In other words, something like:

 

if (!empty(trim($_POST['name'])))
{
   $name = trim($_POST['name']));
}
else
{
   $name = false;
}

// ...

if ($name) // $name is true AND contains a legit value
{
   // email code
}

 

This saves one from checking to see if an error variable exists or not.  You know you'll always have a value for that input.  You only need to see if it's legit or false/error.

Link to comment
Share on other sites

Going to your example:

 

(!isset($hasErrorNume))

It seems that $hasErrorNume is being assigned with a boolean value. I think you don't need to use isset() here but directly apply ! instead like:

 

(!$hasErrorNume)

I think that would suffice but if you are still wary about it... you can just add it up with &&, you don't have to go so much with complication, IMHO.

 

bluejay,

Link to comment
Share on other sites

when i submit the form it only checks for  $hasErrorNume if its valid and goes on but if the rest $hasErrorPrenume $hasErrorOrganizatie etc are empty it still goes on if i add if(!isset($hasErrorNume,$hasErrorPrenume,$hasErrorOrganizatie)) it still check only for the firstone and i want to check for all errors

Link to comment
Share on other sites

Going to your example:

 

(!isset($hasErrorNume))

It seems that $hasErrorNume is being assigned with a boolean value. I think you don't need to use isset() here but directly apply ! instead like:

 

(!$hasErrorNume)

I think that would suffice but if you are still wary about it... you can just add it up with &&, you don't have to go so much with complication, IMHO.

 

bluejay,

 

He has to use isset because the error variables won't exist if the form values validate.  In other words:

 

if (trim($_POST['nume']) == '')
{
   $hasErrorNume = true; // $hasErrorNume exists (is set) and contains true
}
else
{
   $nume = trim($_POST['nume']); // $hasErrorNume does NOT exist (is not set)
}

 

---

 

if(!isset($hasErrorNume' date='$hasErrorPrenume,$hasErrorOrganizatie)) it still check only for the firstone and i want to check for all errors[/quote']

 

What precisely is happening?  What data are you passing in, and what is the result?  Is it still sending out an e-mail even if the other fields are empty?

Link to comment
Share on other sites

He has to use isset because the error variables won't exist if the form values validate.

 

Not necessarily. You see if you use:

(!$hasErrorNume)

and this variable is not initialized, it will still go in because it will return false. Since what he would do here is run the following statements that the said variable is not true then the same idea would still be valid. Otherwise the he wants to do some strong typing development, then he needs to do else for his if so that it will always exist.

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.