The next question is there is a page on HTML in it there are fields for login and password for connecting to the database. How to transfer the entered data to the script?

  • 2
    Send form or Ajax - Kostiantyn Okhotnyk
  • Can you give a link to the ajax topic? - N. Turshiev
  • Script on the client or server side? If on the client side, hang the onsubmit event handler on the form. If on the server - as I wrote above @KonstantinOkhotnick (sending the form will result in the display of a new page returned by the server in response, and AJAX is used when it is necessary to modify the page without reloading it). - ߊߚߤߘ

1 answer 1

You can transfer using superglobal arrays $_GET and $_POST . The transfer method can be set in the form itself <form action="/index.php" method="post"> where action="/index.php" is let to the file that receives data from the form. In the file ( index.php for example) there will be the following code.

Form html for $_POST :

 <form action='/index.php' method='post'> <input type="text" name="name"> <input type="password" name="password"> <input type="submit" name="save" > </form> 

PHP code index.php :

 <?php if(isset($_POST['password']) && isset($_POST['name'])){ echo $_POST['password'].'<br>'; echo $_POST['name']; } 

If one file, then you can write as follows (file index.php ):

 <?php if(isset($_POST['password']) && isset($_POST['name'])){ echo $_POST['password'].'<br>'; echo $_POST['name']; } ?> <html> <head> </head> <body> <form action='/index.php' method='post'> <input type="text" name="name"> <input type="password" name="password"> <input type="submit" name="save" > </form> </body> </html> 

Yes. it is also possible via AJAX , sending JSON .

Here is a working example of ajax implementation using jquery index.php file:

 <?php function is_ajax() { return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; } if (is_ajax() && isset($_POST['password']) && isset($_POST['name'])) { echo json_encode(array( 'ok' => 'AJAX OK!!!' )); exit(); } ?> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> </head> <body> <form id="sub" action='/index.php' method='post'> <input type="text" name="name"> <input type="password" name="password"> <input type="submit" name="save" > </form> <script> $("document").ready(function(){ $("#sub").submit(function(event){ event.preventDefault(); var data = $(this).serialize(); $.ajax({ type: "POST", dataType: "json", url: "index.php", data: data, success: function(r) { console.log(r); } }); return false; }); }); </script> </body> </html> 

Using echo json_encode(array('ok' => 'AJAX OK!!!')); we send the answer to our browser and in the developer panel we can see the answer.

enter image description here

It should be noted that passwords and important data are usually passed by the post method.

  • And tell me, did you write above that all this can be done through Jquery? - N. Turshiev
  • Yes, code is written on jquery to call ajax and send it to json using the post or get methods - fonjeekay
  • And could you give a link to an example? I was very grateful. - N. Turshiev
  • w3schools.com/Tags/ref_httpmethods.asp get and post are http methods, they are used in all programming languages ​​for the web where there is a server and a website or service for transmitting data via http . The difference between AJAX and regular sending is that AJAX works without reloading the page. You can use AJAX on both jQuery and regular javascript. - fonjeekay
  • Added a working example of ajax with jquery - fonjeekay