8 Replies - 1013 Views - Last Post: 15 August 2011 - 08:32 AM Rate Topic: -----

#1 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

[solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:07 AM

Hey guys hows it going? I have currently taken up a PHP task with the project from my boss to make a form for some employee information and get it into a MySQL Database. Well first thing is first, I don't know PHP so learning some of it real quick, I usually program in Java so I do have some experience in it.

This is just a simple html file and a input file to read what it says and print it, like I said, basics. :blush:

testpage.php
<html>
<head>
<title>Listing 9.2 A 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>


eg9.3.php
<html>
<head>
<title>Listing 9.3 reading input from the form in listing 9.2</title>
</head>
<body>
<?php
print "Welcome <b>$user</b><P>\n\n";
print "Your address is:<P>\n\n<b>$address</b>";
?>
</body>
</html>


Now the problem I am having is that is is saying that user and address are undefined variables in the eg9.3 file. I have the files saved to the same folder so shouldn't they be able to feed off one another since I had the form action get line in the html file?

Any help would be appreciated, thanks!

This post has been edited by Fuzzyness: 15 August 2011 - 08:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: [solved]Simple HTMl form and getting input

#2 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 358
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: [solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:11 AM

When you take input elements from a form you you must create the variable like so:
$user = $_POST['user'];
$address = $_POST['address'];


As you can tell I used the post method and you used get. You should be using post if you're going to be writing to a database. If you're trying to use get and put the items in the URL (I don't see why you would), then you would use $_GET instead.

You're also capable of echoing them out, incase you were going to ask :). I hope this helps.

This post has been edited by EnvXOwner: 15 August 2011 - 08:12 AM

Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: [solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:15 AM

They are contained in the $_GET superglobal, which is an array. You can access them by doing:
$_GET['user']
$_GET['address']



I would encourage use of $_POST though, so that values being added to the database aren't in the URL bar. Still, either way, you should validate and sanitize your inputs.

You might want to take a look at PDO for interacting with the database. Use that or MySQLi. The mysql_*() family of functions is deprecated though.

Hope this helps some. :)
Was This Post Helpful? 2
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: [solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:17 AM

That cleared it right up thank you for that post! I was using GET simply because I am just testing the water before I hope in so to speak. The PHP book my boss gave me had nothing like that in the book example code where I got this from. Once again thanks mate!
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12178
  • View blog
  • Posts: 45,247
  • Joined: 27-December 08

Re: [solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:18 AM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

#6 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 358
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: [solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:18 AM

Also take a look at PDO and MySQLi as macosxnerd101 said, I find them really useful :)
Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: [solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:19 AM

View PostEnvXOwner, on 15 August 2011 - 05:11 PM, said:

When you take input elements from a form you you must create the variable like so:
$user = $_POST['user'];
$address = $_POST['address'];

though generally, there is no need to assign the variables from GET/POST. you too easily forget where the values are coming from (user input) and forget that it is unsafe (e.g. SQL Injection, XSS, etc.)

additionally, putting PHP and HTML in one file is the beginning of trouble. start with content-code separation as soon as possible (esp. when you’re not new to programming). this also prevents header problems (Sessions, Redirects).
Was This Post Helpful? 2
  • +
  • -

#8 EnvXOwner  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 358
  • View blog
  • Posts: 2,319
  • Joined: 10-August 09

Re: [solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:22 AM

lol. Here is a revised version of what I said
$user = mysql_real_escape_string(htmlentities($_POST['user']));
$address = mysql_real_escape_string(htmlentities($_POST['address']));


Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4138
  • View blog
  • Posts: 13,074
  • Joined: 08-June 10

Re: [solved]Simple HTMl form and getting input

Posted 15 August 2011 - 08:32 AM

using Prepared Statements even makes that obsolete since they are immune to SQL Injection.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1