Three scripts BAD

Discussion in 'JavaScript' started by koolsamule, Jun 11, 2010.

  1. #1
    Hi Chaps,

    I had a two similar scripts that uses a select option to pass a parameter to a php page, then returns options/values to a seperate select drop down.

    Both of these scripts worked fine until I added another script. The new script is different, in that it checks the value of the original select option, then passes that value to a seperate php page, then, if a condition is met, an input field is displayed.

    This new script works, but has resulted in the two original scripts doubling the values that they return.

    I hope that makes sense, if anyone can spare anytime, I'll gladly post the code and any other information . . . .

    Cheers
     
    koolsamule, Jun 11, 2010 IP
  2. bbBoy

    bbBoy Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Word of advice, Post the code if you really want to receive some help!

    I'd gladly give you a few minutes, but I doubt you are even around now to reply.
     
    bbBoy, Jun 11, 2010 IP
  3. koolsamule

    koolsamule Peon

    Messages:
    101
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi, thanks for the reply . . .it's very late over here.
    Here is my codfe, I would greatly appreciate any help on this:
    OK,

    Here is the select, that triggers 3 scripts:
    <select id="customer" name="FK_cust_id" onchange="getCustContact(this)">
    <option value="">Select Customer</option>
                <?php
    do {  
    ?>
    	<option value="<?php echo $row_rsCustomer['cust_id']?>"><?php echo $row_rsCustomer['custname']?></option>
    <?php
    } while ($row_rsCustomer = mysql_fetch_assoc($rsCustomer));
      $rows = mysql_num_rows($rsCustomer);
      if($rows > 0) {
          mysql_data_seek($rsCustomer, 0);
    	  $row_rsCustomer = mysql_fetch_assoc($rsCustomer);
    	}
    ?>
    </select>
    PHP:
    As you can see, nothing wrong with this, the options are populated from a MySQL database.
    Here is the first script:
    <script type="text/javascript">
    var ajax_CustContact = new Array();
    function getCustContact(sel)
    {
    	var Customer = sel.options[sel.selectedIndex].value;
    	document.getElementById('custcontact').options.length = 0;
    	if(Customer.length>0){
    		var index = ajax_CustContact.length;
    		ajax_CustContact[index] = new sack();
    		
    		ajax_CustContact[index].requestFile = 'getCustomerContact.php?custid='+Customer;
    		ajax_CustContact[index].onCompletion = function(){ createCustContact(index) };
    		ajax_CustContact[index].runAJAX();
    	}
    }
    function createCustContact(index)
    {
    	var obj = document.getElementById('custcontact');
    	eval(ajax_CustContact[index].response);
    }
    </script>
    Code (markup):
    This returns all the contacts for the selected Customer.

    This is what the 'custcontact' select looks like:
    [​IMG]
    Which is what I'm after. There is a similar script that does exactly the same, but populates a seperate select, I'm excluding this of this example.
    Now, the problem I'm having is when I add this script:
    <script type="text/javascript">
                $(document).ready(function(){
                    $('#customer').change(function() {
                        var option = $(this).val();
                        $.get('getCostCentre.php', {select:option}, function(data) {
                            $('#result').html(data).hide().fadeIn(1000);
                        });
                    });
                });
    </script>
    Code (markup):
    This works, in that it does what I asked it to do, the 'getCostCentre.php' echo's an input if a particular customer is selected. However, it triggers a problem with the 'custcontact' select options, by repeating the list. Here is what it looks like after adding the above script:
    [​IMG]
    I have played around with different options but nothing I've tried seems to work.
    Any help would be most appreicated.
     
    koolsamule, Jun 11, 2010 IP