如何在单击按钮上调用PHP

时间:2014-12-19 10:41:32

标签: javascript php jquery

我是 PHP 的新手。

我需要将 jQuery代码运行到 PHP

Class active只需将display:none更改为display: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");

  });

});

我需要做什么?

2 个答案:

答案 0 :(得分: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) {

      }
    });

  });

});

没有jquery [info here]

的ajax调用
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 :(得分:0)

如果你想从点击事件中运行php代码,你需要对你的php页面进行ajax调用。 php是一种服务器端语言,无法在浏览器上运行。

查看jquery的ajax函数here

的文档

你想要像

这样的东西
$.ajax({
    url: "url/to/myPhpPage.php"
}).done(function() {
    //callback code here
});
相关问题