Solved

php, logic

Posted on 2012-03-31
4
264 Views
Last Modified: 2012-03-31
hi guys,
I have 3 form fields, i need away to find out which field isset, i don't want to be using several if statements to find out which of the 3 fields have text inputted in.  Note, that all 3 may have text in them.  After i find out which ones have been set i can add the data to a database after following certain security phases.
             $email =  $_POST['email'];
              $firstname = $_POST['firstname'];
             $surname = $_POST['surname'];

Open in new window


e.g. of what i am trying to avoid
if((isset($email)) && (isset($firstname))&&(isset($surname))){
                     echo 'here';
              }
              if((isset($email)) && (isset($firstname))){
                     echo 'here';
              }
              
              if((isset($email)) && (isset($surname))){
                     echo 'here';
              }
..........................

Open in new window


thanks in advance guys
0
Comment
Question by:MK15
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 7

Expert Comment

by:designatedinitializer
ID: 37791907
You can iterate the $_POST array BEFORE assigning it to local variables, like this:

foreach($_POST as $field){
      if($field!=''){
             // do your stuff
      }else{
             // do other stuff
      }
}

Open in new window

0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37791916
Here is a simple test page showing how to use a 'switch()' statement to identify what inputs you have.  It identifies all 8 possibilities that you have with 3 variables.

<?php
error_reporting(E_ALL);
ini_set('display_errors','On');

$casetest = 0;
// preset variables
$email = '';
$firstname = '';
$surname = '';
// some settings of POST vars
if (isset($_POST['email'])) {
	$email = $_POST['email'];
	if($email != "")$casetest = $casetest + 1;
	}
if (isset($_POST['firstname'])) {
	$firstname = $_POST['firstname'];
	if($firstname != "") $casetest = $casetest + 2;
	}
if (isset($_POST['surname'])) {
	$surname = $_POST['surname'];
	if($surname != "")$casetest = $casetest + 4;
	}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>PHP Logic</title>
</head>

<body bgcolor="#ddeedd">
<div align="center">
<h1>PHP Logic</h1>
<table border="0" cellpadding="0" cellspacing="0" summary="" width="580">
<tr><td align="center">

<?php
switch($casetest) {
	case 0:
		echo "Case = ".$casetest.", no entries.";
		break;
	case 1:
		echo "Case = ".$casetest.", only email set.";
		break;
	case 2:
		echo "Case = ".$casetest.", only firstname set.";
		break;
	case 3:
		echo "Case = ".$casetest.", email and firstname set.";
		break;
	case 4:
		echo "Case = ".$casetest.", only surname set.";
		break;
	case 5:
		echo "Case = ".$casetest.", email and surname set.";
		break;
	case 6:
		echo "Case = ".$casetest.", firstname and surname set.";
		break;
	case 7:
		echo "Case = ".$casetest.", all entries set.";
		break;
	}
?>

<p>Input variables</p>

<form method="POST" action="PHPLogic.php">
    <p>Email: <input type="text" name="email" value="<?php echo $email; ?>"></p>
    <p>Firstname: <input type="text" name="firstname" value="<?php echo $firstname; ?>"></p>
    <p>Surname: <input type="text" name="surname" value="<?php echo $surname; ?>"></p>
    <p><input type="submit" value="submit" name="submit"></p>
  </form>
</td></tr>
</table> 
</div>


</body>
</html>

Open in new window

0
 

Author Comment

by:MK15
ID: 37791932
mate, that is pretty sweet, never would have thought about doing it that way.  cheers dude
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 37791942
Glad you liked it.!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question