943,546 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 2091
  • PHP RSS
Jul 5th, 2009
0

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

Expand Post »
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






php Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karin21 is offline Offline
15 posts
since Jul 2009
Jul 6th, 2009
0

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

Your thread is belongs to PHP forum.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 22nd, 2009
0

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

i think this is a PHP forum
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karin21 is offline Offline
15 posts
since Jul 2009
Jul 22nd, 2009
0

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

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:

PHP Syntax (Toggle Plain Text)
  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:

PHP Syntax (Toggle Plain Text)
  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.

PHP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1046
Solved Threads: 942
Sarcastic Poster
ardav is offline Offline
6,661 posts
since Oct 2006
Jul 22nd, 2009
0

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

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.
Reputation Points: 10
Solved Threads: 5
Newbie Poster
spthorn is offline Offline
21 posts
since Sep 2007
Jul 22nd, 2009
0

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

ok mr.ardav and mr.steve thanks for the help i will try your solutions at home
Reputation Points: 10
Solved Threads: 0
Newbie Poster
karin21 is offline Offline
15 posts
since Jul 2009

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: Logout after time
Next Thread in PHP Forum Timeline: reformatting MySQL date (yyyy-mm-dd)





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


Follow us on Twitter


© 2011 DaniWeb® LLC