FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Sending Varible back to page





Possum
Hi

I have a feedback form the user fills in and a php file that processes that form data and E-Mails it to me..

When the user click Submit and the form both sends and refreshes the page. I would like to have the Form say something to let the user know they were successful. For Example

Thanks we have your Message.

The form data and what I am doing is here..

********************Form****************************
<form action="mail_form.php" method="post">
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr style="font-family: Arial;">
<td style="width: 1034px;"><font size="2">Please fill in only the fields you want to. Your Feed Back is appreciated and Important. Form Empties when Data is sent<br><br>
Your Name:&nbsp;</font><br>
<input size="50" name="your_name"></td>
</tr>
<tr style="font-family: Arial;">
<td style="width: 1034px;"><font size="2">E-mail: <br>
</font>
<input size="50" name="email"></td>
</tr>
<tr style="font-family: Arial;">
<td style="width: 1034px;"><font size="2">www</font><br>
<input size="50" name="www"></td>
<input name="title" value="Web Developers > Web Languages > php > Mail to Form > " type="hidden">
<input type="hidden" name="returnto" value="<?php echo $_SERVER['PHP_SELF']; ?>">
</tr>
<tr>
<td style="width: 1034px;"><font face="Arial" size="2">Comments</font><br>
<textarea cols="60" rows="10" name="comments"></textarea><br>
<input value="Submit" type="submit"><input type="reset" value="Reset" name="Reset"></td>
</tr>
</tbody>
</table>
&nbsp;</form>

**********************************************************************

************** Form handling Script ******mail_form.php********************
<?
function checkOK($field)
{
if (eregi("\r",$field) || eregi("\n",$field)){
die("Invalid Input!");
}
}
$title=$_POST['title'];
checkOK($title);
$www=$_POST['www'];
checkOK($www);
$name=$_POST['name'];
checkOK($name);
$email=$_POST['email'];
checkOK($email);
$comments=$_POST['comments'];
/*checkOK($comments); */
$to="me@woosh.co.nz";
$message="Title: $title \n\n Name: $name \n\nE-mail Address: $email\n\n www: $www\n\nComment:\n$comments";
mail($to,"Comments From Your Site",$message,"From: $email\n");
header("Location: " . $_POST['returnto']);
exit;
?>

****************************************************

A better description
http://udopage.com/udopage_53.php

Cheers Possum..
sonam
You can do this with sessions. Start the session on very top on both page (form.php and mail_form.php) with session_start(); function.

Code:

<?php
session_start();
echo @$_SESSION['message'];
unset $_SESSION['message'];
?>

<form action="mail_form.php" method="post">
<table style="text-align: left; width: 100%;" border="0" cellpadding="2" cellspacing="2">
<tbody>
<tr style="font-family: Arial;">
<td style="width: 1034px;"><font size="2">Please fill in only the fields you want to. Your Feed Back is appreciated and Important. Form Empties when Data is sent<br><br>
Your Name:&nbsp;</font><br>
<input size="50" name="your_name"></td>
</tr>
<tr style="font-family: Arial;">
<td style="width: 1034px;"><font size="2">E-mail: <br>
</font>
<input size="50" name="email"></td>
</tr>
<tr style="font-family: Arial;">
<td style="width: 1034px;"><font size="2">www</font><br>
<input size="50" name="www"></td>
<input name="title" value="Web Developers > Web Languages > php > Mail to Form > " type="hidden">
<input type="hidden" name="returnto" value="<?php echo $_SERVER['PHP_SELF']; ?>">
</tr>
<tr>
<td style="width: 1034px;"><font face="Arial" size="2">Comments</font><br>
<textarea cols="60" rows="10" name="comments"></textarea><br>
<input value="Submit" type="submit"><input type="reset" value="Reset" name="Reset"></td>
</tr>
</tbody>
</table>
&nbsp;</form>



In mail_form.php define your message after the mail is sent on your address but before header location.

Code:
<?
session_start();

function checkOK($field)
{
if (eregi("\r",$field) || eregi("\n",$field)){
die("Invalid Input!");
}
}
$title=$_POST['title'];
checkOK($title);
$www=$_POST['www'];
checkOK($www);
$name=$_POST['name'];
checkOK($name);
$email=$_POST['email'];
checkOK($email);
$comments=$_POST['comments'];
/*checkOK($comments); */
$to="me@woosh.co.nz";
$message="Title: $title \n\n Name: $name \n\nE-mail Address: $email\n\n www: $www\n\nComment:\n$comments";
mail($to,"Comments From Your Site",$message,"From: $email\n");

$_SESSION['message'] = "Thanks we have your Message";

header("Location: " . $_POST['returnto']);
exit;
?>


I am not check your script because I haven't time , but I am using this method in many situations.

Sonam
AOP Web Development
As you i see your code try to put the function on the top of the form and then perform the action of sending once the user successfully send just simply echo a thankyou message so you will see the thank you message at the top of the form once the user submit the message going to the feedback form
Possum
Hi

your code woks but I need to delete Line 4 as I get this error..

Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /home/possum/domains/udopage.com/public_html/test/index.php on line 4

sonam wrote:

unset $_SESSION['message'];
rohan2kool
use unset($_SESSION['message']); instead of unset $_SESSION['message'];

unset despite being a language construct, has a function-type syntax.

What i don't understand is the usage of sessions in this case. You could very well add a line of code just before the exit; line (also.. the exit is more or less useless in that position) that prints out your message.
sonam
I am using unset $_SESSION['message']; for one reason when I print out different massages with same session e.g. "Your mail is sent" or "Your mail is not sent because you didn't fill all fields". But you don't need it because you have only one message.

rohan2kool is right I am fogot brackets, sorry.

Code:
unset($_SESSION['message']);


Sonam
Possum
Thx

My forms needed this.. Works Perfectly

Thx..
Possum
The above code worked. But when I put it into a HTML page I started getting this message

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/possum/domains/udopage.com/public_html/index.php:14) in /home/possum/domains/udopage.com/public_html/index.php on line 15

I was so close....

Any more help would be appreciated..

Cheers Possum..
mathiaus
You need to start the session before anything is output on the page. Put it right at the top of the page.
sonam
Like mathiaus wrote you must start session on the top of the page before any output in browser. Be careful, just one space before first <?php can produce warnings.

Code:
<?php
session_start();
?>
<html>
<head>
...
</head>
<body>
<form action="some.php">
<?php
echo @$_SESSION['message'];
unset ($_SESSION['message']);
?>

...
</form>
</body>
</html>


Sonam
Possum
Thanks everyone I made this php tutorial with your help..

http://www.udopage.com/php/udopage_4.php

Cheer Possum...
sonam
Hi Possum,
nice site. I am only sugest to you put your code in the left side. Now (in center) is looking unprofesional and it is wery hard to read.

Sonam
Related topics
xbox 360
few Laptop questions...
Backpage hits
Bush's Border Buffoonery
Magic: The Gathering
Me
What do u see first in the newspaper?
What attracts you to a book?
GlobalTek Inc???
When you open the newspaper comics....
Sending arbitrary data to/from PHP
How can I import text into a .html file from a .txt file?
"Run (N-word) Run" email from GOP candidate for NY
Discrimination against Christian - a real case :-)
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.