-1

Not really a coding problem just a pointer in the right direction would be amazing (please be patient with me, i'm still new to coding and this may seem like a really obvious answer to some).

I know that I need to learn how to use JQuery and AJAX to display data, however nearly all tutorials I've seen online don't interact with a MySQL database. Instead the tutor is interacting with a XML data sheet with all their data in it. Of course looking at JavaScript with fresh eyes has really confused me on what i need to focus to learn next. I know JQuery is used to present data and AJAX is used to interact with data. So do I need to convert database results, acquired in PHP, into XML documents to then be manipulated through AJAX and Jquery?

I want to know this because i'm trying to create a reactive drop down list for a PC-part picker. i.e. Selecting a CPU from a drop down list, then once the CPU is selected, when it comes to selecting a motherboard, a database query is used to find motherboards compatible with that CPU for example.... So far all of my data on CPU's, Motherboards and GPU's are all in my MySQL database hence why i'm asking this question.

Again any help to point me in the right direction would be fantastic and greatly appreciated.

  • 2
    Your question is too broad. The short broad answer to your broad question is, nothing about interacting with a database through ajax inherently demands that you use XML. – Taplar May 25 '18 at 15:47
  • It is quite normal to convert the data you want to transfer to and from the server using AJAX into JSON see https://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it and PHP provides 2 handy function to do all the heavy lifting http://php.net/manual/en/book.json.php – RiggsFolly May 25 '18 at 15:49
  • See I didn't know that data isn't required to be converted to XML purely because i haven't seen any example of AJAX being able to directly communicate with MySQL. So AJAX can indeed interact with a MySQL database? – HarrisonWD May 25 '18 at 15:50
  • No, in your case you would write a PHP script on the server to interact with the MySQL DB. The AJAX call is told to run the PHP and expect JSON back as an answer – RiggsFolly May 25 '18 at 15:51
  • Ajax doesn't normally communicate directly with a database. Ajax is executed from the client, which is received by the server, which then will perform operations, potentially with a database. The results of that, the server will then return as a response to the client. – Taplar May 25 '18 at 15:51
  • So i need to use AJAX to be the request handler. PHP code to convert a database result array into JSON format to be displayed by JQuery?? You see my confusion? xD @RiggsFolly – HarrisonWD May 25 '18 at 15:57

1 Answers1

1

I believe you need some clarification, from the front end (javascript, jQuery Ajax, etc...) you don't interact with the DataBase directly, you use server-side scripts, could be PHP, JavaScript, Java, C++, or any language on the server side, with it you query the database... from the front end you call this scripts and receive from them a response that you display, this response is returned in different formats that you decide when codding the server side script or that you could get from the documantation of an API if is the case.

[front end]--Request-->[server]
     [server script]--Query-->[DataBase]
     [server script]<--Response--[DataBase]
[front end]<--Response--[server]

The response could be XML, string, an object, json, even HTML code, that is up to the server side script, whatever you code in it!

This also may help Ajax

Hope this help you get started.

DIEGO CARRASCAL
  • 1,971
  • 13
  • 16