1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

java onclick not work in IE

Discussion in 'HTML & JavaScript' started by ija61, Aug 20, 2012.

  1. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:
    Hy.

    I got in to a problem and I will appreciate your help.

    Here is my code:

    Code:
    echo "<option value='".$product_id."' onclick='recp(".$product_id.")' selected >".$product_name."</option>";
    And here is the java:
    Code:
    <script>
    function recp(id) {
    $('#showpacakge').load('getpackage.php?id=' + id);
    }
    </script>
    The idea is that when the visitor select an option to generate another dropdown list auto generated in php from a script located in getpackage.php without refreshing the page.

    The function work great in Mozilla but on IE doesn't work.

    Any suggestion?

    Thank you
     
  2. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    if you're using jquery, this is not the way to go about doing this. inline events are a very old and bad way to handle this.

    You have the product id, already in <option value="x" />

    You'd be better off doing this:

    http://api.jquery.com/change/
     
  3. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:
    I saw that method but since is my first time I use java it is impossible to write the function, I try to rewrite an existing one but no success.

    If I provide you all the code could you write one please..

    Thank you.
     
  4. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    http://jsfiddle.net/chovy/MpNND/
     
    • Thanks Thanks x 1
  5. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:
    Thank you very much but I still can not make it work.

    So tell me what I'm doing wrong:

    register.php

    Code:
    <select name="product">
                            <?php                        
                            $sql = "SELECT * FROM proxy_products";
                            $result = mysql_query($sql) or die('Error, query failed');
                            echo "<option value=''></option>";
                            while(list($product_id,$product_name) = mysql_fetch_array($result))
                                {
                                    if($product_name==$_POST['product'])
                                    {
                                        echo "<option value='".$product_id."' >".$product_name."</option>";
                                    }
                                    else
                                    {
                                        echo "<option value='".$product_id."' >".$product_name."</option>";
                                    }
                                }
                            ?>                    
                    </select>
    Here is where the second list should appear:
    Code:
    <td>
                        <select id="showpacakge" name="package">
                                            
                        </select>
    </td>
    Here is your function (thank you rep is coming :) ):

    Code:
    <script>
    $('select').change(function(e){
        var $targ = $(e.target);
        var selected_value = $targ.find('option:selected').val();
        
        $("#showpacakge").html(selected_value);
        
        
        $('#showpacakge').load('getpackage.php?id=' + selected_value);
        
    });
    </script>
    What I'm doing wrong?

    and here is the getpackage.php

    Code:
    //make the connection to the database
    $connection = @mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
    $db = @mysql_select_db($db_name,$connection)or die(mysql_error());
    
    $id = $_GET['id'];
    $sql = "SELECT package_id, package_name FROM proxy_package WHERE product_id='".$id."'";
    $result = mysql_query($sql) or die('Error, query failed');
    echo "<option value=''></option>";
        while(list($package_id,$package_name) = mysql_fetch_array($result))
        {
            echo "<option value='".$package_id."' >".$package_name."</option>";
        }    
    
     
    Last edited: Aug 21, 2012
  6. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    for starters your list is empty. you have no options in the output. your sql query needs work. I can't help you there.

    Also, remove this line, it was just a demo:

    Code:
        $("#showpacakge").html(selected_value);
    it's not going to work until you get options in your markup.
     
  7. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:
    The sql query us ok, I just test it.

    the option line is generated by the getpackage.php here:

    Code:
    while(list($package_id,$package_name) = mysql_fetch_array($result))     
    { 
            echo "<option value='".$package_id."' >".$package_name."</option>";     
    }
    The idea is that when I select a value from the first list that value will be send to getpackage.php and the script construct the html code for the second list and that should be loaded in the main page.

    What I can not understand from your function is where jv know where to take the value, from witch list?

    Thank you.
     
  8. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    you example code has an empty select menu. Can you paste the real html for it?
     
  9. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:

    This is what it should appear

    HTML:
    <select id='swhopackage' name='package'>
    <option value=''></option>
    <option value='6' >PProxy-1</option>
    <option value='7' >PProxy-16</option>
    <option value='8' >PProxy-32</option>
    <option value='9' >PProxy-64</option>
    </select>

    this is what I have on the main page
    HTML:
    <select id='swhopackage' name='package'>
    <!-- here the java function should add the constructed html code from getpackage.php-->
    </select>
    Html code constructed based on the sql query result that need a value from the first list.
     
  10. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    ok, i misunderstood. I thought you had a select menu w/ options and you wanted to fetch the url while passing the selected option value. It makes no sense that you pass an id to the .load() function, you don't have product ids yet.
     
  11. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:
    The idea is this.

    I have 4 product category and each product category have 5 packages. I want that when a visitor select a product category in the first list they will have in the second list only the packages from the selected category.

    At this moment I know all the products id and all packages id but in the future they will be more and it will be more simple to add them in a sql and load them from there then adding a new drop down list every time I get new category and new packages.
     
  12. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    ok, so you have two lists. one for product categories, they pick one, then it populates the second list with product ids for that category?
     
  13. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:
    Yes.. And all are retrieve from sql database.
     
  14. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    give this a shot. http://jsfiddle.net/chovy/MpNND/2/
     
  15. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:
    Is not working.. now it also stop working even in Mozilla.

    Shouldn't be a event triggering like "onclick" or "onchange" to be plased on the <option> tag of the first list?

    Just asking, as I said in java I'm still a baby:)
     
  16. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    http://utuxia.com/test/
     
  17. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    Replace:
    HTML:
    <script>
    function recp(id) {
    $('#showpacakge').load('getpackage.php?id=' + id);
    }
    </script>
    With:
    HTML:
    <script>
    function recp(id) {
      var theOptions = "";
      $.ajax({
       url: 'getpackage.php?id=' + id,
        success: function(data) {
          theOptions = data;
        }
      });
      $('#showpacakge').append(theOptions);
    }
    </script>
    Let me know how it goes.
     
    Last edited: Aug 21, 2012
  18. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:

    No, it does not working. It is also stop working in mozzila.

    Here is my loading library

    Code:
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    Maybe this could be a a problem.

    The strange thing is that my previous function work ok on mozzila but didn't work on IE and chrome maybe I do something wrong beside the javascript..

    Thank you and hope to find a solution soon since it get me almost 24h on this... with no sleep
     
    Last edited: Aug 21, 2012
  19. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    did you try my last one? I pretty much spelled it out for you...
     
  20. ija61

    ija61 Senior Member

    Joined:
    Mar 2, 2011
    Messages:
    960
    Likes Received:
    634
    Gender:
    Male
    Occupation:
    The first SEO economist:)
    Location:
    Romania
    Home Page:
    Yes... and still not working for what I notice the java script is not running.

    I say that because beside that I test the getpackage.php and it work.

    I also set the $id var in getpackage.php so no matter what the script should print the html code and if I access the file directly on the browser it is ok and I get the right result. If I try to load in the main script with java then nothing happen no matter a I try it on mozzila, ie, or chrome.