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.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post

    Can't see my error (jQuery)

    Hi,

    Trying to populate a select statement with a jquery ajax call. The span element is populated if it outside of the form. When I copy and past the span element within the select statement, it doesn't produce the options. I can't figure out what I"m doing wrong. Would appreciate your help.

    This works:

    Code:
    <html>
    <head><title>This Title</title>
    <script src="jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $.post("getpage.php",function(data){
    $('#pagechoice').html(data);
    	});
    });
    </script>
    </head>
    <body>
    <h1>Subject is Page Choice</h1>
    <form name="page_select" id="pageselect" method="post" action="showpage.php">
    <select name="page" size="1" style="width: 30em">
    </select>   
    </form>
    <span id="pagechoice"></span>
    </body>
    </html>
    This doesn't work. The span has been moved up into the select statement:

    Code:
    <html>
    <head><title>This Title</title>
    <script src="jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    $.post("getpage.php",function(data){
    $('#pagechoice').html(data);
    	});
    });
    </script>
    </head>
    <body>
    <h1>Subject is Page Choice</h1>
    <form name="page_select" id="pageselect" method="post" action="showpage.php">
    <select name="page" size="1" style="width: 30em">
    <span id="pagechoice"></span>
    </select>   
    </form>
    </body>
    </html>
    This is the process page:

    Code:
    <?php
    include("conandy.php");
    error_reporting(E_ALL);
    if (!$conn)
      {
      die('Could not connect: ' . mysql_error());
      }
    $rs = mysql_select_db("andy",$conn);
    $sql = "SELECT * FROM  page LIMIT 250";
    if(!$sql){echo "Unable to select" . mysql_error();}
    $rs = mysql_query($sql,$conn);
    while($row=mysql_fetch_array($rs)){
    	echo("<option>"  . $row['title'] . "</option>");
    	}
    mysql_free_result($rs);		
    mysql_close($conn);
    ?>
    Thank you,

    Andy

  • #2
    Senior Coder
    Join Date
    Jan 2005
    Location
    Memphis, TN
    Posts
    1,785
    Thanks
    8
    Thanked 131 Times in 129 Posts
    okay so the process page is your getpage.php? When it displays into the span when not in the form, it looks something like so:
    Code:
    <option>stuff 1</option>
    <option>stuff 2</option>
    <option>stuff 3</option>
    correct?

    If that is the case it's probably because the span doesn't belong inside a select.

    maybe try this:
    Code:
    <script type="text/javascript">
    $(document).ready(function(){
    $.post("getpage.php",function(data){
    $('select#page').html(data);
    	});
    });
    </script>
    
    <select name="page" id="page" size="1" style="width: 30em">
    </select>
    Stop making things so hard on yourself.
    i is tugbucket :: help raise tugburg :: Whitehaven Kiwanis

  • Users who have thanked harbingerOTV for this post:

    Eldarrion (02-24-2009)

  • #3
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post
    With that, the options appear as a list above the form window for the seclect but not within the select, ie:

    News and Information
    Employment Opportunities
    Training Opportunities
    Warming Center

    When I open the Firebug consol, the response is:

    <option>News and Information</option><option>Employment Opportunities</option><option>Training Opportunities</option><option>Warming Center</option>
    Seems as though the ajax is doing it's stuff OK, but I'm going something wrong with the form part. I've done this a hundred times with just php/mysql but this has me stumped.

    Thanks for your interest.

    Andy

  • #4
    Regular Coder
    Join Date
    Feb 2009
    Location
    Wheeling, IL
    Posts
    358
    Thanks
    5
    Thanked 62 Times in 60 Posts
    What harbingerOTV posted seems to work okay on my side. Did you add an id "page" to the select and remove the span with the same id from the page? If you haven't, the php would just be spitting the code out inside the span, instead of putting it within the <select> tags. Hope this helps. If not... could you provide us with a link to have a look at or the way your code looks after the changes you made?
    Last edited by Eldarrion; 02-24-2009 at 09:42 PM.

  • #5
    Regular Coder
    Join Date
    Jul 2006
    Posts
    171
    Thanks
    13
    Thanked 1 Time in 1 Post
    My bad. harbingerOTV's solution is dead-on. I failed to read his code correctly at first. Now is see.

    Thank you, harbingerOTV

    Thank you, Eldarrion

    Andy


  •  

    Posting Permissions

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