Page 1 of 1

Submit form show echo on form

Posted: Wed Jul 19, 2006 11:12 am
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";
}
?>

Posted: Wed Jul 19, 2006 11:16 am
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.

Posted: Wed Jul 19, 2006 11:33 am
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

Posted: Wed Jul 19, 2006 11:37 am
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>

Posted: Wed Jul 19, 2006 12:02 pm
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>";
}
?>

Posted: Wed Jul 19, 2006 12:38 pm
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)

Posted: Wed Jul 19, 2006 1:06 pm
by reecec
yea i thought that could be blank (actions

yea and thats fine with the echo


thnaks for all your help


reece