943,692 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2118
  • PHP RSS
Sep 27th, 2009
0

Form with an if...loop

Expand Post »
Hi all,
I have a two-part form intended to replicate the output from a toolbar, with the aim of sending data to a database.

So the data is generated by the following code (sim1.php):

PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Start</title>
  6. <link href="toolbar_popup.css" rel="stylesheet" type="text/css" />
  7. </head>
  8.  
  9. <body>
  10. <form method="post" action="toolbar_process.php">
  11. <input type="text" name="tbar_site"/>
  12. <br />
  13. <select name="tbar_type" size="2">
  14. <option value="h">housing</option>
  15. <option value="o">other</option>
  16. </select>
  17. <br />
  18. <input type="hidden" name="origin" value="tbar">
  19. <input type="submit" value="Enter details!"/>
  20. </form>
  21. </body>
  22. </html>
this is then sent to toolbar_process.php:
PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <script type="text/javascript">
  6.  
  7. <!-- Begin
  8. function popUp(URL) {
  9. eval("page" + id + " = window.open(URL, '" + id + "','toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=200,height=300');");
  10. }
  11. // End -->
  12. </script>
  13. <title>Toolbar Process</title>
  14. <link href="toolbar_popup.css" rel="stylesheet" type="text/css" />
  15. </head>
  16.  
  17. <body>
  18. <div id="wrapper">
  19. <div id="text">
  20. <?php
  21. if (!isset($origin)) {
  22. require_once('connect.php');
  23. if ($_SERVER['REQUEST_METHOD'] == "POST")
  24. {
  25. #set variables, stripslashes
  26. $type = addslashes($_POST["tbar_type"]);
  27. $site = addslashes($_POST["tbar_site"]);
  28.  
  29. # setup SQL statement
  30. $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site','$type','$origin')";
  31.  
  32. #execute SQL statement
  33. #$result = mysql_query($sql);
  34.  
  35. # check for error
  36. if (mysql_error())
  37. {
  38. print "Database ERROR1: " . mysql_error();
  39. }
  40.  
  41. //message for user
  42. echo "<h1>Thanks!</h1>Your toolbar entry: ".$site.", was submitted.";
  43. }
  44. $origin = NULL;
  45. }
  46. ?>
  47. <h2>
  48. Got more?</h2>
  49. <form method="post" action="toolbar_process.php">
  50. <input type="text" name="site"/>
  51. <br />
  52. <select name="type" size="2">
  53. <option value="h">housing</option>
  54. <option value="o">other</option>
  55. </select>
  56. <br />
  57. <input type="submit" value="Enter details!"/>
  58. </form>
  59. </div>
  60. <?php
  61. require_once('connect.php');
  62. if ($_SERVER['REQUEST_METHOD'] == "POST")
  63. {
  64. #set variables, stripslashes
  65. $type1 = addslashes($_POST["type"]);
  66. $site1 = addslashes($_POST["site"]);
  67. $origin1 = "page";
  68.  
  69. # setup SQL statement
  70. $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site1','$type1','$origin1')";
  71.  
  72. #execute SQL statement
  73. #$result = mysql_query($sql);
  74.  
  75. # check for error
  76. if (mysql_error()) { print "Database ERROR1: " . mysql_error(); }
  77.  
  78. //message for user
  79. echo "<h1>Thanks!</h1>Your entry was submitted.";
  80. }
  81. ?>
  82. </div>
  83. </div>
  84. </body>
  85. </html>

The aim is that the data should either come from sim1.php, or if the user has landed on the second page without going to sim1, the data is sent via similar form.
Both times the origin is added to the row.
Sadly, the entries are not being recorded on mysql.

I'm sure it's almost there... Can anyone help?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
AliHurworth is offline Offline
34 posts
since Jun 2008
Sep 27th, 2009
0

Re: Form with an if...loop

