1

HI,

I wrote this code in php.

    <head>
<title>listent</title>
</head>
<body>
<form  action="untitled 3.php">
<input type = "text" name = "user">
<br>
<textarea name = "address" rows = "10" cols = "40">

</textarea>
<br>
<input type = "submit" value = "heat it">
<br>
<select name="combobox" multiple[]>
<option>mehdi
<option>nine
</select>

</form>

</body>
</html>

now when i click on submit button untitled 3.php is run.

in untitled 3.php i wrote

<?php

print "welcome $user";



?>

but it has error.

Notice: Undefined variable: user in C:\xampp\htdocs\me\Untitled 3.php on line 4
welcome

what is problem?how can i solve it?

Mahdi_Nine
  • 11,985
  • 25
  • 78
  • 112
  • do you **really** have a space in the filename "untitled 3.php"? – AJ. May 07 '11 at 20:35
  • See [That's because ("global") variables aren't really global in PHP.](http://stackoverflow.com/questions/1557787/are-global-variables-in-php-considered-bad-practice) – mario May 07 '11 at 20:38

3 Answers3

3

Form values don't just magically appear as variables anymore - at least not in any decently modern and properly configured PHP installation. You need to do $_GET["user"] to access the value which is sent by the form (into the URL - you might want to read about the difference between GET and POST)

And please, please use more descriptive names for your files...

Matti Virkkunen
  • 58,926
  • 7
  • 111
  • 152
1

PHP Globals wont survive the new page.

In you case you must use the POST variables sent by your form.

So in untitled3.php you should have

echo "welcome ".$_POST['user'];

PS : I would avoid spaces in PHP filenames.

c0mm0n
  • 959
  • 5
  • 6
1

First you should specify a Form submission method in your first page:

<form  action="untitled 3.php" method="post">

Then you have access to all posted values in the $_POST array in untitled 3.php:

$user = $_POST['user'];
jeroen
  • 88,615
  • 21
  • 107
  • 128
  • The method attribute is explicitly optional. – Quentin May 07 '11 at 20:39
  • it didn't have any error but in page showed **Notice: Undefined index: user in C:\xampp\htdocs\me\Untitled 3.php on line 4** – Mahdi_Nine May 07 '11 at 20:42
  • @David Dorward Thanks, I didn't know that, I've never omitted it. – jeroen May 07 '11 at 20:42
  • @mehdi If you haven't added a method, it seems you can use `$_GET` instead of `$_POST`. You can also use `$_REQUEST` as that is both arrays combined, but I personally prefer to specify a method and get the values accordingly. – jeroen May 07 '11 at 20:45