-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>
2
  • 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 (note: that answer uses Python, not PHP, so you'll need to do some translating) Commented Oct 30, 2022 at 4: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.) Commented Oct 30, 2022 at 4:25

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I am sorry, my server was forbidden. How to fix it?
Do you have a CORS-related problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.