Php and Ajax

I am currently working on a project and I need to implement. And I have been trying to implement Ajax crud operations but it is not working
Can I please get a link to where I can understand Ajax better from scratch thank you…

Hi genmax100ab welcome to the forum

AJAX - XHR is a way for script to make Http Requests.

To be able to write script that will “work”, you need to have at least a basic understanding of the Hyper Text Transfer Protocol

I think this is the more definitive reference

https://www.w3.org/Protocols/

But this should be good enough and easier to digest

When the AJAX is correct, the sever-side script needs to be able to take what it is given, do what needs to be done, and return an appropriate response.

Hi @genmax100ab, just a thing to note since you posted this as a PHP question… the backend part really works the same whether the request is asynchronous or not. What makes AJAX different is that the request gets sent by some JS when the page is already loaded; a most basic example would be something like

hello.php

<?php

echo 'Hello world!';

JS on page

const xhr = new XMLHttpRequest()

xhr.addEventListener('load', function onload () {
  console.log(this.responseText) // Logs "Hello world!"
})

xhr.open('GET', 'hello.php')
xhr.send()

The key element here is the XMLHttpRequest instance for asynchronous client/server interaction. (Since ES6 there’s also the promise-based fetch API, but you’ll need a polyfill for a certain browser.) There are of course more advanced topics such as serializing form data, monitoring progress etc., but AJAX itself is basically just JS sending an XHR and waiting for a response. :-)

PS: Here’s another “Getting started” article on the MDN:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.