Reputation: 10256
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.
$('button').click(function(){
<?php mkdir('/test1/test2', 0777, true); ?>
return false;
})
Upvotes: 11
Views: 65337
Reputation: 1462
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); ?>
Upvotes: 27
Reputation: 1375
Buddy , first of all understand that jquery is a client side programming language which is running on clients browse and Php runs on webserver .Copy the php code in to a php file and create a request to the php file from jquery using
$.ajax();
Upvotes: -1
Reputation: 1412
Why not to call an ajax function on the server side? You could do something like :
$('button').click(function()
{
$.ajax
({
type: "POST",
url: "some.php",
data: "val1:value&lvaln:valn",
success: function(msg)
{
alert( "Data Saved: " + msg );
}
});
return false;
});
Documentation here
Try not to embed php code into html. If you can avoid this practice, the better.
Hope it helps
Upvotes: 2
Reputation: 10674
You can greatly benefit from a framework supporting AJAX.
As author of Agile Toolkit I can recommend you to try it, the code would look like this:
$button=$page->add('Button');
if($button->setLabel('Create Directory')->isClicked()){
if(mkdir('/tmp/123', 0777, true)){
$button->js()->univ()->alert('Directory Created'); // executes JS code
}else{
$button->js()->univ()->alert('Problem'); // executes JS code
}
}
Other frameworks may offer other ways to simplify AJAX and keep it in one file.
Upvotes: 0
Reputation: 1659
First of all you should understand how php works (no offense but this is essential). Why PHP script is not workig in a web browser? To accomplish what you need you have to use ajax (to request a script on the server using javascript)
PHP File (createdir.php):
<?php
mkdir('/test1/test2', 0777, true);
?>
JavaScript Code:
$('button').click(function() {
$.ajax({
url: 'createdir.php',
success: function(){
alert('dir created');
}
});
return false;
});
I have not validated if the code acually works. If you encounter any problems you should have a look at the jquery documentation (it's awsome :-) ) http://api.jquery.com/jQuery.ajax/
Upvotes: 9
Reputation: 9397
Just do an ajax request and then execute the PHP server side:
$('button').click(function(){
$.ajax({url: 'mkdir.php'});
return false;
})
and the php:
<?php mkdir('/test1/test2', 0777, true); ?>
That's all you need.
Upvotes: 3
Reputation: 1496
you have to do:
//mkdir.php
<?php mkdir('/test1/test2', 0777, true); ?>
<script>
$('button').click(function(){ $('#hidden').load('mkdir.php'); return false; })
</script>
<div id='hidden' style='display:none;'></div>
Upvotes: 1
Reputation: 9929
You are mixing up client side and server side code here. The PHP code is already executed on the server when the user clicks the button and therefore nothing will happen. You can use the xmlhttprequest (or ajax) for this.
Upvotes: 3
Reputation: 48038
JavaScript - Client Side.
PHP - Server Side.
You need to issue an AJAX call of some kind in order to communicate with the Server. There's no way for JavaScript to create directories on your Remote Server (this would be a huge security hole otherwise).
Upvotes: 1