-1

Please help me, I have a game on my website, but I don't know how to insert that poin into my database because I do not use a form.

I also want to ask, how to enter data without using form into the global variable $_POST? (You can use PHP or javascript for it)

<div class="container">
  <h2>
    Your point :
    <span class="poin">120</span> <!-- It will be inserted into the database -->
  </h2>
</div>
gunzxx
  • 3
  • 3
  • I'm not well-versed in PHP, but you'll want to take a look at AJAX. I wrote an answer for a similar problem using flask [here](https://stackoverflow.com/a/74230109/13376511) (note: that answer uses Python, not PHP, so you'll need to do some translating) – Michael M. Oct 30 '22 at 04:23
  • at least you should think how should the system trigger an insert (or update) of the data ? (on change of the value by some means ? on click of a button ? etc.) – Ken Lee Oct 30 '22 at 04:25

1 Answers1

1

Without a form, you can send a POST request to your server by using the fetch API.

Just add the following JavaScript code:

const poin = document.querySelector('.container .point').textContent
;(async () => {
  await fetch('host:port', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
    body: JSON.stringify({ point })
  })
})()

And replace 'host:port' to the correct host and port (of your server).

With document.querySelector('.container .point') you are selecting the DOM-element which CSS-selector is '.point'. fetch accepts the following parameters: url and [options]. The [options] (which is optional) is a object with the following properties: method, headers and body, among others.