?
Solved

php, logic

Posted on 2012-03-31
4
Medium Priority
?
267 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 84

Accepted Solution

by:
Dave Baldwin earned 2000 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 84

Expert Comment

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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 dynamically set the form action using jQuery.
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…
Suggested Courses

719 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