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

<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>

Recommended Answers

All 5 Replies

Your thread is belongs to PHP forum.

i think this is a PHP forum

Member Avatar for diafol

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:

<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>

6. Write the popMe() function in your custom.js file:

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});
}

7. Write the php code for getting the data and call the file getrecs.php.

...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;

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.

Member Avatar for spthorn

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

ok mr.ardav and mr.steve thanks for the help i will try your solutions at home

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.