943,960 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 4437
  • PHP RSS
Jun 13th, 2004
0

Error getting forms to send information

Expand Post »
heres the code to send the info

PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <HEAD>
  3. <title> form Handling with PHP </title>
  4. </head>
  5. <body bgcolor="#FFFFFF">
  6. <center>
  7. <form method=post action="submittedinfo.php">
  8. <input type="hidden" name="id" value="NULL">
  9. <table>
  10. <tr>
  11. <td colspan="2"><font SIZE="+0" face="verdana">
  12. Lets see if we cant get this form to work!
  13. </td>
  14. </tr>
  15. <tr>
  16. <td>
  17. </td>
  18. </tr>
  19. <tr>
  20. <td align="left"><font SIZE="+0" face="verdana">
  21. <b>Your name <br \>Your E-Mail Address</b>
  22. </td>
  23. <td>
  24. <input type="text" name="name" id="name">
  25. <br />
  26. <input type="text" name="email" id="email">
  27. </td>
  28. </tr>
  29. <tr>
  30. <td colspan="2"><center>
  31. <SELECT name="opinion" id="opinion">
  32. <option value="is great">I like your site</option>
  33. <option value="is OK">Your Site is OK</option>
  34. <option value="is horrible">Your Site is horrible</option>
  35. </SELECT><p><input type="submit" value="Tell us!">
  36. </td>
  37. </tr>
  38. </table>
  39. </form>
  40. </body>
  41. </html>

and here is the code to show the information entered by the user

[php]<?
$DBhost = "localhost";
$DBuser = "michael";
$DBpass = "koolaide";
$DBName = "phpforms";
$table = "information";


mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
@mysql_select_db("$DBName") or die("Unable to select database $DBName");
$sqlquery = "INSERT INTO $table VALUES('$id','$name','$email','$opinion')";
$results = mysql_query($sqlquery);
mysql_close();
echo "
<html>
<head>
<title> PHP and MySQL </title>
</head>
<body>
<center>
<table border='0' width='500'>
<tr>";
echo " <td>
<font face='verdana' size='+0'>
<center>
<p>You Just Entered This Information Into the Database</p>
</center>";

echo " <blockquote>
<center>
<p>
Name :<p> $name E-Mail :</p> $email <p>Opinion :</p> $opinion
</p>
</center>
</blockquote>
</td>
</tr>
</table>
</center>
</body>
</html>";
?>
[/php]

heres the problem, when i run the HTML on the first page and fill out the form, i press send the info, it should bring up another page saying You entered so and so information. but instead i get you entered so and so info but it doesnt list the info, adding to that, im very noob to PHP, this is another tutorial that i found, and im begining to think that alot of tutorials have errors in them or somthing. i even went into my mysql and added the database and lines of info needed (again using his tutorial on how to) any help would be greatly appreciated.


ok, so i was able to fix the form, and ill post how i did it too.

[php]$name = "$_POST[name]";
$email = "$_POST[email]";
$opinion = "$_POST[opinion]";[/php]

needed to be defined at the top of the page. now when i view my databases via phpmyadmin, i can see that the information has been added to the database, but now i need to write a script that will print out all data in the database to onscreen.
Last edited by samaru; Jun 15th, 2004 at 2:00 am.
Similar Threads
Reputation Points: 152
Solved Threads: 39
Master Poster
Killer_Typo is offline Offline
778 posts
since Apr 2004
Jun 13th, 2004
0

Re: Error getting forms to send information

Glad you were able to help yourself, but remember, don't double post. If you need help with displaying the data, give us a holler.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002
Jun 13th, 2004
0

Re: Error getting forms to send information

What i like to do with form submissions...

First off, you never want to put your SQL login info directly in your script instead make a file like db.cnnt.php
Inside, write something like this...
PHP Syntax (Toggle Plain Text)
  1. <?PHP
  2. // No direct call...
  3. if(ereg('db.cnnt.php', $_SERVER['SCRIPT_NAME']))
  4. {
  5. die("Access Denied! No Direct Call!");
  6. }
  7. // Set DB login vars...
  8. $SQL_Host = 'localhost:3306';
  9. $SQL_Usr = 'Your_Username';
  10. $SQL_Pss = 'Your_Password';
  11. $SQL_DB = 'Database_Name';
  12. // Now connect to the database server...
  13. if(!$SQL_LNK = @mysql_pconnect($SQL_Host, $SQL_Usr, $SQL_Pss))
  14. {
  15. die("Couldn't Connect to Database Server...");
  16. }
  17. // Now select the database...
  18. if(!$SQL_SLT = @mysql_select_db($SQL_DB))
  19. {
  20. die("Couldn't Connect to database");
  21. }
  22. // Or if you use multiple databases/users you can just set the different login Vars here and call connect function in the script.
  23. ?>

Now make a functions file to house the functions you use on many different pages...
functions.inc.php
PHP Syntax (Toggle Plain Text)
  1. <?PHP
  2. //functions
  3. function ControlContent($string)
  4. {
  5. //You can set more vars to this one. for instance if you want to only strip_tags from certain inputs...You could add $HTML to the function vars. If $HTML == 0 or $HTML == NULL then strip_tags ELSE dont strip_tags. But in this example im just gonna fix the strings
  6.  
  7. $string = strip_tags(trim(stripslashes(urldecode($string))));
  8. return $string;
  9. } //End of function
  10.  
  11. function NoGET($METHOD)
  12. {
  13. if(eregi('get', $METHOD))
  14. {
  15. die("Access denied! Post method only");
  16. }
  17. }
  18.  
  19. //Of course there are many more functions you could make but I'll stop there.
  20. ?>

Now finally
ForumHandle.php
PHP Syntax (Toggle Plain Text)
  1. <?PHP
  2. require_once("db.cnnt.php");
  3. require("functions.inc.php");
  4. NoGET($_SERVER['REQUEST_METHOD']);
  5. // takes takes all vars out of the $_POST array. Now $_POST[email] becomes $email
  6. extract($_POST);
  7. // This function to clean the input string...
  8. ControlContent($email);
  9.  
  10. // Now that the strings are clean you can insert them into your database.
  11.  
  12. // After inserting the data close Mysql connection and exit script.
  13. mysql_close();
  14. exit();
  15. ?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fEcAlMaTteR is offline Offline
6 posts
since Jun 2004
Jun 13th, 2004
0

Re: Error getting forms to send information

Quote originally posted by fEcAlMaTteR ...
What i like to do with form submissions...

First off, you never want to put your SQL login info directly in your script instead make a file like db.cnnt.php
Yeah I agree. I usually set aside a separate php file where I include global variables and settings. Here I include the passwords. However, I think Killer_Typo is doing this now because it's only an example to show how to get data from forms and do stuff with it.
Team Colleague
Reputation Points: 262
Solved Threads: 18
a.k.a inscissor
samaru is offline Offline
1,227 posts
since Feb 2002

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Fun with Forms
Next Thread in PHP Forum Timeline: New to PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC