Submit form show echo on form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Submit form show echo on form

Post by reecec »

Hi all


i need a script to echo some data on submit but i need the form to show aswell as the echo

i did a test like this but when i click submit it has a blank page with my echo "hi" but no form please help

thanks reece

CODE:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Contact Form</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){
$me = $_SERVER['PHP_SELF'];
?>
<form name="form1" method="post"
action="<?php echo $me;?>">
<table border="0" cellpadding="2" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td>Name:</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="Subject"></td>
</tr>
<tr>
<td valign="top">Message:</td>
<td><textarea name="MsgBody"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit"
value="Send"></td>
</tr>
</table>
</form>
<?php
} else {
error_reporting(0);
echo "hi";
}
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

it's because you are saying (in your php) "If form has not been sent, show it, else, echo 'hi'"

You need to remove the if

Also... use $_POST instead of $_SERVER['REQUEST_METHOD']

If you would like me to point out some other issues, I will gladly.
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

hi thanks i got the request thing of a tutorial so if you would be happy to show me how its done it would be apriciated
yea the if sorted it out thanks what about the request

thanks

reece
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Notice the new hidden field I added:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
 <html>
  <head>
   <title>Contact Form</title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
<?php
if ($_POST['form_is_sent'] == 'yes'){
	echo "<pre>";
	print_r($_POST);
	echo "</pre>";
}
?>
<form name="form1" method="post"
action="<?php echo $me;?>">
<table border="0" cellpadding="2" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td>Name:</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="Subject"></td>
</tr>
<tr>
<td valign="top">Message:</td>
<td><textarea name="MsgBody"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="hidden" name="form_is_sent" value="yes"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

Hi thanks that works but what if i need to move where it echos which i could before

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
 <html>
  <head>
   <title>Contact Form</title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
<?php
if ($_POST['form_is_sent'] == 'yes'){

?>
<form name="form1" method="post"
action="<?php echo $me;?>">
<table border="0" cellpadding="2" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td>Name:</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="Subject"></td>
</tr>
<tr>
<td valign="top">Message:</td>
<td><textarea name="MsgBody"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="hidden" name="form_is_sent" value="yes"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
</body>
</html>
<?
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
}
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

reecec wrote:Hi thanks that works but what if i need to move where it echos which i could before

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
   <title>Contact Form</title>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
<form name="form1" method="post" action="">
<table border="0" cellpadding="2" cellspacing="0" bgcolor="#FFFFFF">
<tr>
<td>Name:</td>
<td><input type="text" name="Name"></td>
</tr>
<tr>
<td>Subject</td>
<td><input type="text" name="Subject"></td>
</tr>
<tr>
<td valign="top">Message:</td>
<td><textarea name="MsgBody"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="hidden" name="form_is_sent" value="yes"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?
if ($_POST['form_is_sent'] == 'yes'){
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
}
?>
</body>
</html>
move the entire if statement with the echo

(Oh... and you can just post to "" if it's the same page... I'm not sure if this is standard, but it's what I do -- notice the new action of the form)
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Post by reecec »

yea i thought that could be blank (actions

yea and thats fine with the echo


thnaks for all your help


reece
Post Reply