Run php function inside jQuery click

后端 未结 9 739
醉酒成梦
醉酒成梦 2020-12-03 12:19

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.

         


        
相关标签:
9条回答
  • 2020-12-03 12:59

    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/

    0 讨论(0)
  • 2020-12-03 13:01

    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>
    
    0 讨论(0)
  • 2020-12-03 13:01

    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();
    
    0 讨论(0)
提交回复
热议问题