Solved

php, logic

Posted on 2012-03-31
4
248 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 82

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 82

Expert Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now