onclick funktion是如何工作的,如何获取javascript的php信息,如何获取php的javascript信息以及如何使用ajax js? [重复]

This question already has an answer here:

I try to use an onclick function. If the user clicks the html button a js function should be done. For this function I need some variables of the php code. How do I get them? If I get them I have to change them. So how do I got them (the changed variables) to php? I thought that ajax could perhaps help me. But how do I use ajax?

</div>

Using a library like Axios or jQuery might help, compared to using Javascript's fetch function. Here's how you might achieve that using jQuery:

HTML

<button onClick="handleClick()">Click me<button>

Javascript

function handleClick() {
  console.log('Clicked the button')
    $.get('path/to/script.php', function (data) {
    // It helps if the data returned in the PHP script is JSON formatted
    console.log(data)
  });
}