Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2009, 04:58 PM   PM User | #1
1andyw
Regular Coder

 
Join Date: Jul 2006
Posts: 171
Thanks: 13
Thanked 1 Time in 1 Post
1andyw is an unknown quantity at this point
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
1andyw is offline   Reply With Quote
Old 02-24-2009, 06:34 PM   PM User | #2
harbingerOTV
Senior Coder

 
Join Date: Jan 2005
Location: Memphis, TN
Posts: 1,765
Thanks: 8
Thanked 123 Times in 121 Posts
harbingerOTV will become famous soon enough
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
harbingerOTV is offline   Reply With Quote
Users who have thanked harbingerOTV for this post:
Eldarrion (02-24-2009)
Old 02-24-2009, 07:38 PM   PM User | #3
1andyw
Regular Coder

 
Join Date: Jul 2006
Posts: 171
Thanks: 13
Thanked 1 Time in 1 Post
1andyw is an unknown quantity at this point
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:

Quote:
<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
1andyw is offline   Reply With Quote
Old 02-24-2009, 09:05 PM   PM User | #4
Eldarrion
Regular Coder

 
Join Date: Feb 2009
Location: Wheeling, IL
Posts: 358
Thanks: 5
Thanked 62 Times in 60 Posts
Eldarrion is on a distinguished road
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..
Eldarrion is offline   Reply With Quote
Old 02-24-2009, 11:25 PM   PM User | #5
1andyw
Regular Coder

 
Join Date: Jul 2006
Posts: 171
Thanks: 13
Thanked 1 Time in 1 Post
1andyw is an unknown quantity at this point
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
1andyw is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:50 PM.


Advertisement
Log in to turn off these ads.