Run PHP Function Inside Jquery Click

Run php function inside jQuery click

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.

JS:

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

test.php FILE:

 <?php mkdir('/test1/test2', 0777, true); ?>

Call PHP function from jQuery?

AJAX does the magic:

$(document).ready(function(

$.ajax({ url: 'script.php?argument=value&foo=bar' });

));

Execute PHP function with onclick

First, understand that you have three languages working together:

  • PHP: It only runs by the server and responds to requests like clicking on a link (GET) or submitting a form (POST).

  • HTML & JavaScript: It only runs in someone's browser (excluding NodeJS).

I'm assuming your file looks something like:

<!DOCTYPE HTML>
<html>
<?php
function runMyFunction() {
echo 'I just ran a php function';
}

if (isset($_GET['hello'])) {
runMyFunction();
}
?>

Hello there!
<a href='index.php?hello=true'>Run PHP Function</a>
</html>

Because PHP only responds to requests (GET, POST, PUT, PATCH, and DELETE via $_REQUEST), this is how you have to run a PHP function even though they're in the same file. This gives you a level of security, "Should I run this script for this user or not?".

If you don't want to refresh the page, you can make a request to PHP without refreshing via a method called Asynchronous JavaScript and XML (AJAX).

That is something you can look up on YouTube though. Just search "jquery ajax"

I recommend Laravel to anyone new to start off right: http://laravel.com/

calling php function from jquery?

From jQuery you can only call php script with this function. Like that:

$.ajax({
url: 'hello.php',
success: function (response) {//response is value returned from php (for your example it's "bye bye"
alert(response);
}
});

hello.php

<?php
echo "bye bye"
?>

Execute PHP script onclick with jQuery

.load() must be called on an element, like this:

$('#element').click(function() {
$('#element').load('.../script.php', function(e){
console.log(e);
});
});

Run php file through jquery link click

Rather than using inline javascript, try doing something like this.

Give the "Like" link a class, example:

<a href='#' class='likeLink'>Like</a>

An instead of your current section, use something like this:

<script>
$(function() {
$('.likeLink').click(function() {
$.get('upone.php', function(data) {
alert("Server Returned: " + data);
});
return false;
});
});
</script>

If the alert message returns what you expect from the PHP page, you can just comment it out for production.

By the way, I typed this out a long time ago today and forgot I was working on it. Sorry if it's already solved.



Related Topics



Leave a reply



Submit