Current location: Hot Scripts Forums » General Web Coding » JavaScript » Fill a Text Field from a Drop-Down Box


Fill a Text Field from a Drop-Down Box

Reply
  #1 (permalink)  
Old 03-02-10, 10:07 AM
giza65 giza65 is offline
New Member
 
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Fill a Text Field from a Drop-Down Box

Hi All,

Being a newbie I've found all the threads on this forum about this topic extremely useful. So, i've cobbled together the attached javascript/html/php/mysql code that uses a first selection box to populate a second selection box that, in turn, populates a text box. i.e. the user selects either bead or utensil that populates the second selection box with the relevant items (either beads or utensils). I link to a database. When the user selects an item in the second drop-down box, a price appears in the text field.

My problem is that I can only seem to get the prices for the first selection, i.e. beads. When I select utensils from the drop-down box I get the same set of prices (i.e. the bead prices) instead of the utensil prices. I would be very grateful for some help on this! Here is my code:

Code:
<hmtl>
<?php
require_once("/..../...php");
?>

<form name="form1"> 
    <select name="types" onChange="fillItems(0);"> 
        <option value="1">Beads</option> 
        <option value="2">Utensils</option> 
    </select> 
    <select name="items" onChange="fillPrices(0);"></select>
    <input type="text" name="prices" value= " " />

</form> 
 
<script type="text/javascript">

var arItems = new Array() 
arItems = [
<?php 

$query = "SELECT item_type, stock_id, model_name, price FROM stock"; 
$result = mysqli_query($dbc, $query); 
$num_rows = mysqli_num_rows( $result ); 
$i = 1; 
while ($row_result = mysqli_fetch_row($result)) { 
    echo "['".$row_result[0]."', '".$row_result[1]."', '".$row_result[2]."', '".$row_result[3]."']"; 
    if ( $i < $num_rows ) 
        echo ", 
        "; 
    $i++;  
} 
?> 
]


function fillItems(int_Start) {
    var fTypes = document.form1.types;
    var fItems = document.form1.items;
    var a = arItems;
    var b, c, d, intItem, intType, intPrice; 

    if ( int_Start >= 0 ) {
	
        for ( b = 0; b < a.length; b++ ) {
            if ( a[b][1] == int_Start )
                intType = a[b][0];
        }
   
        for ( c = 0; c < fTypes.length; c++ ) {
            if ( fTypes.options[ c ].value == intType )
                fTypes.selectedIndex = c;
        }
    }

    if ( intType == null )
        intType = fTypes.options[ fTypes.selectedIndex ].value;

    fItems.options.length = 0;

    for ( d = 0; d < a.length; d++ ) {
        if ( a[d][0] == intType )
            fItems.options[ fItems.options.length ] = new Option( a[d][2], a[d][1] );

        if ( a[d][1] == int_Start )
            fItems.selectedIndex = fItems.options.length - 1; 
        }
    }

function fillPrices() {
    var fItems = document.form1.items;
    var fPrices = document.form1.prices;
    fPrices.length = fItems.options.length;    
    var a = arItems;
    var x = fItems.selectedIndex;

		fPrices = a[x][3];
   		document.form1.prices.value = fPrices;
 
}

</script>

</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-02-10, 03:06 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
It would be easier if you posted the 'View source' version of the code, so the result of the PHP execution could be seen.

One of the easiest ways to debug javascript is to use Firebug, a Firefox plugin. You can also use alert statements, with both Firefox and IE.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-02-10, 04:01 PM
giza65 giza65 is offline
New Member
 
Join Date: Mar 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Wirehopper! Here is the 'View Source" version. The names don't make sense I know, just a jumble of test characters at this stage for the "model_name". So basically you have from left to right in each row: item_type, stock_id, model_name, price. I'm using firebug to debug. It's a great tool. It's not showing any errors that I can see though. I see some faulty logic on my part now though. I'm using one array arItems to group the beads and the utensils together. I guess that needs to be split into two arrays to be able to access the relevant price data in each group, right...?

Code:
<hmtl>
Connection to database established

<form name="form1"> 
    <select name="types" onChange="fillItems(0);"> 
        <option value="1">Beads</option> 
        <option value="2">Utensils</option> 
    </select> 
    <select name="items" onChange="fillPrices(0);"></select>
	<input type="text" name="prices" value= " " />
</form> 
 
<script type="text/javascript">

var arItems = new Array() 
arItems = [
['1', '7', 'happy happy', '52.00'], 
        ['1', '8', 'dignbat dong', '23.00'], 
        ['1', '9', 'zongea', '412.00'], 
        ['1', '10', 'asegaai', '474.00'], 
        ['1', '11', 'anckor', '789.00'], 
        ['1', '12', 'zongalero', '12.00'], 
        ['1', '13', 'wwww', '145.00'], 
        ['1', '14', 'ert', '145.00'], 
        ['1', '15', 'werefasdvd', '7895.00'], 
        ['1', '16', 'def', '557.00'], 
        ['1', '21', 'wicker', '147.00'], 
        ['1', '22', 'ertyh', '415.00'], 
        ['1', '23', 'wereerrr', '789.00'], 
        ['1', '24', 'eertt', '789.00'], 
        ['1', '25', 'ser', '789.00'], 
        ['1', '27', 'k5s754', '717.00'], 
        ['1', '28', 'rerv', '1415.00'], 
        ['1', '29', 'cewr', '586.00'], 
        ['1', '30', 'ewqexweqe', '147.00'], 
        ['1', '31', 'dsdsdsadsa', '14.00'], 
        ['1', '32', 'retqret', '417.00'], 
        ['1', '33', 'r23ec21e', '78.00'], 
        ['1', '34', 'rqewrr', '478.00'], 
        ['1', '35', 'wrwrfewfewtg', '417.00'], 
        ['1', '36', 'rewreewrewqr', '145.00'], 
        ['1', '37', 'wqrrewtrtr', '1756.00'], 
        ['1', '38', 'tretergregtrgert', '474.00'], 
        ['1', '39', 'fwrewrer', '4554.00'], 
        ['1', '40', 'rwer', '17.00'], 
        ['1', '41', 'retewttwertre', '744.00'], 
        ['2', '42', 'erewrerewrewrewr', '47458.00'], 
        ['2', '43', 'wrewqrewrqw', '7474.00'], 
        ['2', '44', 'weqtertryterytrwy', '174.00'], 
        ['2', '45', 'terwtretrewt', '747.00'], 
        ['2', '46', 'rerrwttretrwetqew', '444.00'], 
        ['2', '47', 'ewwrtretreyrewy', '445.00'], 
        ['2', '48', 'erewr', '7.00'], 
        ['2', '49', 'aswww', '445.00'], 
        ['2', '50', 'vee', '145.00'], 
        ['2', '51', 'dsaDSAdas', '145.00'], 
        ['2', '52', 'qwww', '78.00'], 
        ['2', '53', 'aassdd', '415.00'], 
        ['2', '54', 'glass bead2', '1455.00'], 
        ['2', '55', 'bowl', '145.00'], 
        ['2', '56', 'saa', '145.00'], 
        ['2', '57', 'sddd', '145.00'], 
        ['2', '58', 'sSEE', '4458.00'], 
        ['2', '59', 'happy happy2', '145.00'], 
        ['2', '60', 'happy happy22432', '145.00'], 
        ['2', '61', '', '345.00'], 
        ['2', '62', 'happy king', '334.00'], 
        ['2', '63', 'dqwddqg', '145.00'], 
        ['2', '64', 'qdasDASCAsa', '145.00'] 
]


function fillItems(int_Start) {
    var fTypes = document.form1.types;
    var fItems = document.form1.items;


    var a = arItems;
    var b, c, d, intItem, intType, intPrice; 

    if ( int_Start >= 0 ) {
	
        for ( b = 0; b < a.length; b++ ) {
            if ( a[b][1] == int_Start )
                intType = a[b][0];
        }
   
        for ( c = 0; c < fTypes.length; c++ ) {
            if ( fTypes.options[ c ].value == intType )
                fTypes.selectedIndex = c;
        }
    }

    if ( intType == null )
        intType = fTypes.options[ fTypes.selectedIndex ].value;

    fItems.options.length = 0;

    
    for ( d = 0; d < a.length; d++ ) {
        if ( a[d][0] == intType )
            fItems.options[ fItems.options.length ] = new Option( a[d][2], a[d][1] );

        if ( a[d][1] == int_Start )
            fItems.selectedIndex = fItems.options.length - 1;
            
    }

    }

function fillPrices() {
    var fItems = document.form1.items;
    var fPrices = document.form1.prices;
	fPrices.length = fItems.options.length;    
    var a = arItems;
    var x = fItems.selectedIndex;

		fPrices = a[x][3];
   		document.form1.prices.value = fPrices;

}

</script>

</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-02-10, 06:28 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Quote:
I guess that needs to be split into two arrays to be able to access the relevant price data in each group, right...?
That will make it much easier.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
text box with scroll bar silvermane CSS 7 01-16-09 04:03 AM
div css theighost CSS 11 09-14-08 03:30 AM
Draggable Tables Ares JavaScript 10 08-03-06 07:55 AM
Need Your HelP! Loading Multiple External Text into Multiple Dynamic Text Fields Flash_Boi Flash & ActionScript 2 03-30-06 04:27 PM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 08:19 PM


All times are GMT -5. The time now is 08:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.