Minase Posted September 3, 2008 Share Posted September 3, 2008 hy there,i did made a select box which retrive data from a mysql DB and i want when i select a item from there (just click on it) information to be updated into the textbox im prety new to JS so any help will be apreciated. thanks Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 4, 2008 Share Posted September 4, 2008 Well, it would really help if you gave us an idea of the data you are using. But, here's one possibility" JS Code <script type="text/javascript"> function setVal(fieldID, inputValue) { document.getElementById(fieldID).value = inputValue; } </script> HTML Code <select name="thelist" onchange="setVal('thetext', this.value);"> <option value="One">1</option> <option value="Two">2</option> <option value="Three">3</option> </select> <br /> <input type="text" name="thetext" id="thetext" /> Quote Link to comment Share on other sites More sharing options...
Minase Posted September 4, 2008 Author Share Posted September 4, 2008 let me explain better please i do fetch all results from a DB i do have a select list and a textarea the select list is in left and textarea in right in the textarea should be echoed variable $description when i click on a item from select list like in select list i have countrys if i select Romania,then in the textarea it will apear a description,if i select another country,it change. thank you for your time Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 4, 2008 Share Posted September 4, 2008 And your point is??? The code I provided will do what you want. You just need to create the select list accordingly when you do the db query. Here is a slightly modified version with the fields as you described: <html> <head> <script type="text/javascript"> function setVal(fieldID, inputValue) { document.getElementById(fieldID).value = inputValue; } </script> </head> <body> Country: <select name="thelist" onchange="setVal('thetext', this.value);"> <option value=""><--Select a Country--></option> <option value="Description of Romainia">Romania</option> <option value="Description of United States">United States</option> <option value="Description of Canada">Canada</option> </select> <br /> Description: <input type="text" name="thetext" id="thetext" /> </body> </html> Quote Link to comment Share on other sites More sharing options...
Minase Posted September 4, 2008 Author Share Posted September 4, 2008 thank you very much,sorry but im kinda new to it Quote Link to comment Share on other sites More sharing options...
Minase Posted September 4, 2008 Author Share Posted September 4, 2008 cant edit the last post but i do have a question "again" how i can put in the description a value from database? here is my current code <? error_reporting(E_ALL ^ E_WARNING); require_once ( 'settings.php' ); $count = $db->RecordCount("SELECT ID FROM Items"); $query = mysql_query("SELECT * FROM Items"); while ($item = mysql_fetch_array($query)) { $options .= "<option value='{$item['ID']}'>{$item['Name']}</option>"; $desc = $item['Name']; } ?> <html> <head> <script type="text/javascript"> function setVal(fieldID, inputValue) { document.getElementById(fieldID).value = inputValue; } </script> </head> <select name="items" size="50" class="input" onchange="setVal('desc', this.value);"> <?=$options;?> </select> <input type="text" name="desc" id="desc" /> </html> how can i put $item['Name'] php variable to be shown instead of selected value thank you very much Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 4, 2008 Share Posted September 4, 2008 OK, now I'm confused. You are using $item['Name'] as the "text" for the option and $item['ID'] as the "value" of the option. And you state you want to have $item['Name'] shown in the description field? If the user can see the 'Name" as the selected option in the select list, why is there a need to populate the same text into the description field? You could do that by changing the function as follows: function setVal(fieldID, inputObj) { selText = inputObj.options[inputObj.selectedIndex].text; document.getElementById(fieldID).value = selText; } And then changing the onchange call to this onchange="setVal('desc', this);" Is there, possibly, a description field in the database that is not included in the above code? That would change things. Quote Link to comment Share on other sites More sharing options...
Minase Posted September 4, 2008 Author Share Posted September 4, 2008 lol normal but if i could do that with showing name,i could easily change it to my own needs the real thing is way bigger the above code work,but i cant figure out how to put the description from another variable i did ask how i can put name there,just to get a basic ideea,but i got what i asked lol $desc = $item['description']; thank you again Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 4, 2008 Share Posted September 4, 2008 OK, one laast try. I think this is what you want: <?php error_reporting(E_ALL ^ E_WARNING); require_once ( 'settings.php' ); $count = $db->RecordCount("SELECT ID FROM Items"); $query = mysql_query("SELECT * FROM Items"); while ($item = mysql_fetch_array($query)) { $options .= "<option value='{$item['ID']}'>{$item['Name']}</option>"; $jsDescrAry .= " dscrList[{$item['ID']}] = '{$item['DEscription']}';\n"; } ?> <html> <head> <script type="text/javascript"> dscrList = new Array(); <?php echo $jsDescrAry; ?> function setVal(fieldID, inputID) { document.getElementById(fieldID).value = (dscrList[inputID] || ''); } </script> </head> <body> Country: <select name="thelist" onchange="setVal('thetext', this.value);"> <option value=""><--Select a Country--></option> <?php echo $options; ?> </select> <br /> Description: <input type="text" name="thetext" id="thetext" /> </body> </html> Also, it is bad practice to use short tags, i.e. <? as it is not supported on all server by default and can be a problem if you ever nee to move to another server. Quote Link to comment Share on other sites More sharing options...
Minase Posted September 5, 2008 Author Share Posted September 5, 2008 thank you very much for your reply,dont worry about PHP,at that part im not a beginer,but not a pro.i do know about short tags and others,but i do have my own boxes. here is what problem i have now ... weird enough it doesnt work how it should... everything from PHP is good,but it seem a JS problem again.. sorry for beign bothersome $jsDescrAry .= " dscrList[{$item['ID']}] = '{$item['ID']}';\n"; // this work normally it echo item ID into the box but ... $jsDescrAry .= " dscrList[{$item['ID']}] = '{$item['Description']}';\n"; // or another column,tryed with name also,but it work just with $item['ID'] everything else is ignored.... thank you very much Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 5, 2008 Share Posted September 5, 2008 Have you looked a the HTML that was produced when you used $item['Description']? Are you getting any errors? Is there is a single quote mark in the data which is breaking the javascript (would just need to escape them through PHP)? Please provide some details. Quote Link to comment Share on other sites More sharing options...
Minase Posted September 5, 2008 Author Share Posted September 5, 2008 everything is 100 % perfect,but i wonder why JS doesnt echo it ??? i did look .. no problem with echoing .... Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 5, 2008 Share Posted September 5, 2008 I can't help without something to go on. Please post: 1) The enitre javascript output that is written to the HTML page - especially the array. 2) The Select and text field from the HTML Be sure to provide the rendered HTML not the PHP code. Quote Link to comment Share on other sites More sharing options...
Minase Posted September 5, 2008 Author Share Posted September 5, 2008 //edit nvm i did found... my items name had " ' " that too,and caused the script to work. thank you very much for your help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.