2014-12-19 32 views
-1

我是PHP的新手。如何在點擊一個按鈕時調用PHP

我需要運行這個jQuery代碼PHP

Class active只是改變display:nonedisplay:block

的代碼在這裏http://jsbin.com/weribi/1/

$(document).ready(function(){ 

    $(".cecutient-btn").click(function(){ 
    event.preventDefault(); 
    $(this).parent().find(".more-info-open") 
    .slideToggle("slow") 
    .toggleClass("active"); 

    }); 

}); 

什麼我需要做什麼?

+0

你需要使用AJAX – vaso123 2014-12-19 10:42:53

+1

那麼你不能從客戶端,因爲按鈕的點擊執行PHP代碼jquery是客戶端和PHP是服務器端。您需要學習並嘗試使用ajax ..http://api.jquery.com/jquery.ajax/ – 2014-12-19 10:43:09

+0

您無法從按鈕運行php。你可以對一個php頁面進行Ajax調用,使其運行在網絡上運行 – 2014-12-19 10:43:24

回答

2

請在點擊事件

研究的Ajax調用,學習!啓動從here

$(document).ready(function(){ 

    $(".cecutient-btn").click(function(){ 
    event.preventDefault(); 
    $(this).parent().find(".more-info-open") 
    .slideToggle("slow") 
    .toggleClass("active"); 

    $.ajax({ 
     type: "GET", 
     url: "yourFile.php", //your file .php 
     data: data, 
     success: function(data) { 

     } 
    }); 

    }); 

}); 

AJAX調用,而不jQuery的[info here]

function loadXMLDoc() { 
    var xmlhttp; 

    if (window.XMLHttpRequest) { 
     // code for IE7+, Firefox, Chrome, Opera, Safari 
     xmlhttp = new XMLHttpRequest(); 
    } else { 
     // code for IE6, IE5 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4) { 
      if(xmlhttp.status == 200){ 
       document.getElementById("myDiv").innerHTML = xmlhttp.responseText; 
      } 
      else if(xmlhttp.status == 400) { 
       alert('There was an error 400') 
      } 
      else { 
       alert('something else other than 200 was returned') 
      } 
     } 
    } 

    xmlhttp.open("GET", "ajax_info.txt", true); 
    xmlhttp.send(); 
} 
+1

請爲OP添加一些解釋。 – vaso123 2014-12-19 10:43:58

+0

@lolka_bolka我更新了答案:) – WhiteLine 2014-12-19 10:45:43

0

如果你想從你需要做一個AJAX調用你的PHP網頁上的點擊事件上運行PHP代碼。 php是一種服務器端語言,不能在瀏覽器上運行。

退房的文檔的jQuery的Ajax功能here

你想要的東西像

$.ajax({ 
    url: "url/to/myPhpPage.php" 
}).done(function() { 
    //callback code here 
}); 
相關問題