Trouble w/ single page form

jlknauff

New member
Aug 25, 2008
237
1
0
I have to create a single page form, and since I haven't done any programming in a while, I seem to be hitting a wall. Can someone tell me what I'm doing wrong here?

I want the form to display, but then after it's submitted, I want just the results to display. The code below just keeps displaying the form no matter what I try.

PHP:
<!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=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<?php

if (isset($field)) {

?>



    <p>Done</p>

</body>
</html>

<?php
die();
}

else

?>

    <form name="form" method="post" action="http://www.domain.com/">
    <input type="text" name="field" />
    <input type="submit" value="Submit" />
    </form>

</body>
</html>
 


Here's a cleaner approach.

PHP:
<html>
<body>
<?php
$html = <<<DEFAULT_HTML
<form name="form" method="post" action="{$_SERVER['SCRIPT_NAME']}">
    <input type="text" name="field" />
    <input type="submit" value="Submit" />
</form>
DEFAULT_HTML;

if(isset($_POST['field']))
{
    $html = trim($_POST['field']);
}

print $html;
?>
</body>
</html>

Of course it's always good practice to validate/sanitize user input.