簡體   English   中英

php函數調用ajax

[英]php function call with ajax

我試圖在單擊一個HTML按鈕時調用一個php函數。我做了一些搜索,發現不可能直接這樣做,我應該使用ajax。 所以這是我到目前為止的嘗試,它不起作用。這是我的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';
    }
    ?>

從ajax調用中獲取x的值。

$ x = $ _POST ['x'];

然后使用它。

編輯

首先,您必須檢查變量設置的天氣與否。

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

嘗試這個

首先,您需要將按鈕設置為type="button" ,然后發出AJAX請求,然后代碼中缺少的部分是處理請求的后端部分。 然后后端響應該呼叫。 在那之后,你可以根據你的反應做你喜歡的事。 考慮這個例子:

<?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>

我幾乎已經使用了你的代碼並使其正常工作。 您的PHP就好了,對於您的AJAX調用,它必須successdone以使返回的數據受益。

參考代碼在這里

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的

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

PHP

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

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

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

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

是什么讓你覺得直接調用php函數是不可能的。 這正是CodeIgniter PHP MVC框架中所做的。 你可以直接在CodeIgniter中調用php類中帶有參數的php函數,這實際上就是一直做的事情。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM