user2411290
user2411290

Reputation: 641

PHP Post Method returns back my PHP Source Code

I'm trying to do a very simple PHP Post Method on my Wamp Server, but when I click "submit," I am returned with my PHP Source Code, and not what it's supposed to do. For what it's worth, I did a few simple tests on Wamp, and it definitely is reading PHP.

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="" />
<title>308 Assigning Values</title>
<link rel="stylesheet" type="text/css" href="css/basic.css" />

</head>

<body>

<form method="post" action="0308_Assigning_Values.php">

<p>
First Name: 

<input type="text" name="firstname" size="30"/>
</p>

<p>
Last Name: 

<input type="text" name="lastname" size="30"/>
</p>

<p>Your Age: 
</p>
<p><input type = "text" name="age" size="3"/></p>

<p><input type="submit" value="Submit Information" />
</p>


</form>

</body>
</html>

--------------------------------------------------------------

<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

$fullname = "$firstname $lastname";

$factor = 5;

$ageplus = $age + $factor;

$current_year = date('Y');

$birth_year = $current_year - $ageplus;

print "<p>Your name is $fullname ";
print "<p>and you say your age is $age ";
print "<p>but I bet you are really $ageplus ";
print "<p>and were born in $birth_year</p> ";

?>

Upvotes: 0

Views: 1043

Answers (2)

Rwd
Rwd

Reputation: 35190

Try This:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="" />
<title>308 Assigning Values</title>
<link rel="stylesheet" type="text/css" href="css/basic.css" />

</head>

<body>
<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

$fullname = "$firstname $lastname";

$factor = 5;

$ageplus = $age + $factor;

$current_year = date('Y');

$birth_year = $current_year - $ageplus;

print "<p>Your name is $fullname ";
print "<p>and you say your age is $age ";
print "<p>but I bet you are really $ageplus ";
print "<p>and were born in $birth_year</p> ";

?>
<form method="post" action="0308_Assigning_Values.php">

<p>
First Name: 

<input type="text" name="firstname" size="30"/>
</p>

<p>
Last Name: 

<input type="text" name="lastname" size="30"/>
</p>

<p>Your Age: 
</p>
<p><input type = "text" name="age" size="3"/></p>

<p><input type="submit" value="Submit Information" />
</p>


</form>

</body>
</html>

--------------------------------------------------------------

You will be outputting the entire document including the HTML. The only reason it won't print any of the post data when you first load the page is because the post data won't have been sent yet.

Hope this Helps!

Upvotes: 0

dsw88
dsw88

Reputation: 4592

First off, is this homework? The title of your HTML document would make it seem that way. If so, I don't want to give away the solution to you, as that wouldn't help your learning.

Here's a hint though: Remember that PHP just executes right where it's at in the HTML document. Act as if you're writing an HTML document, then in places right where you need a dynamic value calculated (one you're not sure of in advance) you should put a PHP tag that calculates and prints out the HTML that should be there.

Remember that any valid PHP code can go inside this PHP tag, so that's where you'll have your if/else statements to determine whether a value has been POSTed to the server.

Upvotes: 1

Related Questions