简体   繁体   English

php函数调用ajax

[英]php function call with ajax

I am trying to call a php function when an HTML button is clicked.i have done some searching and found that its impossible to do this directly and i should use ajax. 我试图在单击一个HTML按钮时调用一个php函数。我做了一些搜索,发现不可能直接这样做,我应该使用ajax。 so this is my attempt so far which is not working.this is my test.php and the function is also in this page. 所以这是我到目前为止的尝试,它不起作用。这是我的test.php,该功能也在这个页面中。

   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $('.NextPage').on('click', function() {
                $.ajax({
                    url: 'test.php',
                    data: {x: 1},
                    type: 'POST',
                    dataType: 'JSON',
                    success: function(response) {
                        alert(response);
                    }
                });

            });
        });
    </script>
     </head>
     <body>

    <button type="button" class="NextPage">go to nextpage</button> 

    <?php
    if (isset($_POST['x'])) {
        if ($_POST['x'] == 1) {
            $data = function1();
            echo json_encode($data);
            exit;
        }
    }

    function function1() {
        return 'Hi user! im function #1';
    }

    function function2() {
        return 'Hi user! im function #2';
    }
    ?>

get the value of the x from ajax call. 从ajax调用中获取x的值。

$x = $_POST['x']; $ x = $ _POST ['x'];

then use it. 然后使用它。

EDIT 编辑

First you have to check weather your variable is set or not.. 首先,您必须检查变量设置的天气与否。

if(isset($_POST[x]))
{
$x = $_POST['x'];
}

try this 尝试这个

First off, you need to set your button to type="button" , then, make an AJAX request, then the missing part on your code is the backend part which processes the request. 首先,您需要将按钮设置为type="button" ,然后发出AJAX请求,然后代码中缺少的部分是处理请求的后端部分。 Then the backend responds to that call. 然后后端响应该呼叫。 After that you can just do what you please on that response. 在那之后,你可以根据你的反应做你喜欢的事。 Consider this example: 考虑这个例子:

<?php

// this part handles the AJAX request
if(isset($_POST['x'])) {
    if($_POST['x'] == 1) {
        $data = function1();
        // a call has made, then give a response
        echo json_encode($data);
        exit;
    }
}

function function1() {
    // do some processing
    return 'Hi user! im function #1';
}

function function2() {
    return 'Hi user! im function #2';
}

?>

<!-- make its type "button" -->
<button type="button" class="NextPage">go to nextpage</button>
<div id="notification"></div>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('.NextPage').click(function(){
        $.ajax({
            url: 'test.php',
            data: {x: 1},
            type: 'POST',
            dataType: 'JSON',
            success: function(response) {
                $('#notification').html('Your ajax call has been successful<br/> and this is a notification').css({background: 'yellow'});
            }
        });

    });
});
</script>

I've practically used your code and got it working. 我几乎已经使用了你的代码并使其正常工作。 Your PHP is just fine, as for your AJAX call, it must have success or done to benefit the returned data. 您的PHP就好了,对于您的AJAX调用,它必须successdone以使返回的数据受益。

The code for reference is here 参考代码在这里

HTML HTML

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="mobile-web-app-capable" content="yes">
        <link rel="shortcut icon" sizes="196x196" href="">

        <link rel="stylesheet" type="text/css" href="" />
    </head>
    <body>
        <div id="abc"></div>
        <button type="submit" class="NextPage">go to nextpage</button>
        <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script type="text/javascript" src="app.js"></script>
    </body>
</html>

JavaScript JavaScript的

$(document).ready(function() {
    $('.NextPage').click(function() {
        $.ajax({
            type:'post',
            url:'service.php',
            data:{x:1}
        }).done(function(data) {
            $("#abc").text(data);
        });
    });
});

PHP PHP

<?php
    $x = $_POST['x'];

    if ($x == 1) {
        function1();
    }

    function function1() {
        echo "This is function 1";
    }

    function function2() {
        echo "This is function 2";
    }

What makes you think it is impossible to call a php function directly. 是什么让你觉得直接调用php函数是不可能的。 This is exactly what is done in CodeIgniter PHP MVC framework. 这正是CodeIgniter PHP MVC框架中所做的。 You can call a php function with arguments inside a php class directly in CodeIgniter and that is actually what is done always. 你可以直接在CodeIgniter中调用php类中带有参数的php函数,这实际上就是一直做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM