Form with an if...loop

Reply

Join Date: Jun 2008
Posts: 13
Reputation: AliHurworth is an unknown quantity at this point 
Solved Threads: 0
AliHurworth AliHurworth is offline Offline
Newbie Poster

Form with an if...loop

 
0
  #1
Sep 27th, 2009
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):

  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:
  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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,493
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Form with an if...loop

 
0
  #2
Sep 27th, 2009
Try uncommenting line 33 where you have commented the function mysql_query(). Also try replacing the $_SERVER['REQUEST_METHOD'] with the following:
  1. if (isset($_POST) && !empty($_POST)) {
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - MacGyver Fan
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 13
Reputation: AliHurworth is an unknown quantity at this point 
Solved Threads: 0
AliHurworth AliHurworth is offline Offline
Newbie Poster

Re: Form with an if...loop

 
0
  #3
Sep 28th, 2009
Thanks cwarn.
I've altered the code as you suggested:
  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
mysql.jpg  
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,493
Reputation: cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about cwarn23 has a spectacular aura about 
Solved Threads: 136
cwarn23's Avatar
cwarn23 cwarn23 is offline Offline
Nearly a Posting Virtuoso

Re: Form with an if...loop

 
0
  #4
Sep 28th, 2009
Try this:
  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>
Try not to bump 10 year old threads as it can be really annoying.
http://syntax.cwarn23.net/
Smilies: ^_* +_+ v_v -_- *~*`
My favourite PC. - MacGyver Fan
Reply With Quote Quick reply to this message  
Reply

Tags
forms, if...loop, mysql, php

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC