0

Let's say a website needs to pull information from a specific table in a database based on a user's menu selection. That table's data is then fed into some JS equations and thrown onto the page.

What is the best way to go about pulling that table's information? I've read that trying to access an SQL database via JavaScript is bad practice, so is there another way to do this? I know about PHP's json_encode, but I guess I'm not entirely sure

  1. What the syntax is if I'm calling PHP from a JS script, and
  2. If that's 'best' practice. Still relatively new to this, so I'd like to do this right.

Another option as far as I'm concerned is attempting to pull ALL of the possible tables (not a security concern) at once on page load. I expect that'd introduce a good deal of latency, though.

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
  • Have you looked into [AJAX with PHP](http://www.w3schools.com/ajax/ajax_php.asp)? – Aruna Tebel Aug 16 '15 at 17:52
  • Not much, honestly. I've heard of AJAX and have read up just a *little* bit on it. From that link it definitely looks like it'll solve my issue. Is this the usual method websites use to do what I'm trying to do? –  Aug 16 '15 at 18:01
  • Yes, for this kind of a scenario, ajax with php, mysql will be the approach. You can find out an simple example (do not take this as a reference for ajax) [here](http://www.w3schools.com/php/php_ajax_database.asp) which uses all 3 stacks (PHP, MySQL, Javascript). – Aruna Tebel Aug 16 '15 at 18:09

1 Answers1

0

It looks to me that you are not really sure what technique to use. Here are some options. I'm not going to type them here because, there is enough to find about each one:

plain php: w3schools

pure ajax call: stackoverflow

jquery: jquery

Ajax calls are more user friendly and many times more efficient because, you don't have to refresh the page. I usually get all information at once( as long your mysql data is not to big). As for security: You use php either way so it doesn't matter if you use Ajax or not. Oh and don't select valuable data of users data (like password or their emails). I hope you get more overview after this :)

Community
  • 1
  • 1
Vince Verhoeven
  • 1,693
  • 14
  • 25
  • Definitely appreciate the information. I'm pretty sure the plain PHP way isn't what I'm looking for. I know how to pull the info from the database using just php, but I'm looking for ways to update information & such without having to reload the whole page. AJAX sounds like it'll do the trick nicely. –  Aug 16 '15 at 18:51