Page 1 of 1

simple form post

Posted: Wed Aug 03, 2005 12:04 am
by naijaguy
I'm a newbie trying to get the following code to work, and the POST handling works the way I expects (it echoes that one line), but when it's just a GET, it doesn't output the form anymore. How do I write this properly in PHP? My hosting company is using 4.3.10, I think. Thanks...

Code: Select all

<html>
<head>

</head>

<body>


<?
$os = $_GET["os"];
if(!(isset($os))) {
	$os = $_POST["os"];
        $myname = $_POST["myname"];

        if (isset($myname)) {
          echo "Your name is $myname.";
        }
} else {

?>

<form method="post" ><div>
First Name: <input type="text" name="first" value=""> 
<br />
Last Name: <input type="text" name="last" value="">
<br />
Number of guests including yourself: <input type="text" name="total" value="">
<br />
Comments: <input type="text" name="comments" value="">
<input type="hidden" name="os" value="ds">
<input type=submit value="Go!"></div>
</form>

<?
}  # end if os is set
?>

</body>

</html>

Posted: Wed Aug 03, 2005 12:15 am
by josh
If you want to use get you must update it in the form too:
change:
<form method="post" >

to
<form method="get" >

of course I'm going to have to HIGHLY recommend using post, theres not really a reason for get with forms, but if you want to use get be my guest.

Posted: Wed Aug 03, 2005 12:32 am
by naijaguy
I thought it was weird myself...I was going off of this tutorial--it was talking about clearing the $os variable if you wanted to reset the form:

http://www.codehelp.co.uk/php/form.php

Posted: Wed Aug 03, 2005 2:01 am
by s.dot
your form doesn't have an action attribute.
<form action="page" method="get/post">

And the reason it's not showing your form is:

you have said

IF there is NOT $_GET['os']
{
THEN print this line
}
ELSE
{
show form
}

so if you're not receiving the $_GET['os'] data, your form will be shown. Since you're not seeing the form, you're receiving that data and your if statement is satisfied.

Posted: Wed Aug 03, 2005 2:51 am
by josh
I can't believe I overlooked that, I saw the "post" in the form and immediately assumed that was the problem, not paying any attention to the remainder of the code, good eye.