...

View Full Version : optimizing code



rndilger
11-05-2007, 01:15 PM
Hello, I'd like to gather input on how I can optimize some code. My objectives are to:

1) Gather input data from a user form
2) Check to see that all required fields have been entered
3) Take the user to a confirmation page that displays the info they entered into the form
4) Submit the information via email to an administrator
5) Do all of this using a single PHP file

I believe session variables are the best way to pass information between functions in this case. Here is the code I currently have:


<?php

function Register(){

echo "<form method='POST' name='register' action='../register1.php' enctype='multipart/form-data'>
<table align='center' border='0'>
<tr><td colspan='3' align='center'><h1>Registration</h1><br><br></td></tr>
<tr>
<td align='right'>Name(s) of Adults Attending </td>
<td align='left'><input type='text' size='50' maxlength='50' name='name' value='".$_SESSION['name']."'></td>
<td align='left' width='100'>".$_SESSION['name_mt']."</td>
</tr>
<tr><td></td>
<td align='left' colspan='2'>
<input type='hidden' name='action' value='CheckInfo'>
<input type='submit' value='Verify Registration'>
</td></tr>
</table></form>";
}

function CheckInfo(){

$_SESSION['name'] = $_POST['name'];

if(empty($_SESSION['name'])){
$_SESSION['name_mt'] = "< required field";
Register();
}else{
Verify();
}

}

function Verify(){

echo "<br><br><form method='post' name='verify' action='../register1.php' enctype='multipart/form-data'>
<table align='center' border='0' cellpadding='5' cellspacing='0' class='Large bold'>
<tr><td colspan='2' align='center'><h1>Verify Registration</h1><br><br><br></td></tr>
<tr><td align='right'>Name(s):</td><td align='left'>$name</td></tr>
<tr><td colspan='2'>&nbsp;</td></tr>
<tr><td align='center' colspan='2'>
<input type='hidden' name='action' value='Submit'>
<input type='hidden' name='name' value='$name'>
<input type='submit' value='Submit'>
<input type='button' value='Modify Registration' onclick='history.go(-1)'>
</td></tr>
</table></form>";

}

function Submit(){

$to = "Test <test@test.com>";
$from = "test@test.com";

$headers = "MIME-Version: 1.0\r\n" .
"Content-type: text/plain; charset='iso-8859-1'\n" .
"From: $from\r\n" .
"Date: ".date("r")."\r\n";

$subject = "Registration";

$message = "Below is information from a recently submitted Registration.\n\n" .
"Name(s): ".$_SESSION['name']."";

mail($to, $subject, $message, $headers);

echo "<br><br><table align='center'><tr><td align='center'>
<h2>Thank you, your registration was successfully submitted.</h2><br><br></td></tr></table>";

}

switch ($action){

case "Register":
Register();
break;

case "CheckInfo":
CheckInfo();
break;

case "Verify":
Verify();
break;

case "Submit":
Submit();
break;

Default:
Register();
break;
}
?>

Note that I will add many more form entries later; I just wanted to get the code working first. And BTW, do note that I have placed the session_start(); code on the HTML page that will house this code.

Would it be easiest to read the user data into an array and then loop through the array to check for missing data? Basically, I just need to optimize this code so it can be easily implemented in other places later.

Thanks in advance for the assistance.

Ryan

idalatob
11-05-2007, 01:57 PM
Hi, I havent really used this a lot. But it works well when you need to validate tons of user input and need it validated. Just remember to call your input fields 'input_x'.


<?php
$submit=$_POST['submit'];
$required = explode(",","input_name,input_email");
if (isset($submit)){
foreach($required as $check){
if (!$_POST[$check]){
$error = true;
$message = "Please enter your " . substr($check,6);
}
}
if (!$error){
validate();
} else {
register();
}
}
?>

The html


<?php echo($message); ?><br>
<form action ="your_page" method="post">
Name:<input type="text" name="input_name"><br>
Email:<input type="text" name="input_email"><br>
Submit: <input type="submit" name="submit">
</form>

Can someone give any insight as to how well this works?

Hope it helps you find a really efficient way to validate stuff. I hope Ive answered part of your question.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum