Jump to content

[SOLVED] Select box and JS


Minase

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

$desc = $item['description'];

 

thank you again ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.