Forum Moderators: coopster

Message Too Old, No Replies

Form validation part Deux

         

Kysmiley

8:29 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



I finally was able to get rid of the errors that PHP was producing yet I can not get the error messages to post to the main page. I have a few questions so will list them.
I will keep the posting of the script as small as I can at the end to show what I have so far
1)I am not able to get the $PHP_SELP to actually post the errors to itself.,
2) Now the instead of transfering the information that has already been posted back onto the form it opens a new window with nothing on the form.
3)I am not able to get the header or CSS code to come thru either. I will post a seperate question on CSS links with includes in PHP.
I have a few areas in the script I will outline that give me trouble so i removed it just to get the form to load
The script.
############################

<?
if(isset($_POST['submit_form'])) {
$error_msg = null;
# Default error_header
$error_header = "The following errors occured, Please rectify them before continuing<br>";

# error checking
if(!$_POST['name']) {
$error_msg .= "Please Fill in the Name field<br>";
}
if(!$_POST['email']) {
$error_msg .= "Please fill in the email field<br>";
}
if(!$_POST['suggestion']) {
$error_msg .= "Please fill in a suggestion<br>";
}
# end error checking.

if(!$error_msg) {
# if everything ok,
# define variables from the posted data
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
$suggestion = $_POST ['suggestion'];
$messagetype = $_POST['messagetype'];

header("Location: thankyou.php");
}
?>
<html>
<head>

<title></title>
<link rel="stylesheet" type="text/css" href="bluegrass.css">
</head>
<body>
<?php
include ('header1.inc');
?>
<div class="content">
?>

<div class="content">
<? if(isset($error_msg)) {?>
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td colspan="2" align="center"><? echo $error_header?><br></td>
</tr>
</table>
<?
}
}
?>
<center>
<p>Tell us what you think about our web site, our products, our organization, or
anything else that comes to mind.<br> We welcome all of your comments and
suggestions.</p>
<FORM TARGET="<?php echo $PHP_SELF;?>" METHOD=POST>
<table>
<tr><td>Name:<? echo $error_msg;?><br>

<input type="text" size="35" maxlength="256" <? echo $name?> name="name" ></td>
</tr>
</table>

<input type="Submit" value="Submit_form">
<input type="reset" value="Clear Form">
</form></center><br>

</div>
<?
require ('footer1.inc')
?>
</body>
</html>
#########################
I do not have the function mail() put in the script yet it was giving me problems so i decided to try to fix this one item or a few at a time. at least I wanted to get the page to load with out parse errors
Please help me out to understand what I am doing wrong and where. The more I read the more confused I become I hope someday the clouds start to clear
Pat

baze22

9:09 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



One thing I noticed - In your <form> line it should be 'action=' instead of 'TARGET='.

baze

baze22

9:13 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



Another thing, I would think that you would want to put the values in your variables if there WAS an error, so that they wouldn't have to retype. The way you have it is you get the values from _POST if there isn't an error and you are redirecting them to the thank you page anyway (not using those variables).

baze

Kysmiley

9:52 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



Ok the action= took care of teh other window opening I have changed the.
######################
<td><textarea name="suggestion" value="suggestion" <? echo $_POST['suggestion']?>rows="10" cols="42"></textarea></td>
but that still does not pre-fill the areas that were filled b4 and i still have no CSS or Header
Pat

baze22

10:04 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



Do you see where you have the

<?
}
}
?>

You've included the top part your page in that 'if (submit)' block . You'll need to get it out of that block because it would only show up if submit were clicked.

baze

Kysmiley

10:59 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



You mean like this Blaze
###################
<?
if(isset($_POST['submit_form'])) {
$error_msg = null;
# Default error_header
$error_header = "The following errors occured, Please rectify them before continuing<br>";
}
####################
got that part fixed but the error messages are repeating three times when the page is firt loaded and if i fill in one part and leave the others unfilled to test it still shows the erro msg repeated two times in each place example
Ok I filled in the name field and this is what I got back

####################
Name:

Please fill in the email field

Please fill in a suggestion

E-Mail:

Please fill in the email field

Please fill in a suggestion
##########################
Its still not showing the $error_header though
Any other suggestions
Pat

baze22

11:22 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



There were quite a few areas that needed fixing, I'd just glanced at it prior. I finally had time to sit and take a look:

<?
//Things fixed:
//Didn't have a name for the submit button
//changed form action value to $_SERVER["PHP_SELF"]
//needed to have 'value=' in "name" input tag

