Results 1 to 5 of 5

Thread: Javascript changing select drop menu

  1. #1
    Join Date
    Mar 2014

    Javascript changing select drop menu

    I have two select boxes they are drop downs I want the first select box too change the second one based on is selection. It works great but the problem is that the first drop down value that I assigned is not the value that I want.
    This is my table setup
    my first table name is "calltypecode."
    the fields for the this table is
    some example data
    description and priority
    description = fire one
    priority = regular
    description = fire two
    priority = silent

    my second table name is "prioritytype".
    my second table only has field priority
    priority = regular
    priority = silent

    They booth have something in commend but for the first drop down box I want the value to be description not the value priority I already have that value in the other drop down box.

    I pretty much give up. Does anyone now how to do what I want.

    This code works great but the value that I want is this variable to post $description
    this is my code for javascript
    window.onload = function() { BindEvent(); }
        function BindEvent()
            var elemToBind = document.getElementById ( "maintype" );
            elemToBind.onchange = function () { SetSel ( this ); }
        function SetSel(elem)
            var secondCombo = document.getElementById ( "priority" );
            secondCombo.value = elem.value;   
    this is the code for the first select box
    <select class="type" id="maintype"  name="maintype" selected="selected" onchange="showhide(this)">
    	<option value=0 selected="selected"></option>
    	while($fetchdrop = mysqli_fetch_array($executedrop))
    		$description = $fetchdrop['description'];
    		$priority = $fetchdrop['priority'];
    		 echo "<option id= 1 value='$priority'>". $description . "</option>";
    I am optaining information from a database.

    This is my other select drop down box
    <select class="priority" id="priority">
    	<option value=0></option>
    	include "connection.php";
    	$sqlpriority = mysqli_query($cnx, "SELECT * FROM prioritytype");
    	while($fetch2 = mysqli_fetch_array($sqlpriority))
    		$priority = $fetch2['priority'];
    		echo "<option value='$priority'>" . $priority . "</option>";

  2. #2
    Join Date
    May 2006
    Somewhere behind your screen
    why wouldn't you post the html code for your dropdowns instead of php which is absolute useless when someone tries to run the code locally? do you think we all have access to your db?

    looking at this code

    while($fetchdrop = mysqli_fetch_array($executedrop))
    		$description = $fetchdrop['description'];
    		$priority = $fetchdrop['priority'];
    		 echo "<option id= 1 value='$priority'>". $description . "</option>";
    i can predict that all of this dropdown options are going to have same id attribute values which is not correct because every id within the current HTML document must be unique ( http://www.w3schools.com/tags/att_global_id.asp /Definition and Usage/ )

    i'm not sure i understood your question correctly but try this code:

    <!doctype html>
    <meta charset="utf-8" />
    div > #db_maintype,#db_priority{margin-top:15px;margin-bottom:15px;text-align:center;}
    function $(id){return document.getElementById(id);}
    var val=this.value,
    return ($('db_maintype').value===$('db_priority').value) ? false : true;
    <form id="myform" name="myform" action="">
    <b>Maintype:</b><select id="maintype">
    <option value=""></option>
    <option value="regular">fire_one</option>
    <option value="silent">fire_two</option>
    <b>Priority:</b><select id="priority">
    <option value=""></option>
    <option value="regular">regular</option>
    <option value="silent">silent</option>
    <input type="text" id="db_maintype" name="db_maintype" size="70" placeholder="this field value will be sent to the DB as 'db_maintype', set this type 'hidden'" />
    <input type="text" id="db_priority" name="db_priority" size="70" placeholder="this field value will be sent to the DB as 'db_priority', set this type 'hidden'" />
    <input type="reset" value="Reset" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="Send" />
    signature under construction

  3. #3
    Join Date
    Mar 2014
    Hey I am sorry but I am a beginner at html and javascript as well php. I am doing this project at the same time I am learning this languages trying to combine them together. The one I am learning the most is javascript. As for php and html not some much I am learning. But I found my solution to this problem. What I did was they submitted that form it executed a javascript code this is the code that work for me. I created a function alert() in that function have this var x = document.getElementById("maintype").selectedIndex; var y = document.getElementsByTagName("option")[x].value = document.getElementsByTagName("option")[x].text; and on the button onclick="alert()" so when I submit the information javascript runs first then php at the end which gives me the end results. Insteda of giving me the value to post it gives me the text information. By the way do you know where are some tutorials that allow you to learn javascript like beginners intermidiate and advance concepts. So when I read the beginners and I am done I will go to intermidiate so on.

  4. #4
    Join Date
    May 2006
    Somewhere behind your screen
    as for me i don't know where to find such tutorials. i learn js right here trying to help people if i can and i know many high skilled coders over here (i'm just an amateur) learnt it by themselves aswell. may be somebody else can advice you where those tutorials can be found. i would advice you a good book which you can easily find in the internet (buy or download - it's your choise) - this is Secrets of the JavaScript Ninja
    signature under construction

  5. #5
    Join Date
    Mar 2014
    I just have one question Padonak.
    What does this mean.

    var y = document.getElementsByTagName("option")[x].value = document.getElementsByTagName("option")[x].text;
    I am interested in knowing what is this mean [x]. I understand the rest except for that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09998 seconds
  • Memory Usage 2,892KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (5)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (5)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (71):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates