Scripting Error

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
skwilliams
Forum Newbie
Posts: 5
Joined: Tue Aug 19, 2003 3:35 pm

Scripting Error

Post by skwilliams »

I am very new to PHP and am studying the PHP manual. I'm stumped on an example that I tried.

I created a php page called form.php. Here's the HTML

<html>
<head>
<title>Form</title>
</head>
<body>
<form action="action.php" method="POST">
Your name: <input type="text" name="name" />
Your age: <input type="text" name="age" />
<input type="Submit">
</form>
</body>
</html>

I also created a page called action.php and it's script.

<html>
<head>
<title>Action</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
Hi <?php echo $_POST["name"]; ?>
You are <?php echo $_POST["age"]; ?> years old.
</body>

I can fill out the form fine and submit it. But the resulting action.php page displays:

Hi You are years old.

It seems to be missing the data that was just entered.

Is there a problem with my script?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

your code is fine, you prolly got a older version of php, try this in action.php page

Code: Select all

<html> 
<head> 
<title>Action</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 

<body> 
Hi <?php $HTTP_POST_VARS&#1111;'name']; ?> 
You are <?php $HTTP_POST_VARS&#1111;'age']; ?>  years old. 
</body>
Bongulim
Forum Newbie
Posts: 21
Joined: Thu Aug 07, 2003 12:50 pm

Re: Scripting Error

Post by Bongulim »

skwilliams wrote: Your name: <input type="text" name="name" />
Your age: <input type="text" name="age" />
I'm not sure this is going to solve your problem, but I'd suggest removing the / from the end of the tags:

Code: Select all

Your name: <input type="text" name="name">
	 Your age: <input type="text" name="age">
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Nope, he's just following (more) proper XHTML standards.

Specifically, the fact that all html tags *should* have end tags. In the case of <br>, <input>, <img>, et cetera, one simply puts /> to end it, for example

Code: Select all

<br />
<img src=source.gif />
<input type=crash />
It's not necessary, but if you're anal retentive about your html, it's a good standard to follow.
skwilliams
Forum Newbie
Posts: 5
Joined: Tue Aug 19, 2003 3:35 pm

Post by skwilliams »

I tried changing the code. It still gives me the same problem.

I'm running Apache on a Red Hat 6.2 platform with PHP4.
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

Your problem is with your echo's:

Code: Select all

&lt;body&gt; 
Hi &lt;?php echo $_POST&#1111;"name"]; ?&gt; 
You are &lt;?php echo $_POST&#1111;"age"]; ?&gt; years old. 
&lt;/body&gt;
It should be something like this:

Code: Select all

<body>
<?php
$name = $_POST['name'];
$age = $_POST['age'];
echo "Hi $name.";
echo "You are $age years old";
?>
</body>
Your problem was that you didnt have the "" around the echo. But you need to set $name and $age since you cant use $_POST['xxx'] because of the ''. It should work now
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

Echos generally work fine printing things like

Code: Select all

echo $_POST["one"];
for me. I haven't noticed any installations (Linux, PC, Mac) that have really complained about it.

Skwilliams, if you put in a line saying

Code: Select all

print_r($_POST);
does the array have anything inside of it at all? I use that a lot to just check form posts.
skwilliams
Forum Newbie
Posts: 5
Joined: Tue Aug 19, 2003 3:35 pm

Post by skwilliams »

Thanks for your response.

I copied the script from your reply, but am still getting the same problem.
User avatar
greenhorn666
Forum Commoner
Posts: 87
Joined: Thu Aug 14, 2003 7:14 am
Location: Brussels, Belgium

Post by greenhorn666 »

Could you like tell me what this gives out as your action.php script

Code: Select all

<pre>
<?
if(isset($_POST)) {
    echo "POSTED VALUES:\n";
    print_r($_POST);
}
if(isset($_GET)) {
    echo "GET VALUES:\n";
    print_r($_GET);
}
?>
</pre>
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

So it just prints out "Array ( )" then?

Hmmm... Try changing the method to GET in form.php and variable calls to $_GET["name"] and $_GET"age"] in action.php... This way you should see the info submitted in the URL and we'll know that the form sent it, at least.

We'll get you there, one step at a time! :)
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

There are 2 things you should try. First, on the action.php page, hit ctrl + F5 to make sure the page is reloaded completely. If that doesnt work, you should try this. Although it *shouldnt* make a differnence:

Code: Select all

<?php
echo "<form method="GET" action="action.php"/>\n";
echo "Name: <input type="text" name="name"/><br/>";
echo "Age: <input type="text" name="age"/><br/>";
echo "<input type="submit"></form>";
?>
Then have your action.php page have this:

Code: Select all

<?php
$name = $_GET['name'];
$age = $_GET['age'];
echo "Hi, $name.  You are $age years old!";
?>
like_duh44
Forum Commoner
Posts: 63
Joined: Sat Jul 26, 2003 6:57 pm

Post by like_duh44 »

LOL trolll, on the same wavelength huh?
skwilliams
Forum Newbie
Posts: 5
Joined: Tue Aug 19, 2003 3:35 pm

Post by skwilliams »

Here is what displays on the action.php page once I enter a name and age and click Submit on form.php.

Hi, . You are years old!
skwilliams
Forum Newbie
Posts: 5
Joined: Tue Aug 19, 2003 3:35 pm

Post by skwilliams »

It was a problem with $HTTP_POST_VARS

It works fine now.

Thanks for all your help!
Post Reply