Run php function inside jQuery click

Asked
Active3 hr before
Viewed126 times

6 Answers

insidefunction
90%

You cannot run PHP code inside a jquery function. PHP runs on the server-side whereas jquery/javascript runs on the client-side. However, you can request a PHP page using jquery and with the PHP code on that page will run the mkdir that you want.,Why not to call an ajax function on the server side? You could do something like :,How do i run a PHP function inside jQuery click event. I have the following which is not correct. when the user clicks on a button, i want a new directly created.,Just do an ajax request and then execute the PHP server side:

JS:

$.ajax({
   url: 'test.php',
   success: function(data) {
      alert('Directory created');
   }
});

test.php FILE:

 < ? php mkdir('/test1/test2', 0777, true); ? >
load more v
88%

Execute PHP Function With onclick,Use jQuery to Execute the PHP Function With the onclick() Event,Use Plain JavaScript to Execute the PHP Function With the onclick() Event,Use the GET Method and the isset() Function to Execute a PHP Function From a Link

Example Code:

#php 7. x
   <
   ? php

function php_func() {
   echo " Have a great day";
}
php_func(); ?
>
load more v
72%

How to call PHP function on the click of a Button ?,Calling a PHP function using the HTML button: Create an HTML form document which contains the HTML button. When the button is clicked the method POST is called. The POST method describes how to send data to the server. After clicking the button, the array_key_exists() function called.,How to delete an array element based on key in PHP?,Little o and little omega notations

65%

UPDATE: the html and PHP code are in the same PHP file,In javascript, make an ajax function,,The response from print_r is then returned and displayed with ,The += populates and adds to existing html elements, removing the + just updates and replaces the existing contents of the html p element "results".

PHP:

function removeday() {
   ...
}

HTML:

<a href="" onclick="removeday()" class="deletebtn">Delete</a>
load more v
75%

I am searching for a simple solution to call a PHP function only when a-tag is clicked., javascript - How to set the onclick property in IE8 using functions containing values? , When to execute JavaScript functions , javascript - Phantomjs does not execute page.evaluate Functions in functions

PHP:

function removeday() {
   ...
}
load more v
40%

Keep in mind that the PHP code will still run on the server itself. We will just provide it with data from within our script.,Now, let's see an example of a basic AJAX request where we will pass data to a PHP file and call the PHP function wordwrap() within that file. Here is our complete webpage:,If you are using jQuery on your website, it becomes incredibly easy to call any PHP file with code that you want to run.,There can be a lot of other cases where you might want to run some PHP code inside JavaScript—for example, to save some data on your server. Simply placing the PHP code inside JavaScript will not work in this case either.

Now, let's see an example of a basic AJAX request where we will pass data to a PHP file and call the PHP function wordwrap() within that file. Here is our complete webpage:

<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>PHP Function in JavaScript Demo</title>
<style>
body {
    font-family: 'Lato';
    font-weight: 400;
    font-size: 1.4rem;
}

p {
    text-align: center;
	margin-bottom: 4rem;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<p class="unbroken">The United States (U.S. or US)—officially the United States of America (USA), commonly known as America—is a country primarily located in North America, consisting of 50 states, a federal district, five major self-governing territories, 326 reservations, and various possessions. At 3.8 million square miles (9.8 million square kilometers), it is the world's third- or fourth-largest country by total area. With a population of more than 328 million people, it is the third most populous country in the world. The national capital is Washington, D.C., and the most populous city is New York City.</p>
<p class="broken"></p>
<script>
$.ajax({
  method: "POST",
  url: "wrap.php",
  data: { text: $("p.unbroken").text() }
})
  .done(function( response ) {
    $("p.broken").html(response);
  });
</script>
</body>
</html>
load more v

Other "inside-function" queries related to "Run php function inside jQuery click"