jQuery: Textbox value to Listbox and MySQL

Reply

Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

jQuery: Textbox value to Listbox and MySQL

 
0
  #1
Jul 29th, 2009
Hi.
Suppose I have a list box, which is filled out from mysql, and I have a text box and a submit button.
I want to add the text box data into list box and at the same time I want to have the text box data into mysql as well.

so how can I do this using jquery?
I did many search but still i don't have the clue.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 199
Reputation: tanha is an unknown quantity at this point 
Solved Threads: 0
tanha tanha is offline Offline
Junior Poster

Re: jQuery: Textbox value to Listbox and MySQL

 
0
  #2
Jul 30th, 2009
I did a bit and it is working, but the list box does not appear when the page load, after after inserting new data the list box with having new data is showing correctly. I did this example just for the category now.

how can I display the list box onload as well? look the code:

category.php
  1.  
  2. <?php
  3.  
  4. $con = mysql_connect("localhost", "root", "sherzad");
  5. mysql_select_db("db_student", $con);
  6.  
  7. $category_name = $_POST['bkCategory'];
  8. $sql = "INSERT INTO tbl_category (category_name) VALUES('$category_name')";
  9. mysql_query($sql);
  10.  
  11. $category_sql = "SELECT * FROM tbl_category order by category_name";
  12. $result_category = mysql_query($category_sql);
  13.  
  14.  
  15. $out = "<select name='selectCategory' size='6' multiple='multiple' id='selectCategory'>";
  16. while($row_category = mysql_fetch_array($result_category)){
  17. $out.= "<option value=" . $row_category['category_id'] . ">". $row_category['category_name'] . "</option>";
  18. }
  19. $out.= "</select>";
  20.  
  21. echo $out;
  22. ?>

registry.php
  1.  
  2. <?php
  3.  
  4. include("conn.php");
  5.  
  6. $author_sql = "SELECT * FROM tbl_author order by first_name";
  7. $result_author = mysql_query($author_sql);
  8.  
  9. ?>
  10.  
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  14. <title>Book Registration Form</title>
  15.  
  16. <style>
  17. label{
  18. font-weight: bold;
  19. }
  20. </style>
  21.  
  22. <script type="text/javascript" src="jquery.js"></script>
  23. <script>
  24. $(function() {
  25.  
  26. $(".insertCategory").click(function() {
  27.  
  28. var test = $("#bkCategory").val();
  29. var dataString = 'bkCategory='+ test;
  30.  
  31. if(test=='')
  32. {
  33. alert("Please Enter Some Text");
  34. }
  35. else
  36. {
  37.  
  38. $.ajax({
  39. type: "POST",
  40. url: "category.php",
  41. data: dataString,
  42. cache: false,
  43. success: function(html){
  44. $("#cat").html('');
  45. $("#cat").html(html);
  46. document.getElementById('bkCategory').value='';
  47. document.getElementById('bkCategory').focus();
  48. }
  49. });
  50. } return false;
  51. });
  52.  
  53. $(".insertAuthor").click(function() {
  54.  
  55. // we want to store the values from the form input box, then send via ajax below
  56. var fname = $('#first_name').attr('value');
  57. var lname = $('#last_name').attr('value');
  58.  
  59. if(fname=='' || lname=='')
  60. {
  61. alert("Please Enter Author First Name and Last Name");
  62. }
  63. else
  64. {
  65.  
  66. $.ajax({
  67. type: "POST",
  68. url: "author.php",
  69. data: "fname="+ fname +"& lname="+ lname,
  70. cache: false,
  71. success: function(){
  72.  
  73. document.getElementById('first_name').value='';
  74. document.getElementById('last_name').value='';
  75. document.getElementById('first_name').focus();
  76. }
  77. });
  78. } return false;
  79. });
  80.  
  81. });
  82.  
  83. </script>
  84.  
  85. </head>
  86.  
  87. <body>
  88. <center><h1>Book Registration Form</h1></center>
  89. <form id="form1" name="form1" method="post" action="">
  90. <table width="800" border="0" align="center" cellpadding="3" cellspacing="3">
  91. <tr>
  92. <td>Book Title </td>
  93. <td><label>
  94. <input name="bkTitle" type="text" id="bkTitle" size="80" value="<?php echo $_POST['bkTitle']; ?>" />
  95. </label></td>
  96. </tr>
  97. <tr>
  98. <td>Book Language </td>
  99. <td><label>
  100. <select name="selectLanguage">
  101. <option value="Persian">Persian</option>
  102. <option value="English">English</option>
  103. <option value="German">German</option>
  104. </select>
  105. </label></td>
  106. </tr>
  107.  
  108. <form id="category" onsubmit="return false;">
  109. <tr>
  110. <td>Book Category (if not exist) </td>
  111. <td><label>
  112. <input name="bkCategory" type="text" id="bkCategory" value="New Category" size="50" />
  113. <input type="button" value="submit" class="insertCategory">
  114. </label></td>
  115. </tr>
  116. </form>
  117.  
  118. <tr>
  119. <td>Select Category </td>
  120. <td id="cat">
  121.  
  122. </td>
  123. </tr>
  124.  
  125. <form id="author">
  126. <tr>
  127. <td valign="top">Author Full Name (if not exist) </td>
  128. <td><label>
  129.  
  130.  
  131. <input name="first_name" type="text" id="first_name" value="New Author First Name" size="40" />
  132. <br />
  133. <br />
  134. <input name="last_name" type="text" id="last_name" value="New Author Last Name" size="40" />
  135. <input type="button" value="Save Author" class="insertAuthor">
  136. </label>
  137. </td>
  138. </tr>
  139. </form>
  140.  
  141. <tr>
  142. <td>Select Author </td>
  143. <td><select name="select2" size="6" multiple="multiple" id="select">
  144. <?php
  145. while($row_author = mysql_fetch_array($result_author)){
  146. ?>
  147. <option value="<?php echo $row_author['author_id']; ?>" ><?php echo $row_author['first_name'] . " , " . $row_author['last_name']; ?></option>
  148. <?php } ?>
  149. </select>
  150. </td>
  151. </tr>
  152. <tr>
  153. <td>Book Edition </td>
  154. <td><label>
  155. <input name="bkEdition" type="text" id="bkEdition" size="10" value="<?php echo $_POST['bkEdition']; ?>" />
  156. </label></td>
  157. </tr>
  158.  
  159. <tr>
  160. <td colspan="2"><label>
  161. <input name="cmdRegister" type="submit" id="cmdRegister" value="Register Book" />
  162. </label></td>
  163. </tr>
  164. </table>
  165. </form>
  166. </body>
  167. </html>
Last edited by tanha; Jul 30th, 2009 at 3:41 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC