Page 1 of 1

basic question

Posted: Tue Jun 09, 2009 8:39 pm
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

Re: basic question

Posted: Tue Jun 09, 2009 9:38 pm
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>

Re: basic question

Posted: Tue Jun 09, 2009 11:22 pm
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?

Re: basic question

Posted: Wed Jun 10, 2009 10:35 am
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.