Send data from Javascript to PHP and then use Phpmyadmin

0

I am in a project in which I use javascript to take information from some forms and I would like to be able to work with a database through phpmyadmin but as I understand I must first pass all the data I have taken through javascript to php There my doubt.

What would be the easiest way to pass a data obtained from an HTML form by javascript to PHP to communicate with phpmyadmin there?

For example, a user tries to log in through a login form, that data is received by javascript, javascript is passed to Php and this communicates with the database that gives us an OK or a KO according to the result of the query.

I feel if I have explained myself wrongly. Thanks in advance.

    
asked by JavierD 16.05.2018 в 10:10
source

2 answers

0

As they say above, you must use ajax.

Here I leave a link where I explained at the time how it works.

link

Greetings

    
answered by 16.05.2018 / 10:35
source
1

Here is an example of the ajax function, adapted to your question. The PHP part is simply manage since you receive that data, make the connection with the database and according to the data returned, the php always ends with a echo of the result in json format (to make it easier to manage, although you can simply return "OK" or "KO") with json_encode(respuesta,JSON_FORCE_OBJECT);

$("form").submit(function(evt){
  evt.preventDefault();
  $.ajax({
  //la url en la que esté tu php
  url  :"url/compurebaLogin.php",
  //(aquí GET o POST)
  method :  "POST",
  data : {
    "nombreUsuario": $("#usr").val(),
    "pass" : $("#pass").val()
    }
  }).done(function(response){
    //Aquí te devuelve el OK o KO dependiendo de lo que hagas en PHP
    //En php recibes en este caso concreto $_POST['nombreUsuario'] y $_POST['pass']
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="text" id="usr"/>
<input tpe="password" id="pass"/>
<input type="submit"/>
</form>
    
answered by 18.05.2018 в 13:53