simple form post

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
naijaguy
Forum Newbie
Posts: 2
Joined: Wed Aug 03, 2005 12:01 am

simple form post

Post 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>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
naijaguy
Forum Newbie
Posts: 2
Joined: Wed Aug 03, 2005 12:01 am

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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.
Post Reply