Type=button onClick method

Discussion in 'JavaScript' started by lost, Dec 13, 2005.

  1. #1
    i have a button created in my form (type=button) and on the onClick event, I need to execute some php code.
    I don't want to create a submit button instead because its seems to reset one of my selection boxes.
    From what i understand, i think you can only call a javascript method on the onClick method. If this is true, how would i go about doing this?
     
    lost, Dec 13, 2005 IP
  2. mnemtsas

    mnemtsas Super Dud

    Messages:
    497
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could do it with AJAX or with a POST only.
     
    mnemtsas, Dec 13, 2005 IP
  3. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    could you give me more details please??
     
    lost, Dec 14, 2005 IP
  4. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The submit button will reset your select box as the page will reload.

    You can add some code to select the select box with its previous value as follows:
    
    <SELECT name="value" size="1" >			
    <OPTION <?php if($value=="0"){ echo "selected"; } ?>>0</OPTION>	
    <OPTION <?php if($value=="1"){ echo "selected"; } ?>>1</OPTION>	
    <OPTION <?php if($value=="2"){ echo "selected"; } ?>>2</OPTION>	
    <OPTION <?php if($value=="3"){ echo "selected"; } ?>>3</OPTION>
    </SELECT>
    
    PHP:
     
    dave487, Dec 14, 2005 IP
  5. lost

    lost Peon

    Messages:
    144
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yah i realized that and i've done that php stuff you just sent me dave. however, my second selection box is a little more complex...do you think you could take a look and see how i could insert that code or fix my problem.

    Below are my 2 selection boxes:

    
    
    <SELECT NAME="firstChoice" ID="firstChoice" ONCHANGE="selectChange(this, genericform.secondChoice, arrItems1, arrItemsGrp1);"> 
    <OPTION VALUE="Select Unit" selected <?php if(@strcmp($_POST['firstChoice'], 'Select Unit')  == 0) echo "SELECTED";?>> Select Unit  
    <OPTION VALUE="SDC1-2" <?php if(@strcmp($_POST['firstChoice'], 'SDC1-2') == 0) echo "SELECTED";?>> SDC1-2        
    <OPTION VALUE="P73 Kit" <?php if(@strcmp($_POST['firstChoice'], 'P73 Kit') == 0) echo "SELECTED";?>> P73 Kit  </OPTION>
    </SELECT>
    <SELECT NAME="secondChoice" ID="secondChoice" ONCHANGE=showLoad();></SELECT>
    
    
    HTML:
    Here is the javascript function selectChange():

    
    
    <SCRIPT LANGUAGE="Javascript" TYPE="text/javascript">
    
       var arrItems1 = new Array();
       var arrItemsGrp1 = new Array();
       
       arrItems1[0] = "304315-701";
       arrItemsGrp1[0] = 'SDC1-2';
       arrItems1[1] = "304315-702";
       arrItemsGrp1[1] = 'SDC1-2';
       arrItems1[2] = "304315-703";
       arrItemsGrp1[2] = 'SDC1-2';
       arrItems1[3] = "304315-704";
       arrItemsGrp1[3] = 'SDC1-2';
       arrItems1[4] = "304315-705";
       arrItemsGrp1[4] = 'SDC1-2';
       arrItems1[5] = "304315-706";
       arrItemsGrp1[5] = 'SDC1-2';
    
       arrItems1[6] = "304316-701";
       arrItemsGrp1[6] = 'P73 Kit';
       arrItems1[7] = "304316-702";
       arrItemsGrp1[7] = 'P73 Kit';
       arrItems1[8] = "304316-703";
       arrItemsGrp1[8] = 'P73 Kit';
       arrItems1[9] = "304316-704";
       arrItemsGrp1[9] = 'P73 Kit';
    
       function selectChange(control, controlToPopulate, ItemArray, GroupArray) 
       {
          var myEle;
          var x;
          // Empty the second drop down box of any choices
          for (var q = controlToPopulate.options.length; q >= 0; q--) 
          {
              controlToPopulate.options[q] = null;
          }
          // ADD Default Choice - in case there are no values
          myEle   = document.createElement("option");
          theText = document.createTextNode("Select Part #");
          myEle.appendChild(theText);
          myEle.setAttribute("value", "0");
          controlToPopulate.appendChild(myEle);
          // Now loop through the array of individual items 
          // Any containing the same child id are added to
          // the second dropdown box
          for ( x = 0; x < ItemArray.length; x++ ) 
          {
             if ( GroupArray[x] == control.value ) 
             {
                myEle = document.createElement("option");
                myEle.setAttribute(ItemArray[x], x);
                var txt = document.createTextNode(ItemArray[x]);
                myEle.appendChild(txt);
                controlToPopulate.appendChild(myEle);
             }  
          }
      }
    </SCRIPT>
    
    Code (markup):
     
    lost, Dec 14, 2005 IP