"...Remember being a youngster, mom or dad telling you "close the door"? Well the same applies to HTML ...."

Go Back   Webmaster Forums > Code Forum > Javascript Forum

Reply
 
Thread Tools Rate this Webmaster Discussion
Old 03-02-2010, 08:36 AM   #1
giza
New User
 
Join Date: Mar 2010
Webmaster Discussions: 1
Rep Power: 4
giza is on a distinguished road
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>


giza is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Rate This Thread
Rate This Thread:

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


Similar Webmaster Discussions
Thread Webmaster Discussion Starter Forum Replies Last Post
Populate multi text field and saving them in mysql Kid Javascript Forum 0 10-14-2009 12:12 AM
[Javascript] Populate a Text field via MYSQL Drop Down xChAMaRx Javascript Forum 11 10-11-2009 07:27 AM
Issue with Form Value only displaying initial # of a retrieved mysql text field. lbbeeson PHP Forum 6 07-20-2008 09:42 PM
Drop Down menu "other" *ree text option jvfleming HTML Forum 3 03-06-2004 08:31 AM
Dynamically populate a text field after making dropdown menu selection kperrin Website Scripts Forum 4 11-21-2003 11:12 AM


All times are GMT -5. The time now is 08:38 PM.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.