Can anybody provide me with an example of how to pop开发者_Python百科ulate a dropdown list from data in a sql server database. I have just an html page with no server-side anything...
You need to connect the client side with the data on the server, with a server side script, such as PHP. The following is an example:
your html page:
jQuery.getJSON('test.php',function(items){
jQuery.each(items,function(index,value){
jQuery('#selectid').append('<option>'+value+'</option>');
});
});
the test.php page:
<?php
// todo: you should be get the following array from the sql server
echo json_encode(array('aaa','bbb','ccc'));
?>
Here is on example btw
you should tell about the language
http://www.joe-stevens.com/2010/02/23/populate-a-select-dropdown-list-using-jquery-and-ajax/
You (normally) cannot connect to a Sql Server from an Internet browser directly (although it is possible, because SQL server can have an XML interface via IIS).
There are several reasons why this is not possible, or at least a very bad habit:
- The source code of your html page is visible. But you need a username and password to connect. This will make your username and password visible (to a slightly more advanced user).
- Connections via the Internet are slow, a connection is an "expensive" resource on a server. So you will be making a lot of expensive connections, that also take longer as a connection from server side script.
- Connections from the Internet should not be trusted
- You communicate to the server via SQL. If an end-user can see the SQL, you give him a lot of internal information, that can be used to hack your database. Specially in combination with the username/password.
So what you need to do, is put some code on the server, that connects to the database for your client (browser). This will hide sensitive information, and streamline the connections to the server.
精彩评论