| | |
How do i display records of an item into textbox based on drop down list
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jul 2009
Posts: 15
Reputation:
Solved Threads: 0
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
ex. item color
apple red
car blue
php Syntax (Toggle Plain Text)
<script type="text/javascript" language="javascript"> function populate(oSelect) { var opt, opt2, a=0, i = 0, textarea = document.getElementById('notes'); while (opt = oSelect.options[i++]) if (opt.selected && textarea.value.indexOf(opt.value) == -1) textarea.value += opt.value + '\n'; return true; } </script> <body> <?php $db_host = 'localhost'; $db_user = 'root'; $db_pass = ''; $db_db = 'dbname'; $db_link = mysql_connect($db_host, $db_user, $db_pass) or die('MySQL Connection Error:'.mysql_error()); mysql_select_db($db_db) or die('MySQL Error: Cannot select table'); $sql = "SELECT id,item,colorinfo FROM table ORDER by id"; $result = mysql_query($sql); echo "<form name=f1 method=post action='' onSubmit='return false;'>"; echo "<select name=m1 size='8' onDblClick='populate(this)'>"; while ($row=mysql_fetch_assoc($result)) { $id=$row['id']; $display_name=$row['item']; $display_color=$row['color']; echo "<option value='$display_name'>$display_name</option>"; } echo "</select>"; echo "<br>"; echo "<th>Color:<input type='text' name='color' id='color'>"; echo "</form>"; echo "</td>"; echo "<td valign='top' align='left'>"; echo "<form name=f2 method=post action='' onSubmit='return false;' >"; echo "<p align='left'>"; echo "</form>"; ?> </table> <form> <TEXTAREA class=formfield2 name="notes" id="notes" rows=12 cols=16 wrap="virtual"></TEXTAREA> <input type="button" value="clear" onClick="if (confirm('Clear the field?'))notes.value='',"> </form> </body> </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.
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:
6. Write the popMe() function in your custom.js file:
7. Write the php code for getting the data and call the file getrecs.php.
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.
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)
<select name="m1" id="m1" onchange="popMe();return false"> <?php $q = "SELECT * FROM table..."; $r = mysql_query($q); while($d = mysql_fetch_array($r)){ output .= "\n\t<option id=\"{d['id']}\">{$d['type']}</option>"; } echo $output; ?> </select> ... <textarea name="notes" id="notes"></textarea>
PHP Syntax (Toggle Plain Text)
function popMe(){ var op = $F('m1'); var url = "/includes/getrecs.php"; var param = "id=" + op; var oGetInfo = new Ajax.Updater("notes", url,{method: 'post',parameters: param}); }
PHP Syntax (Toggle Plain Text)
...DB connection details... $id = $_POST['id']; $q = "SELECT info FROM table WHERE id='{$id}'"; $r = mysql_query($r); $d = mysql_fetch_array($r); $info = stripslashes($d['info']); echo $info;
Happy Humbugging Christmas
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:
In that JavaScript function, use the following line to get both the color and name into the textarea:
-steve
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 toecho "<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.
![]() |
Similar Threads
- display records in textbox using combo box (PHP)
- Trouble retrieving value from drop down list (C#)
- drop down list (PHP)
- Displaying image based on condition two drop down list (PHP)
- How to display value of selected item(dropdown list) on textbox (JavaScript / DHTML / AJAX)
- A Picture drop down list ? (JSP)
- display details from drop down list (JavaScript / DHTML / AJAX)
- please help me in drop down list (PHP)
- Populating Multiple Text Fields Based On A Dynamic Drop-Down List Selection (PHP)
- Passing a drop down list item's value (HTML and CSS)
Other Threads in the PHP Forum
- Previous Thread: Logout after time
- Next Thread: reformatting MySQL date (yyyy-mm-dd)
| Thread Tools | Search this Thread |
.htaccess ajax apache api array back basic beginner binary broken cakephp checkbox class cms code computing cron curl customizableitems database date delete display dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql syntax system table thesishelp trouble tutorial update upload url validation validator variable video web xml youtube