if(isset($_POST['submit_btn'])) {
$error_msg = null;
# Default error_header
$error_header = "The following errors occured, Please rectify them before continuing<br>";
# error checking
if(!isset($_POST['name'])) {
$error_msg .= "Please Fill in the Name field<br>";
}
if(!$_POST['email']) {
$error_msg .= "Please fill in the email field<br>";
}
if(!$_POST['suggestion']) {
$error_msg .= "Please fill in a suggestion<br>";
}
# end error checking.

if(!$error_msg) {
# if everything ok,
# define variables from the posted data
header("Location: thankyou.php");
}
}
?>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="bluegrass.css">
</head>
<body>
<?php
include ('header1.inc');
?>
<div class="content">
<? if(isset($error_msg)) :?>
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td colspan="2" align="center"><? echo $error_header?><br><? echo $error_msg;?></td>
</tr>
</table>
<? endif;?>
<center>
<p>Tell us what you think about our web site, our products, our organization, or
anything else that comes to mind.<br> We welcome all of your comments and
suggestions.</p>
<FORM action="<?php echo $_SERVER["PHP_SELF"];?>" METHOD=POST>
<table>
<tr><td>Name:<br>
<input type="text" size="35" maxlength="256" value="<? echo $_POST['name'];?>" name="name" ></td>
</tr>
</table>
<input name="submit_btn" type="submit" value="Submit_form">
<input type="reset" value="Clear Form">
</form></center><br>
</div>
<?
include ('footer1.inc')
?>
</body>
</html>

baze

Kysmiley

11:55 pm on Dec 5, 2004 (gmt 0)

10+ Year Member



Thank-you very much Baze22 I did not notice the missing parts. I also have the header and footer working now. Only one more problem
if i leave an email out it places the error message about the name, telephone, and suggestion box as
##############
Please fill in the email field
#################
and not just the email place where the others may be correct
Any ideas why or what I'm missing in the coding. The rest of it works good to this point till i add the mail() function LOL
Pat

baze22

12:02 am on Dec 6, 2004 (gmt 0)

10+ Year Member



with the code you posted I really can't say. You don't have input fields for anything other than "name". Just check your your conditionals and make sure that you are checking the right vaiable names.

baze

Kysmiley

2:13 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Hi again, sorry been away from the internet for a few days not easy finding truck stops with WIFI. Here is the coding for the form. I still cant see what is causing the form error messages to display all errors about each inputg field. So if i miss email and name it will post that i have missed the email and name above the email, name, and suggestion fields'
Pat
Hope this helps combined with the PHP coding from on top.
###########################

<div class="content"><center>
<? if(isset($error_msg)) :?>
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td colspan="2" align="center"><? echo $error_header?><br><? echo $error_msg;?></td>
</tr>
</table>
<? endif;?>

</center>
<center>
<p>Tell us what you think about our web site, our products, our organization, or
anything else that comes to mind.<br> We welcome all of your comments and
suggestions.</p>
<FORM action="<?php echo $_SERVER["PHP_SELF"];?>" METHOD=POST>

<p>What kind of comment would you like to send?</p>
<tr>
<td><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</td>
</tr>
<p>Please help us direct your feedback by selecting a catagory you would like to comment on?</p>
<tr>
<td><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></td>
</tr>
<p><? echo $error_msg;?></p>
<p>Enter your comments in the space provided below:</p>
<tr>
<td><textarea name="suggestion" value="<? echo $_POST['suggestion'];?>"rows="10" cols="42"></textarea></td>
</tr>
<p>Tell us how to get in touch with you:</p>

<table>
<tr><td>Name:<? echo $error_msg;?><br>

<input type="text" size="35" maxlength="256" value="<? echo $_POST['name'];?>" name="name" ></td>
</tr>
<tr>
<td>E-Mail:<? echo $error_msg;?><br>

<input type="text" size="35" maxlength="256" value="<? echo $_POST['email'];?>" name="email"></td>
</tr>
<tr><td>Telephone:<? echo $error_msg;?><br>

<input type="text" size="35" maxlength="256" value="<? echo $_POST['telephone'];?>" name="telephone"></td>
</tr>

<tr>

<td>Please contact me as soon as possible regarding this matter:
<td><input type="checkbox" name="contactrequested" value="1">

</tr>
</table>
<br>
<input name="submit_form" type="Submit" value="Submit_form">
<input type="reset" value="Clear Form">
</form></center><br>

</div>