drcj64241 2011-06-29 13:28
浏览 92
已采纳

在drupal模块中通过Ajax调用执行PHP代码

I was trying to convert this into a Drupal module so that I can check PHP code instantly on my site for debugging purposes. I saw this Firefox add on which allows you to execute PHP on the fly but admin log in is necessary, so far I did everything , have one form and set up ajax calls , but if I pass a string like:

preg_match($pat,$str,$matches);
print_r($matches);

How to execute this in backend?

EDIT

To load the form:

$items['localphp'] = array(
  'page callback' => 'executePHP',
  'access callback' => TRUE,
  'type' => MENU_CALLBACK,
);

function executePHP(){
  $output = drupal_get_form('executePHP_form');
  return $output;
}

Ajax callback function:

$items['runPHP'] = array(
  'page callback' => 'getResult',
  'access callback' => TRUE,
  'type' => MENU_CALLBACK,
);
function getResult(){
  $code = $_POST['code'];
  //I need help here how to execute $code i.e the php code and return back result
  echo $code;
}

JS function

function executePHP(baseurl){
  var code = $("#edit-code").val();
  $.ajax({
      type : "POST",
      url : baseurl+'runPHP',
      data : 'code='+code,
      async : true,
      cache : false,
      success : function (res) {
        $("#edit-result").html(res);
      },
      error : function (res) {
        alert("error");
      }
  });
  return false;
}
  • 写回答

1条回答 默认 最新

  • dotn30471 2011-06-29 14:10
    关注

    FYI, the Devel module has a feature that allows admins to run custom PHP code from their page. The Devel module has a block with a ton of features useful for debugging.

    For a custom module, without seeing any of your other code, I can tell you what I have done to achieve AJAX in a Drupal module:

    In JavaScript make an AJAX request to a url on your site. Add a menu item with the specified path inside of hook_menu() as 'type' => MENU_CALLBACK that points to a function in your module. This function should do all the processing you need, then return the results to JavaScript to do what you want with it there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)
  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序