basic question

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
chappie
Forum Newbie
Posts: 1
Joined: Tue Jun 09, 2009 12:45 pm

basic question

Post by chappie »

trying to make my first web survey for school.

not sure why .php file does not read variables from client side .htm

using following code snippet to test.

Code: Select all

 
//html client side
<html>
<body>
 
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
 
</body>
</html> 
 

Code: Select all

 
//php server side
<html>
<body>
 
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
 
</body>
</html> 
 
-php file is read and executed but fname and age are blank.
-used GET to test if variables are being passed, they are
Last edited by Benjamin on Tue Jun 09, 2009 11:21 pm, edited 1 time in total.
Reason: Added [code=php] tags.
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: basic question

Post by shanecody »

Your form action should be

Code: Select all

<?php echo $_SERVER['PHP_SELF']; ?>.
so your code would look like:

Code: Select all

 
<html>
<body>
 
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
 
</body>
</html>
 

Code: Select all

 
//php server side
<html>
<body>
 
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
 
</body>
</html>
Last edited by Benjamin on Tue Jun 09, 2009 11:21 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: basic question

Post by requinix »

shanecody wrote:Your form action should be

Code: Select all

<?php echo $_SERVER['PHP_SELF']; ?>
.
Almost. It needs a call to htmlentities (or htmlspecialchars) first.

Code: Select all

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
And maybe I missed it, but I thought the HTML file and the PHP file were separate?
shanecody
Forum Newbie
Posts: 23
Joined: Fri May 08, 2009 4:12 pm

Re: basic question

Post by shanecody »

I guess I didn't notice they were separate when I replied last night. Since that's how they are then the code is correct, and it must be a server issue.
Post Reply