Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Three scripts BAD

    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

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Don't ask to post the code - just post it. No one (aside from a clairvoyant) can help without seeing what you're talking about.
    Are you a Help Vampire?

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts
    OK,

    Here is the select, that triggers 3 scripts:
    PHP Code:
    <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($rsCustomer0);
          
    $row_rsCustomer mysql_fetch_assoc($rsCustomer);
        }
    ?>
    </select>
    As you can see, nothing wrong with this, the options are populated from a MySQL database.
    Here is the first script:
    Code:
    <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>
    This returns all the contacts for the selected Customer.

    This is what the 'custcontact' select looks like:

    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:
    Code:
    <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>
    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:

    I have played around with different options but nothing I've tried seems to work.
    Any help would be most appreicated.

  • #4
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    I wonder if the multiple change events on the same element (#customer) have confused it.

    What do getCustomerContact.php and getCostCentre.php return? Just <option> tags? Something else?
    Are you a Help Vampire?

  • #5
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Tom,

    getCustomerContact.php returns select options

    and getCostCentre.php returns a couple of table cells and an input field

    What do you suggest I try?

  • #6
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Without knowing what's going on, I would try a bit of re-arranging that change event to test whether the multiple event calls are part of the problem. That would involve:
    1) removing the onchange attribute from the select tag.
    2) putting that original function call into the jquery (before or after?) the ajax call there
    - should look something like: getCustContact($(this)), I think
    Are you a Help Vampire?

  • #7
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Dude,

    Thanks for the advice, will try it out on Monday. Will shout you then . . .

    Cheers

  • #8
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Dude,

    I removed the onChange attribute from the select, so now the jQuery call works but the other two scripts do not (as nothing is triggering them).
    I've tried to insert the original function into the jQuery call, but if I add it before or after the call, nothing works.
    This is what I have:
    Code:
    <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:
    <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>
    What do you suggest I try?

  • #9
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by koolsamule View Post
    Code:
    <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>
    There's no call to getCustContact() there. If nothing works when you put it in, debug.
    Are you a Help Vampire?

  • #10
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Tom,

    Yeah, that's the bit I don't understand. . .I'm a complete newbie with these things.

    I understood that the onChange() attribute on the select triggered the script, but now we have taken that attribute off, I don't know how to call the getCustContact() bit into the 'getCostCentre' script!

  • #11
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Again, try it in the jquery. It might look like this:
    Code:
               $(document).ready(function(){
                    $('#customer').change(function() {
                        getCustContact();
                        var option = $(this).val();
                        $.get('getCostCentre.php', {select:option}, function(data) {
                            $('#result').html(data).hide().fadeIn(1000);
                        });
                    });
                });
    Are you a Help Vampire?

  • #12
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts
    OK, thanks Tom, I've tried that, my script looks exactly like your latest code. Now nothing works, what else can I try?

  • #13
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Why doesn't it work? Debug. Do you have Firebug installed? If not, get it. Then see what error(s) it's reporting.
    Are you a Help Vampire?

  • Users who have thanked tomws for this post:

    koolsamule (06-14-2010)

  • #14
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Tom, I'm using IE8 not Firefox . . .so I guess it's IE Developer Tools? Haven't a clue what I'm supposed to be looking for!

  • #15
    New Coder
    Join Date
    Sep 2009
    Posts
    45
    Thanks
    4
    Thanked 0 Times in 0 Posts
    OK, checked out the IE DT, it looks like the getCustContact() is being called, but the error stops:
    var Customer = sel.options[sel.selectedIndex].value;
    document.getElementById('custcontact').options.length = 0;
    Error message states:
    'options' is null or not an object


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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