Forum Moderators: coopster
Pat
<?php // displaying form function
function display_form()
{
require ('./includes/header.inc'); // Include HTML header
global $PHP_SELF;
if (!isset($name)) $name = ""; if (!isset($error_msg)) $error_msg = "";
if (!isset($email)) $email = ""; if (!isset($error_msg)) $error_msg = "";
if (!isset($telephone))
if (!isset($suggestion)) $suggestion = ""; if (!isset($error_msg)) $error_msg = "";
if (($department) == 'none') $error_msg = "<p>Please select a department you would like to comment on<p/>";
if (($contactrequested) == 1) $contactrequested = "Please contact me";
?>
<div class="content">
<p>Tell us what you think about our web site, our products, our organization, or
anything else that comes to mind. We welcome all of your comments and
suggestions.</p><center>
<FORM TARGET="<?php echo $PHP_SELF;?>" METHOD=GET>
<p>What kind of comment would you like to send?</p>
<dl>
<dd><input type="radio" name="messagetype" value="Complaint">Complaint <input type="radio" name="messagetype" value="Problem">Problem
<input type="radio" checked name="messagetype" value="Suggestion">Suggestion
<input type="radio" name="messagetype" value="Praise">Praise</dd>
</dl>
<p>Please help us direct your feedback by selecting a catagory you would like to comment on?</p>
<dl>
<dd><select name="department" size="1">
<option value="none">Select a Department</option>
<option value="Web Site">Web Site</option>
<option value="Company">Company</option>
<option value="Products">Products</option>
<option value="Artists">Artists</option>
<option value="Employee">Employee</option>
<option value="Customer_service">Customer Service</option>
</select></dd>
</dl><p><? echo $error_msg;?></p>
<p>Enter your comments in the space provided below:</p>
<dl>
<dd><textarea name="suggestion" value="suggestion" <? echo $suggestion?>rows="5" cols="42"></textarea></dd>
</dl>
<p><strong>Tell us how to get in touch with you:</strong></p>
<dl>
<dd>
<table>
<tr><p><? echo $error_msg;?></p>
<td>Name:
<td><input type="text" size="35" maxlength="256" <? echo $name?> name="name" >
</tr>
<tr><p><? echo $error_msg;?></p>
<td>E-mail:
<td><input type="text" size="35" maxlength="256" <? echo $email?>name="email">
</tr>
<tr><p><? echo $error_msg;?></p>
<td>Telephone:
<td><input type="text" size="35" maxlength="256" <? echo $telephone?>name="telephone">
</tr>
</table>
</dd>
</dl>
<dl>
<dd><input type="checkbox" name="contactrequested" value="1">
Please contact me as soon as possible regarding this matter.</dd>
</dl>
<p><INPUT TYPE=HIDDEN NAME="stage" VALUE="results"><input type="submit" value="Submit Comments"> <input type="reset" value="Clear Form"></p>
</form></center><br>
</div>
</div>
<?php
require ('./includes/footer.inc');
}
?>
<?php
// Validate form
function form_validation()
{
$errors = 0; if(!isset($name)) { $error_msg = "<span style=\"color:red;\">I'm sorry you forgot to include your name</span><br>\n";
$errors++; }
$errors = 0; if(!isset($email)) { $error_msg = "<span style=\"color:red;\">I'm sorry $name you forgot to include your emailoaddress</span><br>\n";
$errors++; }
$errors = 0; if(!isset($suggestion)) { $error_msg = "<span style=\"color:red;\">I'm sorry $name you forgot to include your suggestion</span><br>\n";
$errors++; }
if (!isset($suggestion) ¦¦ empty($suggestion)) $error_msg = "<span style=\"color:red;\">I'm sorry $name you forgot to include your comments address</span><br>\n";
//next one is for a drop down list, may be wrong
if (!isset($department) == 'none') $error_msg = "<p>Please select a department you would like to comment on<p/>";
//this is for a radio button
if (!isset($contactrequested) == 1) $contactrequested = "Please contact me";
//process form if no missed field errors
if ($errors > 0) display_form();
else email_results();
}
?>
<?php
function email_results()
{
$toaddress = 'pat@mysite.com';
$subject = $_get["messagetype"];
$mailcontent = 'Department: '.$department."\n"
.'Customer name: '.$name."\n"
.'Customer email: '.$email."\n"
.'Customer Telephone number: '.$telephone."\n"
.'Contact requested: '.$contactrequested."\n"
."Customer comments: \n".$suggestion."\n";
$fromaddress = 'From: feedback@mysite.com';
mail($toaddress, $subject, $mailcontent, $fromaddress);
}
?>
<?php
if (empty($stage)) { display_form(); }
else { email_results(); }
?> //forgot that with my copy and paste
For one, I'd have a look at the HTML source your script is producing for the error thingies - it looks like you have these output inside <tr>'s without <td>'s, which is sort of a no-no. I'm also not sure if you are indeed calling these functions when you need to - glancing around quickly, I see functions defined, I see a function which calls another function - but unless this function is called, it won't call that other function - functions are designed so they do nothing except when you call them. With very few exceptions, it doesn't matter where they are in the code - top, bottom, or middle - they don't do anything by just being there, you need a line that says:
myfunction($myvar1, $myvar2);
Look into using switch statements [us2.php.net] for navigating your functions. I find these much easier to deal with than if/elseif/else statements--especially when navigating a bunch of funcitons.
In your form, put a hidden field with attributes like name="action" value="validate_form".
Then in your switch block, you'll have something like:
switch ($action) {
case "validate_form":
validate_form();
break;
case ...
} Change your form method from GET to POST. This will keep the form data out of the URL.
Understand that variables within functions have local scope by default. To get your POST variables into your downstream functions, you'll need to do something like.
$name = $_POST['name'];
To keep your code clean and to make sure there are no unset variables, however, consider the ternary operator to do it like this:
!$_POST['name']? $name = ""? $name = $_POST['name'];
In your form_validation function, set $errors = 0 only once (before any validation condition). If you keep resetting it to zero before each validation condition, you'll find yourself sending off emails even when earlier conditions do not validate.
Do this stuff, and you're still likely to get errors, but that's just part of the process, and you'll be farther along whether you feel like it or not!
I wish you well.
Dont worry I love learning so i am not about to give up till i get it right
Pat
!$_POST['name']? $name = "" : $name = $_POST['name'];