what's wrong with this? Just 1 varialble

I just started learning PHP and the very first time I try to link an .htm page to a .php file I can’t make it work.

I’m frustrated because this is the most basic of programs and I can’t get it to work. The print command is returning “Hi there, !” The variable is not being printed.

The name = “userName” in the .htm page is not working with the $userName in the .php file.

.htm:

[code]

What's your name?

What's your name?

Writing a form for user input

Please type your name:
[/code]

.php:

[code]

Hi User

Hi User

PHP program that receives a value from "whatsName"

<?php print "

Hi there, $userName!

"; ?> [/code]

put this into your php code

$userName = $_GET['userName']; 

That should work now, although it might be better changing

method = 'get' 

to

method = 'post'

and putting

$userName = $_POST['userName']

so that it doesn’t appear in the URL e.g. with get

http://www.mysite.com/something.php?$userName=hellobob

e.g. with post
http://www.mysite.com/something.php

the problem is you didn’t set the variable from the form, using $_POST or $_GET.

tell me if that works

Perfect!!!

Thank you so much eFlame.

Being able to link a web form to a PHP program is important to me and you helped me get it done.

May I ask you what $userName = $_POST[‘userName’] actually does. If you are able to break this down for me I would be much appreciative.

metcom

$_POST is the array value that comes from submitting a form. Hence method=“POST”

If you were to do a print_r($_POST) this would output all the variables and there values. Looks something like this.

echo “

”;
print_r($_POST);
echo “
”;

Outputs something like:

array(
username = ‘myusername’,
pasword = ‘password’
)

This is good in debugging to find out what variables and values are actually being passed by a submitted form.

Hope that helps.

what Ragster said, but to break it down even further,

$userName is the variable name, that you wanted to use. You want this variable to take the value of what the field with name ‘userName’ to take. By using the $_POST or $_GET your just getting the information passed from the form.

if you wanted to you could set $bob to be $_POST[‘userName’]

Personally i usually go for the post method because it doesn’t show up in the url.

Sponsor our Newsletter | Privacy Policy | Terms of Service