php and ajax dynamically created select box onchange event

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Leena P
    New Member
    • Mar 2007
    • 11

    #16
    i writing a code to create select box

    [CODE=javascript]function getSelectBoxDOM (options,nm) {
    selectBox =document.creat eElement('selec t');
    for (x=0; x < options.length; x++) {
    optionItem =document.creat eElement('optio n');
    optionItem.appe ndChild(documen t.createTextNod e(options[x]));
    selectBox.appen dChild(optionIt em);
    }
    selectBox.setAt tribute("name", nm);
    selectBox.setAt tribute("id",nm );
    s=new function(){}
    selectBox.oncha nge =new function() {
    showmcode(this. options[this.selectedIn dex].value);
    }
    return selectBox;
    }[/CODE]

    which is called here

    [CODE=javascript] function create_object(o bjType,nm){
    if(objType == 'select'){
    return getSelectBoxDOM (select_opt,nm) ;
    //select_opt.onch ange = function() {
    // showmcode(this. options[this.selectedIn dex].value);
    // }
    }

    obj = document.create Element("input" );
    obj.setAttribut e("type",objTyp e);
    obj.setAttribut e("name",nm);
    obj.setAttribut e("id",nm);

    return obj;
    }[/CODE]

    and when i'm adding rows the function create_object is called

    [CODE=javascript]function add_rows(obj){

    //tabObj = document.create Element("table" );
    //tbodyObj = document.create Element("tbody" );

    tabCols = new init_table();
    trObj = document.create Element("tr")
    col_names = new Array("chk","m_ cde","m_name"," um","qty","over ages");
    for(i=0;i<tabCo ls.length;i++){
    tdObj = document.create Element("td");
    tdObj.appendChi ld(create_objec t(tabCols[i],col_names[i]+'['+j+']'));\
    tdObj.appendChi ld(op.value);
    trObj.appendChi ld(tdObj);
    }
    obj.firstChild. appendChild(trO bj);
    j++;
    }[/CODE]
    and in test.php i'm assigning values to options
    so when the form is loaded select options are filled with database values



    [PHP]<?
    $sqlmaterial="s elect * from materialmaster order by m_cde";
    $db_query_mater ial=$DB_site->query($sqlmate rial);
    while($rsmateri al=$DB_site->fetch_array($d b_query_materia l)){
    $option_vals[]=$rsmaterial['m_cde']."-".$rsmateri al['m_name'];
    }
    ?>
    <script>
    select_opt = new Array();
    <?
    for($i=0;$i<cou nt($option_vals );$i++){
    ?>
    select_opt[<?= $i ?>] = '<?= $option_vals[$i] ?>';
    <?
    }
    ?>[/PHP]



    now how and where to writea code to give onchange event to the select box
    i tried create_object function where objtype="select " and where getselectbox function is called but same error.
    i also tried in addrow function just after creating object ..
    i'm new to ajax and dom
    so please tell me how do do this
    i have a static row where the same m_cde select box is there where this function is working prooerly
    but when i do it for dynamic rows if i alert then it shows [object]
    and with static rows it shows the allthe object created with id and all
    also how do i pass the m_cde as arry to the url inthe showmcode function for rows .

    thanks

    Comment

    • Leena P
      New Member
      • Mar 2007
      • 11

      #17
      but i'm assigning values to select box in the main program test.php from the database to the javascript array variable
      and creating the select boxes in the test.js
      so is it because of that it is creating problem?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #18
        Change line 5 in getSelectBoxDOM to:
        [CODE=javascript]optionItem.text = options[x];
        optionItem.valu e = options[x];[/CODE] You could use setAttribute if you want instead, e.g.[CODE=javascript]optionItem.setA ttribute("value ",options[x]);[/CODE]

        Comment

        • Leena P
          New Member
          • Mar 2007
          • 11

          #19
          Thanks
          my onchange event function works in select option
          but then it gives me error in mozilla

          Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_H IERARCHY_REQUES T_ERR)"

          this is in the add_rows function
          when i try to append

          [CODE=javascript]
          tdObj.appendChi ld(create_objec t(tabCols[i],col_names[i]+'['+j+']'));
          trObj.appendChi ld(tdObj); }
          obj.firstChild. appendChild(trO bj); //this line the above error occurs
          [/CODE]
          am i not setting the attributes in order the way it should for the objects created
          and no rows are added dynamically
          but in IE 6 it works and even the new rows are inserted
          but the value of m_name is not assigned to the corresponding text box in then newly created row like it is shown in static row
          how do i pass it in the test1.js in the statechaged1 corresponding to that row cell created
          Last edited by gits; Sep 9 '07, 10:20 AM. Reason: fix code tags

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5388

            #20
            hi ...

            please have a look at the correct usage of the code-tags:

            &#91;CODE=javas cript] code goes here [/CODE]

            kind regards

            Comment

            • Leena P
              New Member
              • Mar 2007
              • 11

              #21
              [QUOTE=Leena P]i want to basically take some information for the product and let the user
              enter the the material required to make this product

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #22
                Originally posted by Leena P
                Thanks
                my onchange event function works in select option
                but then it gives me error in mozilla

                Error: uncaught exception: [Exception... "Node cannot be inserted at the specified point in the hierarchy" code: "3" nsresult: "0x80530003 (NS_ERROR_DOM_H IERARCHY_REQUES T_ERR)"

                this is in the add_rows function
                when i try to append

                [CODE=javascript]
                tdObj.appendChi ld(create_objec t(tabCols[i],col_names[i]+'['+j+']'));
                trObj.appendChi ld(tdObj); }
                obj.firstChild. appendChild(trO bj); //this line the above error occurs
                [/CODE]
                You're trying to append to a whitespace node. Don't use firstChild. Use a tbody and append to that instead.

                Comment

                Working...