469,361 Members | 2,318 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,361 developers. It's quick & easy.

How do I populate a form from database and update database on one page

Without going into great detail (which I can do if it turns out more info is needed), I would like to:

All on one page, using ajax:
1. In a text field, have the user enter a plant name to insert into a database
2. Have a form pop up with attributes for the plant (name, scientific name, related species)
3. Have the user fill this field out and submit it to a mysql database table.

So far I have part 1 and 2 working. However, I cannot figure out how to continue to part 3 without submitting the data and loading to the next page for confirmation of insertion.

Please look and see what I've accomplished so far at this link

I can provide further information and source if it is needed. Thanks

Esscher
Feb 22 '08 #1
2 1357
It might be helpful for you to run the code yourself, so i'll include my sources

demo_add_plant.php
Expand|Select|Wrap|Line Numbers
  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. <script src="showplantprofileform.js"></script>
  5.  
  6.  
  7. <script language="javascript">
  8.  var url = "add_plant_profile_record.php?id="; 
  9.  
  10.        function handleHttpResponse() {   
  11.         if (http.readyState == 4) {
  12.               if(http.status==200) {
  13.                   var results=http.responseText;
  14.               document.getElementById('PlantInfoDiv').innerHTML = results;
  15.               }
  16.               }
  17.         }
  18.  
  19.         function showPlantProfileForm() {     
  20.             var sId = document.getElementById("Plant2Check").value;
  21.             http.open("GET", url + escape(sId), true);
  22.             http.onreadystatechange = handleHttpResponse;
  23.             http.send(null);
  24.         }
  25. function getHTTPObject() {
  26.   var xmlhttp;
  27.  
  28.   if(window.XMLHttpRequest){
  29.     xmlhttp = new XMLHttpRequest();
  30.  
  31.   }
  32.   else if (window.ActiveXObject){
  33.     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  34.     if (!xmlhttp){
  35.         xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
  36.     }
  37.  
  38. }
  39.   return xmlhttp;
  40.  
  41.  
  42. }
  43. var http = getHTTPObject();
  44. </script>
  45.  
  46. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  47. <title>Phytobase Home</title>
  48. </head>
  49. <body bgcolor="white">
  50.  
  51. <form>
  52. Add Plant Profile (type anything):<input id="Plant2Check" type="text" name="inputplant" value="">
  53. <p><input type="button" value="Click 4 Plant Profile Form" onClick="showPlantProfileForm()" />
  54.  
  55. </form>
  56. <div id="PlantInfoDiv"><b>Plant info will be listed here.</b></div>
  57.  
  58. </body>
  59. </html>
  60.  
add_plant_profile_record.php
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. include ("privatedbconfig.php");
  4. include ("dbconnection.php");
  5.  
  6.  
  7. echo "You've requested to add " .$_GET['id'] .".<br>";
  8.  
  9. $result=mysql_query("select * from plants where common_name='$inputplant'");
  10. $numrows=mysql_num_rows($result);
  11. if ($numrows > 0) 
  12.  {
  13.  echo "Plant is already in database.  Cannot Insert";
  14.  ?><a href="http://66.87.141.7/phytobase/p2/home.html">Click to Return Home</a><?php
  15.  }
  16. else
  17.  {
  18. ?>
  19. <html xmlns="http://www.w3.org/1999/xhtml">
  20. <head>
  21. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  22. </head>
  23.  
  24. <body>
  25. <form id="add_plant_profile_record" name="add_plant_profile_record">
  26.  
  27. <table border="1" cellpadding="3" cellspacing="0">
  28. <tr>
  29. <td align="center" bgcolor="#FFCC00"><strong>Property</strong></td>
  30. <td align="center" bgcolor="#FFCC00"><strong>What to Insert</strong></td>
  31. <?php
  32. ?>
  33.  
  34. </tr>
  35. <tr>
  36. <td bgcolor="#FFFFCC"><?php echo "Common Name";  ?></td>
  37. <td bgcolor="#FFFFCC"><input name="common_name" type="text" value="<?php echo $_GET['id'] ?>"</td>
  38. </tr>
  39. <tr>
  40. <td bgcolor="#FFFFCC"><?php echo "Additional Names";  ?></td>
  41. <td bgcolor="#ffffcc"><textarea  name="additional_names" value=""></textarea></td>
  42. </tr>
  43. <tr>
  44. <td bgcolor="#FFFFCC"><?php echo "Genus";  ?></td>
  45. <td bgcolor="#ffffcc"><input id="genus" name="genus" type="text" value="" /></td>
  46. </tr>
  47. <tr>
  48. <td bgcolor="#FFFFCC"><?php echo "Species";  ?></td>
  49. <td bgcolor="#ffffcc"><input name="species" type="text" value="" /></td>
  50. </tr>
  51. <tr>
  52. <td bgcolor="#FFFFCC"><?php echo "Part Used";  ?></td>
  53. <td bgcolor="#ffffcc"><input name="part_used" type="text" value=""/></td>
  54. </tr>
  55. <tr>
  56. <td bgcolor="#FFFFCC"><?php echo "Warnings";  ?></td>
  57. <td bgcolor="#ffffcc"><textarea id="main" name="warnings" value=""></textarea></td>
  58. </tr>
  59. <tr>
  60. <td bgcolor="#FFFFCC"><?php echo "Picture";  ?></td>
  61. <td bgcolor="#ffffcc"><textarea name="picture" value=""></textarea></td>
  62. </tr>
  63. <tr>
  64. <td bgcolor="#FFFFCC"><?php echo "Effects";  ?></td>
  65. <td bgcolor="#ffffcc"><textarea id="main" name="fx"></textarea></td>
  66. </tr>
  67. <tr>
  68. <td bgcolor="#FFFFCC"><?php echo "Law";  ?></td>
  69. <td bgcolor="#ffffcc"><input name="law" type="text" value=""/></td>
  70. </tr>
  71. <tr>
  72. <td bgcolor="#FFFFCC"><?php echo "Native Land";  ?></td>
  73. <td bgcolor="#ffffcc"><input name="native_land" type="text" value=""/></td>
  74. </tr>
  75. <tr>
  76. <td bgcolor="#FFFFCC"><?php echo "Social Group";  ?></td>
  77. <td bgcolor="#ffffcc"><input name="social_group" type="text" value=""/></td>
  78. </tr>
  79. <tr>
  80. <td bgcolor="#FFFFCC"><?php echo "Additional Notes";  ?></td>
  81. <td bgcolor="#ffffcc"><textarea name="notes" value=""></textarea></td>
  82. </tr>
  83. <tr>
  84. <td bgcolor="#FFFFCC"><?php echo "References";  ?></td>
  85. <td bgcolor="#ffffcc"><textarea name="ref"  value=""></textarea></td>
  86. </tr>
  87. </table>
  88. <input type="button" name="submit_plant_profile" onClick="submit_plant_profile_data()" value="Add Info to DB" />
  89. </form><p>
  90. <div id="putItHere">This should change when the data has been inserted</div>
  91. <br>
  92.  
  93. </body>
  94. </html>
  95.  
  96. </body>
  97. </html>
  98. <?php
  99. }
  100. ?>
  101.  
plants mysql table
Expand|Select|Wrap|Line Numbers
  1. --
  2. -- Table structure for table `plants`
  3. --
  4.  
  5. CREATE TABLE `plants` (
  6.   `id` smallint(6) NOT NULL auto_increment,
  7.   `common_name` varchar(255) NOT NULL default '',
  8.   `additional_names` varchar(2000) default NULL,
  9.   `genus` varchar(255) default NULL,
  10.   `species` varchar(255) default NULL,
  11.   `warnings` varchar(2000) default NULL,
  12.   `picture` varchar(255) default NULL,
  13.   `fx` varchar(2000) default NULL,
  14.   `law` varchar(1000) default NULL,
  15.   `native_land` varchar(255) default NULL,
  16.   `social_group` varchar(2000) default NULL,
  17.   `notes` varchar(10000) default NULL,
  18.   `ref` varchar(1000) default NULL,
  19.   PRIMARY KEY  (`id`,`common_name`)
  20. ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 ;
  21.  
  22. --
  23. -- Dumping data for table `plants`
  24. --
  25.  
  26. INSERT INTO `plants` (`id`, `common_name`, `additional_names`, `genus`, `species`, `warnings`, `picture`, `fx`, `law`, `native_land`, `social_group`, `notes`, `ref`) VALUES
  27. (1, 'kava kava', 'none', 'piper', '', '', '', 'sedative', '', '', '', '', '')
  28.  
Thanks for checking this out and helping

esscher
Feb 23 '08 #2
acoder
16,027 Expert Mod 8TB
See an AJAX POST example. Post the values entered to a script which inserts the values into the MySQL database.
Feb 25 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Japhy | last post: by
1 post views Thread by chromis | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.