10

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;
})
Pinkie
  • 10,126
  • 22
  • 78
  • 124
  • 1
    possible duplicate of [Php inside a javascript function?](http://stackoverflow.com/questions/4729198/php-inside-a-javascript-function) – Marc B May 24 '11 at 21:31
  • 3
    Far far far far far far far far too many dupes of the question... – Marc B May 24 '11 at 21:31
  • 2
    @marc, The duplicate you posted has nothing to do with the question. Thanks to all who answered. – Pinkie May 24 '11 at 21:41
  • it has everything to do with your question. The mechanics may differ, but the end result is the same. – Marc B May 24 '11 at 21:43
  • 1
    @marc The dup link you posted doesn't help me. Answers below did. It wouldn't been more useful if you wasted your time answering the question rather then critiquing – Pinkie May 24 '11 at 21:46

9 Answers9

26

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); ?>
Cargowire
  • 1,488
  • 10
  • 21
9

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/

Community
  • 1
  • 1
Flatlin3
  • 1,658
  • 14
  • 26
3

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.

Bas Slagter
  • 9,831
  • 7
  • 47
  • 78
3

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.

2ndkauboy
  • 9,302
  • 3
  • 31
  • 65
2

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

Sven van den Boogaart
  • 11,833
  • 21
  • 86
  • 169
Cheluis
  • 1,402
  • 3
  • 22
  • 51
1

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).

g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
1

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>
Racooon
  • 1,468
  • 2
  • 10
  • 28
0

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.

romaninsh
  • 10,606
  • 4
  • 50
  • 70
-1

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();
Jijo John
  • 1,375
  • 9
  • 21