Forum Moderators: coopster

Message Too Old, No Replies

echoing output of ( IF )statement elsewhere

         

Kysmiley

9:26 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



I have an if statement that I would like to have the results echoed to another part of the form I finally got my form to validate how i want it to, but the initial error message is going to the top of the page on the right, before the header. I want it in a table in the middle of the header. I also need to know how to get the clear button to work, here is my codeing
#################
if(isset($_POST['clearform'])) { for the form to reset itself with to empty fields.}
###################
Pat
<?
if(isset($_POST['submit_form'])) {

# Default error_header
$error_header = "<p>The following errors occured, Pease be sure to include the missing fields before continuing</p><br>";

# error checking
if(!$_POST['name']) {
echo $error1 = '<p>Please Fill in the Name field</p>';
} /*check to make sure name is filled in, if not display error message on form as error1*/
if(!$_POST['email']) {
echo $error2 = '<p>Please fill in the email field</p>';
} /*check to make sure email is filled in, if not display error message on form as error3*/
if(!$_POST['suggestion']) {
echo
$error3 = '<p>Please fill in a suggestion</p>';
} /*check to make sure a suggestion is filled in, if not display error message on form as error3*/

if($error1 ¦¦ $error2 ¦¦ $error3) {
echo $error_header;
}
else {
header("Location: thankyou.php"); //error testing ended
}
}
?>
<?php
include ('./includes/header.inc.php');
?>
<div class="content"><center>
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
/* this is the only place I want the initial error message insgted of on top of the page where the (if) statement is placing it*/
<td colspan="2" align="center"><? echo "<font color='red'>$error_header</font>";?><br><? echo "<font color='red'>$error1</font>";?><? echo "<font color='red'>$error2</font>";?><?echo "<font color='red'>$error3</font>";?></td>
</tr>
</table>
</center>
###############################################
Thanks, pat

StupidScript

9:50 pm on Dec 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you are probably seeing two error_header messages, one at the top and one inside the cell, correct?

To eliminate the error_header that appears on top, make this change:

Current version:

if($error1 ¦¦ $error2 ¦¦ $error3) {

echo $error_header; <-- prints at the top

}

else {

header("Location: thankyou.php");

}

New version:

if(!$error1 &&!$error2 &&!$error3) {

header("Location: thankyou.php");

}

Just go to the thanks page if everything's okay, otherwise let the page print with your error variables displayed inside the cell.

Are you talking about a "clear" button to empty the contents of the form fields? If so, use

<input type="reset" value="Clear">

The "reset" buttons function is to reset the form to its default state.

Kysmiley

10:08 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



thanks stupidscript, i like that name thats how i feel often now that im learning this.
So u are just do the error messages above each place the error is showing and not do a header telling them to thnat there apears to be missing fields below and then shoing the errors in the cells like u say. As for the clear form I have that button on the bottom but it does not clear the form, I was wondering if i needed to do somthing in php telling it to process the clear form instructions
Pat

Kysmiley

10:23 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



this is part of the new code redone.
##########################
# error checking
if(!$_POST['name']) {
echo $error1 = '<p>Please Fill in the Name field</p>';
} /*check to make sure name is filled in, if not display error message on form as error1*/
if(!$_POST['email']) {
echo $error2 = '<p>Please fill in the email field</p>';
} /*check to make sure email is filled in, if not display error message on form as error3*/
if(!$_POST['suggestion']) {
echo
$error3 = '<p>Please fill in a suggestion</p>';
} /*check to make sure a suggestion is filled in, if not display error message on form as error3*/

if(!$error1 &&!$error2 &&!$error3) {
header("Location: thankyou.php"); //error testing ended
}

}\######################################
yet the missing fields minus the error_header still show up on the top right of the page above the pageheader. I guess the reset button does not work if i try to reset the page with the error messages on it. If i do it to the origninal page it clears it.
Thanks again
Pat

StupidScript

10:56 pm on Dec 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Pat,

Just so I have it straight ... here's how I imagine your page lays out before the form is submitted:

(inactive) error message <table> start
(inactive) error message table first row (error_header)
(inactive) error message table second row (errors)
(inactive) error message </table> end
<form> start
form fields
form reset button
form submit button
</form> end

Here's how it looks if the form is submitted with missing info:

error message <table> start
error message table first row (error_header)
error message table second row (errors)
error message </table> end
<form> start
form fields
form reset button
form submit button
</form> end

Trying to picture it ...

Re: errors printing in the wrong place:

It looks like the table IS at the top, so that's where the errors will show up. Aside from that, if the errors aren't being displayed within the table's cell, it may be because of a miffed closing TD tag. Anything not in a <td> ... </td> will print above the table.

Re: reset button:

If the reset button is included within the <form> ... </form> tags, it will clear that particular form every time ... no PHP required.

Is it for sure between those two tags on the "oops" page display?

The only reason I have ever seen a reset button fail to rest a form is when it is not included between the form-start and form-end tags. Even a missing form-end tag can break it on some browsers.

Check the source of your page in the browser (a) before you fill out the form and (b) after you submit it without all of the info (so you can see the error messages). Check the second source output carefully to make certain that all opening and closing tags are being included where they should be, and that your error messages and reset button are between the proper tags.

Perhaps something is happening in header.inc.php?

At this point, it is probably more valuable for you to post snippets of the compiled page code (from the browser source) than to post snippets from the PHP code. For troubleshooting ...

Kysmiley

11:10 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



Ok thanks again stupidscript, here is the header part. I will point out areas that should not be in here
########################
<p>Please fill in the email field</p> <---wrong place

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang=en><head>
<title></title>
<link rel="stylesheet" type="text/css" href="./includes/bluegrass.css">
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

</head><body >

<div class="top">

<div class="left">
<a href="home.php">
Home</a></div>
<div class="right_of_left">
<a href="Contacts.php">
Contacts</a></div>
<div class="left2right">
<a href="comments.php">
Comments</a></div>
<div class="right">
<a href="sitemap.php">
Site-Map
</a></div>
</div>
<div class="content"><center>
<table width="80%" border="0" align="center" cellpadding="2" cellspacing="2">
<tr>
<td colspan="2" align="center"><font color='red'><p>The following errors occured, Pease be sure to include the missing fields before continuing</p><br></font></td>
</tr><--- the above error indicator, right place
</table>
</center>
#########################
Tell me when you want more you already have the main PHP code. The clear form is above the closing form tags

Kysmiley

11:15 pm on Dec 16, 2004 (gmt 0)

10+ Year Member



I GOT IT, I GOT IT I GOT IT, I'm so happy. Thank you so much for getting me mthinking andf on the right road stupidscript, i remved the echo from these three plases,
######################
if(!$_POST['name']) {
$error1 = '<p>Please Fill in the Name field</p>';
###################
with no echo in front of it it does not echo on top of the page only where i am telling it in later places to echo
Pat
Now to place the main function mail() part into it and get that part working

StupidScript

12:43 am on Dec 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good job, Pat! I completely missed the individual echoes in the subroutine.

Onward! :)