Forum Moderators: coopster

Message Too Old, No Replies

problems with form

         

Kysmiley

1:56 pm on Nov 28, 2004 (gmt 0)

10+ Year Member



I have a form that works, then I decided I would like to have it more professional looking so that the same form, along with headers, footers, and CSS coding. After reading and rereading Salsa's suggestion to me on a previous post, I am now able to finally get my form to show up on line. Now though it does not include my headers or footers I know the path is correct it is the same path I have on my other form that works. Also now when I miss information on purpose to test if it will put the error messages in the correct places as wanted, the script opens a new blank page instead of posting to self.
Here is my coding can anyone offer any suggestions along with reasoning. I find if I know why then I can learn better.
The coding

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

Kysmiley

11:05 pm on Nov 28, 2004 (gmt 0)

10+ Year Member



anyone have any suggestions for this. Please

mincklerstraat

9:09 am on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's always a bummer when you have a whole lot of code like this and there's something wrong with it, but you have difficulty saying exactly what. Unfortunately, this stage of debugging is usually up to you, where we can't help you much - it's even somewhat against the grain of our Charter [webmasterworld.com] to post many lines of code and ask, 'help me fix this' - note the excellent link on the Charter on 'how much code should I post?', it provides some practical advice to troubleshooting.

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);

which kicks the function into action.

Salsa

4:23 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



Pat, It looks like you're off to a good start--whether you feel that way or not;) Because I got you into this, here are a few suggestions besides the very good ones of mincklerstraat.

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.

Kysmiley

1:39 am on Nov 30, 2004 (gmt 0)

10+ Year Member



thanks for the input. I guess I was thinking that if i was having the action as php_self i kind of figured then i should not post because it it not going anywhere but i need to get the info back onto the same page. Even though i have read on the post and get properties I still allowed my way of thinking to go astray. On last thing I think I figured out why the page does not look right I have the styles called from within the header.inc instead of calling them into mthe fuction separetly

Dont worry I love learning so i am not about to give up till i get it right
Pat

Salsa

4:05 am on Nov 30, 2004 (gmt 0)

10+ Year Member



Just to correct myself, when I mentioned the ternary operator, I started out a line using $_POST['name']..., then changed it to!$_POST['name']... to avoid a possible "undefined variable" error, and in copying and pasting during the change I ended up using the "?" twice in the ternary. It should have been:

!$_POST['name']? $name = "" : $name = $_POST['name'];