Try uncommenting line 33 where you have commented the function mysql_query(). Also try replacing the $_SERVER['REQUEST_METHOD'] with the following:
php Syntax (Toggle Plain Text)
  1. if (isset($_POST) && !empty($_POST)) {
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Sep 28th, 2009
0

Re: Form with an if...loop

Thanks cwarn.
I've altered the code as you suggested:
PHP Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.  
  6. <title>Toolbar Process</title>
  7. <link href="toolbar_popup.css" rel="stylesheet" type="text/css" />
  8. </head>
  9.  
  10. <body>
  11. <div id="wrapper">
  12. <div id="text">
  13. <?php
  14. if (!isset($origin)) {
  15. require_once('connect.php');
  16. if (isset($_POST) && !empty($_POST))
  17. {
  18. #set variables, stripslashes
  19. $type = addslashes($_POST["tbar_type"]);
  20. $site = addslashes($_POST["tbar_site"]);
  21. $origin1 = "tbar";
  22.  
  23. # setup SQL statement
  24. $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site','$type','$origin')";
  25.  
  26. #execute SQL statement
  27. $result = mysql_query($sql);
  28.  
  29. # check for error
  30. if (mysql_error())
  31. {
  32. print "Database ERROR1: " . mysql_error();
  33. }
  34.  
  35. //message for user
  36. echo "<h1>Thanks!</h1>Your toolbar entry: ".$site.", was submitted.";
  37. }
  38. $origin = NULL;
  39. }
  40. ?>
  41. <h2>
  42. Got more?</h2>
  43. <form method="post" action="toolbar_process.php">
  44. <input type="text" name="site"/>
  45. <br />
  46. <select name="type" size="2">
  47. <option value="h">housing</option>
  48. <option value="o">other</option>
  49. </select>
  50. <br />
  51. <input type="submit" value="Enter details!"/>
  52. </form>
  53. </div>
  54. <?php
  55. require_once('connect.php');
  56. if ($_SERVER['REQUEST_METHOD'] == "POST")
  57. {
  58. #set variables, stripslashes
  59. $type1 = addslashes($_POST["type"]);
  60. $site1 = addslashes($_POST["site"]);
  61. $origin1 = "page";
  62.  
  63. # setup SQL statement
  64. $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site1','$type1','$origin1')";
  65.  
  66. #execute SQL statement
  67. $result = mysql_query($sql);
  68.  
  69. # check for error
  70. if (mysql_error()) { print "Database ERROR1: " . mysql_error(); }
  71.  
  72. //message for user
  73. echo "<h1>Thanks!</h1>Your entry: ".$site1.", was submitted.";
  74. }
  75. ?>
  76. </div>
  77. <h2>What now?</h2>
  78. Would you like to visit the <a href="index.php">wkho.me.uk homepage</a>?
  79. <br /> Or add to the <a href="directory_entry.php">Directory</a>?
  80. </div>
  81. </body>
  82. </html>
The problem now is that I have two entries when submit is pressed (see attachment).
So, how do I enter just one new row/fire the sql once? And only show the relevant message...sheesh, I know that's a lot to ask!
Attached Thumbnails
Click image for larger version

Name:	mysql.jpg
Views:	23
Size:	79.0 KB
ID:	11834  
Reputation Points: 10
Solved Threads: 0
Light Poster
AliHurworth is offline Offline
34 posts
since Jun 2008
Sep 28th, 2009
0

Re: Form with an if...loop

Try this:
php Syntax (Toggle Plain Text)
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5.  
  6. <title>Toolbar Process</title>
  7. <link href="toolbar_popup.css" rel="stylesheet" type="text/css" />
  8. </head>
  9.  
  10. <body>
  11. <div id="wrapper">
  12. <div id="text">
  13. <?php
  14. if (!isset($origin)) {
  15. require_once('connect.php');
  16. if (isset($_POST) && !empty($_POST))
  17. {
  18. #set variables, stripslashes
  19. $type = mysql_real_escape_string($_POST["tbar_type"]);
  20. $site = mysql_real_escape_string($_POST["tbar_site"]);
  21. $origin1 = "tbar";
  22.  
  23. # setup SQL statement
  24. $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site','$type','$origin')";
  25.  
  26. #execute SQL statement
  27. $result = mysql_query($sql);
  28.  
  29. # check for error
  30. if (mysql_error())
  31. {
  32. print "Database ERROR1: " . mysql_error();
  33. }
  34.  
  35. //message for user
  36. echo "<h1>Thanks!</h1>Your toolbar entry: ".$site.", was submitted.";
  37. }
  38. $origin = NULL;
  39. }
  40. ?>
  41. <h2>
  42. Got more?</h2>
  43. <form method="post" action="toolbar_process.php">
  44. <input type="text" name="site"/>
  45. <br />
  46. <select name="type" size="2">
  47. <option value="h">housing</option>
  48. <option value="o">other</option>
  49. </select>
  50. <br />
  51. <input type="submit" value="Enter details!"/>
  52. </form>
  53. </div>
  54. <?php
  55. require_once('connect.php');
  56. if (isset($_POST) && !empty($_POST))
  57. {
  58. #set variables, stripslashes
  59. $type1 = mysql_real_escape_string($_POST["type"]);
  60. $site1 = mysql_real_escape_string($_POST["site"]);
  61. $origin1 = "page";
  62.  
  63. # setup SQL statement
  64. $sql = "INSERT INTO `wkho_users`.`web_dir` (`site`, `type`,`origin`) VALUES ('$site1','$type1','$origin1')";
  65.  
  66. #execute SQL statement
  67. $result = mysql_query($sql);
  68.  
  69. # check for error
  70. if (mysql_error()) { print "Database ERROR1: " . mysql_error(); }
  71.  
  72. //message for user
  73. echo "<h1>Thanks!</h1>Your entry: ".$site1.", was submitted.";
  74. }
  75. ?>
  76. </div>
  77. <h2>What now?</h2>
  78. Would you like to visit the <a href="index.php">wkho.me.uk homepage</a>?
  79. <br /> Or add to the <a href="directory_entry.php">Directory</a>?
  80. </div>
  81. </body>
  82. </html>
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007

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: Zip Codes
Next Thread in PHP Forum Timeline: Web Crwaler : File Searching





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


Follow us on Twitter


© 2011 DaniWeb® LLC