Send JS form to a PHP

Asked

Viewed 479 times

1

Currently I developed a form in HTML and Validei the same with JS, now I want to send this data to a MYSQL database, but I am using in the ACTION form the address of a page in PHP, which contains the whole structure to insert in the database.

Now my doubt and how to integrate these languages and whether it is correct what I did ?

  • You want to send the data the traditional way (that reloads the page) or by ajax?

  • I want to send in the traditional way, but my PHP page has a paragraph, just so I know if it was sent to the bank or not, nor do I use ajax.

1 answer

1


It is. Through the attribute action of a form you define what will be the URL to which the form data will be sent. That is, when the form is submitted, the browser takes care of generating another HTTP request for the URL at action, using the method defined in method; if method="GET", a GET request will be made and, if method="POST", a POST request will be made. As the goal is to send information to a resource on the server, it makes more sense to use the POST method.

<form action="cadastrar.php" method="POST">
    <input type="text" name="name">
    <button type="submit">Cadastrar</button>
</form>

For the example above, by pressing the button Register, the form will be submitted, thus generating a POST request to cadastrar.php, running it. In PHP code, the value entered in the field will be available in superglobal $_POST:

$name = $_POST["name"];

If you’re doing something like this, yes, you’re doing it right.

  • I am using exactly this concept, calling in the action my php page that will send the data to the database, but when I click on the SEND button nothing happens on the page, I checked in the browser console mode the NETWORK and not even make the request for PHP.

  • @Diogodgo then your JS code that performs the validation must be blocking the form submission.

  • Is there any condition in JS that could override this Submit, basic example you would have ?

  • @Diogodgo, yes, um return false, one event.preventDefault(), etc. It would be better if you edit the question and insert the code you have so far.

  • I have on my page that Event.preventDefault(), I will work on a way not to use it. Thanks for the help, if I find difficulty in this aspect, I will open another question, my own doubt was whether I was using the correct concepts.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.