Solved

php, logic

Posted on 2012-03-31
4
261 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

856 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