How do i display records of an item into textbox based on drop down list

Reply

Join Date: Jul 2009
Posts: 15
Reputation: karin21 is an unknown quantity at this point 
Solved Threads: 0
karin21 karin21 is offline Offline
Newbie Poster

How do i display records of an item into textbox based on drop down list

 
0
  #1
Jul 5th, 2009
hi, i have a script here which display an item name from database into textfield based on the combo box selection, i cn display the item name but my problem is i dont know how to display the item information into the textbox? i'm required to use php but i think it can handle by javascript, hope my simple explanation works n_n

ex. item color
apple red
car blue






  1. <script type="text/javascript" language="javascript">
  2.  
  3.  
  4.  
  5. function populate(oSelect) {
  6.  
  7. var opt, opt2, a=0, i = 0, textarea = document.getElementById('notes');
  8.  
  9. while (opt = oSelect.options[i++])
  10.  
  11. if (opt.selected && textarea.value.indexOf(opt.value) == -1)
  12.  
  13. textarea.value += opt.value + '\n';
  14.  
  15.  
  16.  
  17. return true;
  18.  
  19. }
  20.  
  21.  
  22. </script>
  23.  
  24.  
  25.  
  26. <body>
  27.  
  28.  
  29. <?php
  30.  
  31. $db_host = 'localhost';
  32. $db_user = 'root';
  33. $db_pass = '';
  34. $db_db = 'dbname';
  35.  
  36. $db_link = mysql_connect($db_host, $db_user, $db_pass) or die('MySQL Connection Error:'.mysql_error());
  37. mysql_select_db($db_db) or die('MySQL Error: Cannot select table');
  38.  
  39.  
  40.  
  41. $sql = "SELECT id,item,colorinfo FROM table ORDER by id";
  42. $result = mysql_query($sql);
  43.  
  44.  
  45.  
  46. echo "<form name=f1 method=post action='' onSubmit='return false;'>";
  47. echo "<select name=m1 size='8' onDblClick='populate(this)'>";
  48. while ($row=mysql_fetch_assoc($result)) {
  49. $id=$row['id'];
  50. $display_name=$row['item'];
  51. $display_color=$row['color'];
  52. echo "<option value='$display_name'>$display_name</option>";
  53.  
  54. }
  55.  
  56.  
  57. echo "</select>";
  58. echo "<br>";
  59.  
  60. echo "<th>Color:<input type='text' name='color' id='color'>";
  61. echo "</form>";
  62. echo "</td>";
  63. echo "<td valign='top' align='left'>";
  64. echo "<form name=f2 method=post action='' onSubmit='return false;' >";
  65. echo "<p align='left'>";
  66.  
  67.  
  68. echo "</form>";
  69.  
  70. ?>
  71.  
  72. </table>
  73. <form>
  74. <TEXTAREA class=formfield2 name="notes" id="notes" rows=12 cols=16 wrap="virtual"></TEXTAREA>
  75. <input type="button" value="clear" onClick="if (confirm('Clear the field?'))notes.value='',">
  76. </form>
  77. </body>
  78. </html>
Last edited by peter_budo; Jul 6th, 2009 at 5:15 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,434
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 438
adatapost's Avatar
adatapost adatapost is offline Offline
Nearly a Posting Maven

Re: How do i display records of an item into textbox based on drop down list

 
0
  #2
Jul 6th, 2009
Your thread is belongs to PHP forum.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 15
Reputation: karin21 is an unknown quantity at this point 
Solved Threads: 0
karin21 karin21 is offline Offline
Newbie Poster

Re: How do i display records of an item into textbox based on drop down list

 
0
  #3
Jul 22nd, 2009
i think this is a PHP forum
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 876
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 116
ardav's Avatar
ardav ardav is offline Offline
Practically a Posting Shark

Re: How do i display records of an item into textbox based on drop down list

 
0
  #4
Jul 22nd, 2009
You will definitely need a server-side language to get info from a database. pHp with MySQL is great. If you have a dropdown (select) you can use AJAX to retrieve info into a textarea.

I suggest that you use something like the Prototype library to deal with the ajax implementation.

1. Download this from http://www.prototypejs.org (currently version 1.6).
2. Place this file into a folder, e.g. "/js/"
3. Create a new js file, e.g. custom.js and place it in the same folder.
4. Link the two js files to the php file's head area.
5. Create the form select widget via php or use static html.

e.g. for php:

  1. <select name="m1" id="m1" onchange="popMe();return false">
  2. <?php
  3. $q = "SELECT * FROM table...";
  4. $r = mysql_query($q);
  5. while($d = mysql_fetch_array($r)){
  6. output .= "\n\t<option id=\"{d['id']}\">{$d['type']}</option>";
  7. }
  8. echo $output;
  9. ?>
  10. </select>
  11.  
  12. ...
  13.  
  14. <textarea name="notes" id="notes"></textarea>
6. Write the popMe() function in your custom.js file:

  1. function popMe(){
  2. var op = $F('m1');
  3. var url = "/includes/getrecs.php";
  4. var param = "id=" + op;
  5. var oGetInfo = new Ajax.Updater("notes", url,{method: 'post',parameters: param});
  6. }
7. Write the php code for getting the data and call the file getrecs.php.

  1. ...DB connection details...
  2.  
  3. $id = $_POST['id'];
  4. $q = "SELECT info FROM table WHERE id='{$id}'";
  5. $r = mysql_query($r);
  6. $d = mysql_fetch_array($r);
  7. $info = stripslashes($d['info']);
  8. echo $info;
That's it. I haven't checked the code, it's off the top of my head, so there may be typos or something, but it's pretty close.
PM disabled - use the forum.
Don't start a thread if you don't have the good grace to respond to posts or if you can't find the SOLVED link.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 21
Reputation: spthorn is an unknown quantity at this point 
Solved Threads: 5
spthorn's Avatar
spthorn spthorn is offline Offline
Newbie Poster

Re: How do i display records of an item into textbox based on drop down list

 
0
  #5
Jul 22nd, 2009
Karin,

I see a couple problems.

First, you're not accessing the right column name for the color. The SELECT statement says it's 'colorinfo', but when you access it, it's as 'color'. Change the assignment line to:
$display_color=$row['colorinfo']; and the following line to
echo "<option value='$display_color'>$display_name</option>";
In that JavaScript function, use the following line to get both the color and name into the textarea:
textarea.value += opt.innerHTML + ' ' + opt.value + '\n';
-steve
Last edited by peter_budo; Jul 22nd, 2009 at 3:02 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 15
Reputation: karin21 is an unknown quantity at this point 
Solved Threads: 0
karin21 karin21 is offline Offline
Newbie Poster

Re: How do i display records of an item into textbox based on drop down list

 
0
  #6
Jul 22nd, 2009
ok mr.ardav and mr.steve thanks for the help i will try your solutions at home
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC