我怎么能叫许多PHP函数与阿贾克斯呢? [英] How can i call one of many php functions with ajax?

查看:71
本文介绍了我怎么能叫许多PHP函数与阿贾克斯呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有php网站显示/隐藏通过导航链接的网页。
我想知道如何在相应的页面点击链接时调用我的PHP函数。 是否有可能使用AJAX(jQuery的?)来调用所有的功能之一,在我的function.php文件?
据我了解阿贾克斯将运行整个code在PHP文件的调用?

  $(文件)。在(点击,.dbLink,函数(){
  VAR STEXT = $(本).attr(数据变量);
  $阿贾克斯({
      网址:'../ini/functions.php?q='+sText,
      输入:'得到',
      成功:功能(数据){
          警报(数据);
      }
  });
});
 

function.php

 函数(){
    回声结果;
}

如果(使用isset($ _ POST ['行动'])){
    开关($ _ POST ['行动']){
       案一:
          一个();
          打破;
       案B:
          的b();
          打破;
       默认:
          打破;
   }
}
 

解决方案

将一个字符串中的AJAX的参数,并根据不同的字符串你的PHP code运行不同的功能,例如:

 开关($ _ GET ['运']){
    案删除:
        do_delete();
        打破;
    案更新:
        do_update();
        打破;
    ...
    默认:
        //报告无法识别的操作
}
 

在你的jQuery code,处理函数应该调用事件。preventDefault 或返回为prevent元素的正常动作,你点击(我猜这是一个链接)。另外,你需要向你发送AJAX请求,检索PHP中的参数的方式方法相匹配 - 如果你使用 $ _ POST ,你必须使用键入:后 - 和参数名称必须符合(您在使用Javascript的,而动作中的PHP)。

  $(文件)。在(点击,.dbLink功能(E){
    即preventDefault();
    VAR STEXT = $(本).attr(数据变量);
    $阿贾克斯({
        网址:../ini/functions.php,
        数据:{行动:STEXT}
        类型:'后',
        成功:功能(数据){
            警报(数据);
        }
    });
});
 

I have php website showing/hiding the pages through the nav links.
Im wondering how to call my php functions when clicking on the corresponding page link. Is it possible to use ajax (jquery?) to call one of all the functions on my function.php file?
As i understand it ajax will run the whole code in the php file its calling?

$(document).on("click", ".dbLink", function() {
  var sText = $(this).attr("data-variable");
  $.ajax({
      url: '../ini/functions.php?q='+sText,
      type: 'get',
      success: function(data){
          alert(data);
      }
  });
});

function.php

function a(){
    echo "result";
}

if (isset($_POST['action'])) {
    switch($_POST['action']) {
       case "a":
          a();
          break;
       case "b":
          b();
          break;
       default:
          break;
   }
}

解决方案

Put a string in the AJAX parameters, and have your PHP code run different functions depending on the string, e.g.

switch($_GET['op']) {
    case "delete":
        do_delete();
        break;
    case "update":
        do_update();
        break;
    ...
    default:
        // Report unrecognized operation
}

In your jQuery code, the handler function should call event.preventDefault or return false to prevent the normal action of the element you click on (I'm guessing it's a link). Also, you need to match the way you send the AJAX request with the way you retrieve the parameter in PHP -- if you use $_POST, you have to use type: 'post' -- and the parameter names must match (you used q in the Javascript, but action in the PHP).

$(document).on("click", ".dbLink", function(e) {
    e.preventDefault();
    var sText = $(this).attr("data-variable");
    $.ajax({
        url: '../ini/functions.php',
        data: { action: sText }
        type: 'post',
        success: function(data){
            alert(data);
        }
    });
});

这篇关于我怎么能叫许多PHP函数与阿贾克斯呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