Call php file without leaving the page with HTML

Asked

Viewed 2,057 times

2

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

Example

<a href="arquivo.php">

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

  • You know Ajax???

  • This is the problem, I saw some examples of AJAX but I did not find it very convenient, since I have little use for it in my application, until now it would only be this in the case. I have enough difficulty with scripts, I would like something to escape from AJAX, if it is really the only option I will try to implement it again.

  • You say in your question that a Reload is unwanted, ajax will solve your problem in relation to that. Stay calm regarding the difficulty, it is simpler than it seems that you can be sure.

  • If you want an example in ajax, I can give an answer with the use of it, I can also explain in a simple but understanding way and you will see that it is a very simple thing.

  • Remembering that php is server-side, this means it will run on the server and then come as a result on the page. I don’t know another simple way to do this execution if not with Ajax.

1 answer

2


In this case you can use requests Ajax, I believe that would be the only option for your case. To facilitate the work use the library jQuery. Assuming you’ve already included the library on your page, let’s take a few examples:

To run an external PHP code:

$.get( "test.php" );

If you need to pass some parameter to the script:

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

In case you need to pick up any returns:

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

the jQuery.get() or $.get() amounts to jQuery.ajax() or $.ajax() in terms of operation, however, it would be "but" simple to use. Documentation: https://api.jquery.com/jquery.get/

Beyond the $.get() you can choose to $.ajax() as listed above, if you need more resources. Documentation: https://api.jquery.com/jQuery.ajax/

Browser other questions tagged

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