This is a discussion on PHP doesn't allow me to manage forms within the PHP Language forums, part of the PHP Programming Forums category; I have installed PHP a number of times following tutorials from online and from a book. I have been able ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I have installed PHP a number of times following tutorials from online
and from a book. I have been able to process simple functions such at date() but when it comes to simply sending information from a HTML form to a PHP page to display it doesn't work. I know it isn't my programming as i have copied it word for word from a book, and even used one of their examples from the site. The code I have used in the PHP page is: <?php /* This page receives and handles the data generated by "form.htm".*/ print "Your first name is $FirstName.<BR>\n"; print "Your last name is $LastName.<BR>\n"; print "Your E-mail address is $email.<BR>\n"; print "This is what you had to say:<BR>\n$Comments<BR>\n"; ?> and all of those variables relate to a text field in the form on the HTML page (form.htm). Any suggestions on how to sort this out? Thank you very much |
|
|||
|
Village wrote:
> I have installed PHP a number of times following tutorials from online > and from a book. I have been able to process simple functions such at > date() but when it comes to simply sending information from a HTML form > to a PHP page to display it doesn't work. > > I know it isn't my programming as i have copied it word for word from a > book, and even used one of their examples from the site. > > The code I have used in the PHP page is: > > <?php > /* This page receives and handles the data generated by "form.htm".*/ > print "Your first name is $FirstName.<BR>\n"; > print "Your last name is $LastName.<BR>\n"; > print "Your E-mail address is $email.<BR>\n"; > print "This is what you had to say:<BR>\n$Comments<BR>\n"; > ?> > > and all of those variables relate to a text field in the form on the > HTML page (form.htm). > > Any suggestions on how to sort this out? > > Thank you very much Hi, Well, you didn't show us all your code, so it is hard to say what went wrong. Try this: send.html *************************************** <html> <body> <form action="receive.php" method="Post"> firstname:<input type="text" name="firstname"> <br> LastName<input type="text" name="LastName"> <br> email:<input type="text" name="email"> <br> Comments:<input type="text" name="Comments"> <br> <input type="submit" value="Post me please"> </form> </body> </html> *************************************** and the file receive.php: *************************************** <? $firstname=$_POST["firstname"]; $LastName=$_POST["LastName"]; $email=$_POST["email"]; $Comments=$_POST["Comments"]; ?> <html> <body> I received: <br> Firstname: <? echo $firstname; ?><br> LastName: <? echo $LastName; ?><br> email: <? echo $email; ?><br> Comments: <? echo $Comments; ?><br> </body> </html> *************************************** That code should always work. Regards, Erwin Moller |
|
|||
|
Following on from Village's message. . .
>I have installed PHP a number of times following tutorials from online >and from a book. I have been able to process simple functions such at >date() but when it comes to simply sending information from a HTML form >to a PHP page to display it doesn't work. > >I know it isn't my programming as i have copied it word for word from a >book, and even used one of their examples from the site. The book was taking some deprecated shortcuts. (See 'register globals' in the manual) try print_r($_POST); or print_r($_REQUEST); to see what has been passed to PHP, then you can pick the bits you want out of the array, *validate them*, *validate them again* then use them. -- PETER FOX Not the same since the bolt company screwed up peterfox@eminent.demon.co.uk.not.this.bit.no.html 2 Tees Close, Witham, Essex. Gravity beer in Essex <http://www.eminent.demon.co.uk> |
|
|||
|
On 2006-01-06, Erwin Moller <since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote:
> $firstname=$_POST["firstname"]; > $LastName=$_POST["LastName"]; > $email=$_POST["email"]; > $Comments=$_POST["Comments"]; I've got the feeling that tutorials should spend more attention to cleaning input and output so the student can't shoot himself in the foot. $html = array(); $html['firstname'] = htmlentities($_POST['firstname'], 'UTF-8'); .... echo "Your firstname is: {$html['firstname']} <br/>"; ... -- Met vriendelijke groeten, Tim Van Wassenhove <http://timvw.madoka.be> |
|
|||
|
Tim Van Wassenhove wrote:
> On 2006-01-06, Erwin Moller > <since_humans_read_this_I_am_spammed_too_much@spam yourself.com> wrote: >> $firstname=$_POST["firstname"]; >> $LastName=$_POST["LastName"]; >> $email=$_POST["email"]; >> $Comments=$_POST["Comments"]; > > I've got the feeling that tutorials should spend more attention to > cleaning input and output so the student can't shoot himself in the foot. > > $html = array(); > $html['firstname'] = htmlentities($_POST['firstname'], 'UTF-8'); > ... > > > echo "Your firstname is: {$html['firstname']} <br/>"; > .. > > Hi/hoi Tim, I agree with that. I know I had my fair share of 'obscure bugs' when I started with PHP, and I guess 90% had to do with characters: encoding/in-out database/show them in html, show them in a textfield of a form, etc. etc. You know what I am talking about. :P 'obscure bugs' in a ironic way because it was of course me who scewed up by not paying attention to them from the start. They are excactly the kind of things that make a habit of returning later in the project, and always in a worse form, and of course close to deadline. ;-) But I didn't want to confuse the OP with that because he clearly had problems with the concept of forms and superglobals $_POST and $_GET. btw, I didn't know that UTF-8 directive for htmlentities. Now I do. :-) Regards, Erwin Moller |
|
|||
|
Village wrote:
> > Brilliant, that works fine. Thanks. I will look up about the Register > Globals thing to see what it is. > A good bit of advice: Don't use, nor rely on, register globals. That functionality is going away in the future, according to the PHP web site, I think it's slated for removal entirely in PHP 6. Use the Superglobals instead. Some of the common ones are $_GET, $_POST, and $_COOKIE. There are others, also. It's definitely preferable to use them, however, because with register globals (just as an example), let's say you have a POST form with a hidden value UID and the user wanted to attempt to override it with a GET request appending UID to the URL. The opposite could also be true, but not as likely. The idea is that by using the superglobals, everything is separated into different places, and it makes life somewhat more secure, easier, and more intuitive to deal with. You should get a book on PHP 5. :) Later, Mike |