what is wrong with my code?

A

Anonymous

Guest
hi. I'm trying to learn php but i am stuck at this part: im trying to create a form where you submit your name and adress. anyway this is what i hav:

Code:
 <html>
 <head>
 <title> simple HTML form</title>
 </head>
 <body>
 <form action="eg9.3.php" method="GET">
 <input type="text" name="user">
 <br>
 <textarea name="address" rows="5" cols="40">
 </textarea>
 <br>
 <input type="submit" value="hit it!">
 </form>
 </body>
 </html>

thats where the user would input the text. right? then submit it. this is the php script now

Code:
 <html>
 <head>
 <title>input </title>
 </head>
 <body>
 <?php
 echo "Welcome: $user<br>";
 echo "Your address is: $address";
 ?>
 </body>
 </html>

why, when the user hits submit, the only thing that is displayed in the browsers is welcome: and your address is:

???? why doesnt it display the value that the user submitted?
 
okej... you might have register_globals=off in PHP.ini... this is really recommended so start learning PHP with regoster_globals off :) this will help you to develop really good and secure apps.
that means that you will have to use $_GET['vairiable_name']

In most cases people use method="POST" and $_POST['variable_name']

GET will make the information sumbitted visible in the URL..
$_GET['variable_name'] will get the information from the URL..

for instance: you go to http://www.mysite.com/index.php?var=test
OR submit a form with method ="GET" and an input field name="var1" and user inputs "test"
You want to get and output the value of the var1 varible..
you write
Code:
 echo($_GET['var1']);

You will get the output:
Code:
test

Now... you are using a form with a post method:
Code:
<form action="mytest.php" method="POST">
<input type="text" name="var2">
<input type="submit" name="submit" value="Submit">
</form>

Now... after submiting you want to get your variable, right?
use
Code:
echo($_POST['var2']);

You will get your output there :)
 
Hmmz... havnt seen that one on this forum before Oleg, sorry :)
However in most cases i prefere to give my own information, like writing a reply like i did here and so...
 
sorry to be a bother again. i have another question.

Code:
<html>
<head>
<title> listing 9.5 reading input from a form in listing 9.4</title>
</head>
<body>
<?
echo "welcome:".($_POST['user']);
echo "<br>your address is:".($_POST['address']);
echo "<br>Your products choices are:";

foreach($product as $value)
   {
     echo "$value";
   }
?>
</body>
</html>

welcome:pedro
your address is:california beverly hills 90210
Your products choices are:

everytime is fine until:

Warning: Invalid argument supplied for foreach() in c:\program files\apache group\apache\htdocs\eg9.5.php on line 10
 
Okej... make sure that $product is an array...
take a look here: http://se.php.net/manual/en/control-structures.foreach.php
 
it is an array-
Code:
<html>
<head>
<title>an html form including a select element</title>
</head>
<body>
<form action  = "eg9.5.php" method = "POST">
<input type = "text" name = "user">
<br>

<textarea name ="address" rows = "5" cols ="40">
</textarea>
<br>

<select name = "product[]" multiple>        //see its an array.
<option>sonic screwdriver
<option>tricoders
<option>ORAC AI
<option>HAL 2000
</select>
<br>
<input type = "submit" value ="click here to submit">

</form>
</body>
</html>

im going to take a look at your link also.
 
looked and looked but nothing. everything seems to be fine
 
True... but im used to do that as its my coding style... nothing else :)

It makes it easy to code the same way all the time, if i output only one thing or 1000 in one echo...

Therefore i always use brackets :)
 
Oleg, i think you missunderstod Edd224..

The thing is that he asked about brackets after echo... even with register_globals=Off (the way it always should be) you dont have to use them with echo... but it makes so much easier to write and a code so much more readable..

So therefore im using them... but like i said: its simply my programming style..
 
Back
Top