Call php without leaving the page with HTML

2

I would like to make a button with the tag < a > from the html to call an external php code.

Example

<a href="arquivo.php">

In which .php file would be just a few commands, I'd just like to run them without actually opening a page, because when that occurs the only output I saw would be a refresh to another page, which in the case would do a reload unwanted.

    
asked by anonymous 19.10.2017 / 16:52

1 answer

2

In this case you can use Ajax requests , I believe that would be the only option for you. To make the work easier, use the jQuery library. Assuming you've already included the library on your page, here are some examples:

To execute an external PHP code:

$.get( "test.php" );

If you need to pass some parameter to the script:

$.get( "test.php", { name: "John", time: "2pm" } );

If you need to get some feedback:

$.get( "test.php", function( data ) {
  alert( "Retorno: " + data );
});

jQuery.get () or $. get () is equivalent to jQuery.ajax () or $ ajax () in terms of operation, however, would be "but" simple to use. Documentation: link

In addition to $. get () you can choose $ ajax () as listed above if you need more resources. Documentation: link

    
19.10.2017 / 17:24