$_POST Problems

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
miniquie
Forum Newbie
Posts: 3
Joined: Sat Sep 27, 2008 8:39 pm

$_POST Problems

Post by miniquie »

Hi everyone:

I'm currently in the process of learning PHP. I have been trying to work with the $_POST function and forms. I am running the latest verision of PHP on BRS WebWeaver.

Here's what my code looks like on my html page called form.htm:

Code: Select all

<html>
<head></head>
<body>
<form action="message.php" method="post">
Enter your message: <input type="text" name="msg" size="30">
<input type="submit" value="Send">
</form>
</body>
</html>
and then my php file, message.php:

Code: Select all

<html>
<head></head>
<body>
 
<?php
// retrieve form data
$input = $_GET['msg'];
// use it
echo "You said: <i>$input</i>";
?>
 
</body>
</html>
When I try to execute the script, it gives me this message:
You said: PHP Notice: Undefined index: msg in D:\PHP\message.php on line 7
It seems to be a problem with the variable that is being used... I would think that it's not seeing the form by the name of "msg" on the form.htm file, and that's what's confusing it.

Any suggestions?

Thanks in advance.

-mq
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Re: $_POST Problems

Post by andre_c »

On the form you're specifying post as the method. That means the values will be stored on the $_POST array, not the $_GET array.

Replace line 7 or message.php with:

Code: Select all

$input = $_POST['msg'];
Alvaro
miniquie
Forum Newbie
Posts: 3
Joined: Sat Sep 27, 2008 8:39 pm

Re: $_POST Problems

Post by miniquie »

Ok. I have changed line 7 of message.php from

Code: Select all

$input = $_GET['msg'];
to

Code: Select all

$input = $_POST['msg'];
But still no luck; I still get the error
You said: PHP Notice: Undefined index: msg in D:\PHP\message.php on line 7
Any other suggestions? I am curious to see if it has anything to do with my web server software... just another possibility.

Thanks.

-mq
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: $_POST Problems

Post by Stryks »

Well .. lets see what we have here.

Comment out line 7 and 9 from your original message.php posting and add this code block.

Code: Select all

 
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo 'The form has been submitted.<br>';
 
        if(isset($_POST['msg'])) echo 'msg is set to ' . $_POST['msg'] . '<br>';
        else {
           echo 'msg has not been set for some strange reason.<br><br>POST Dump:<br>';
           print_r($_POST);
        }
    } else echo 'This is not a form submission.<br>';
 
Post back with what messages this produces.

Cheers
miniquie
Forum Newbie
Posts: 3
Joined: Sat Sep 27, 2008 8:39 pm

Re: $_POST Problems

Post by miniquie »

This is the result of what stryks had told me to put in the code:
The form has been submitted.
msg has not been set for some strange reason.

POST Dump:
Array ( ) You said: PHP Notice: Undefined variable: input in D:\PHP\message.php on line 18
tkj
Forum Newbie
Posts: 9
Joined: Sun Sep 28, 2008 5:55 am

Re: $_POST Problems

Post by tkj »

I'm not sure.
Try changing the parameter to something else, like $_POST['mess']
Otherwise, try renaming the message.php file to something else!

I don't know if theese two things could interrupt the script, but maybe...
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: $_POST Problems

Post by Stryks »

Hey ... that's a neat trick.

Ummm ... try changing your form tag to be ...

Code: Select all

<form name="form_1" action="message.php" method="post">
Failing that .. we might need to look at your install config. Unless there are other suggestions??? :?
Post Reply