I'm pretty new to AJAX so I'm wondering what is the most efficient way to get data from the backend database to the Javascript code.
For example, for a project I'm working on, I need to fill a menu with a list of names from the database, then populate other fields with the user's address, phone, etc. based on the name. But it doesn't look like Javascript can make direct database queries, so I copied the database to an XML file, and Javascript builds an array of objects from the XML. This works alright for my purposes because the database, and consequently these XML files, are read much more frequently than they're updated, but overall, it seems like a solution that doesn't scale well.
Another problem I've encountered involves asynchronously adding a row to the database and then performing a calculation (and retrieving the answer) on the new data. For instance, I'll add a new name, etc. to the database and then I need the auto-generated primary key id for the new name. The only way I can think of solving this problem (without a full page reload) is POST-ing the user's data to a script that looks like:
mysql_query("insert into users values id=null, name={$_POST['name'}, ...", $connection);
echo ('new id = ' . mysql_insert_id($connectio
n));
and then parsing the text content of the page PHP generates. Again, this doesn't seem like a very scalable solution.
So can anybody please tell me the "standard" way of connecting AJAX to the database? I figure that there has to be some intermediary code on the server side, but there should be a way to retrieve this data that doesn't involve writing all of it to a separate file, right?
Thanks.
Start Free Trial