2014-05-18 113 views
3

我想調用一個PHP函數,當一個HTML按鈕被點擊。我做了一些搜索,發現它不可能直接做到這一點,我應該使用Ajax。 所以這是我迄今爲止的嘗試,這是不working.this是我的test.php和功能也在這個頁面。php函數調用與AJAX

<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'; 
    } 
    ?> 

+1

你的函數將永遠不會被調用,因爲'$ x的值當你的時候總是'0'做比較。你的代碼在語法上也不正確。它應該看起來像:'$('。NextPage')。click(function(){$ .ajax({'。 –

+0

感謝您的快速響應.i按照您所說的編輯了我的代碼,但現在我得到錯誤「未定義的索引:x「在這一行:$ x = $ _POST ['x']; – user3622910

回答

2

得到Ajax調用的x的值。

$ x = $ _POST ['x'];

然後使用它。

編輯

首先你要檢查的天氣裏變量被設置或不..

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

試試這個

+0

感謝您的快速response.i編輯我的代碼,如你所說,但現在我得到」未定義索引:x「行:$ x = $ _POST ['x']; – user3622910

+0

檢查編輯的答案.. –

-2

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

1

首先,您需要將您的按鈕設置爲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> 
+0

感謝您的回答,我編輯我的code.still不工作!當我點擊按鈕 – user3622910

+0

@ user3622910 ,我在ajax的url上使用了'index.php',嘗試編輯它並匹配將處理該ajax的php的文件名稱: – user1978142

+2

@ user3622910:請不要抱怨「無法正常工作」,請實際嘗試調試問題。使用瀏覽器的控制檯查看是否正在觸發'POST'請求​​,以及是否收到回覆。查看網頁來源。繼續嘗試 - 這就是你如何解決問題:) –

1

我幾乎已經使用你的代碼,並得到它的工作。你的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"; 
    } 
+0

感謝您的answer.i編輯我的代碼,並且點擊按鈕時仍然沒有任何反應 – user3622910