Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

php, logic

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
MK15
Asked:
MK15
  • 2
1 Solution
 
designatedinitializerCommented:
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
 
Dave BaldwinFixer of ProblemsCommented:
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
 
MK15Author Commented:
mate, that is pretty sweet, never would have thought about doing it that way.  cheers dude
0
 
Dave BaldwinFixer of ProblemsCommented:
Glad you liked it.!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now