如何通过JavaScript变量使用AJAX到PHP [英] How to pass javascript variable to php using ajax

查看:159
本文介绍了如何通过JavaScript变量使用AJAX到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我得到当按钮被点击到PHP,然后运行MySQL查询一个javascript变量。我的code:

I am trying to pass a javascript variable which I get when a button is clicked to php and then run a mysql query. My code:

function ajaxCall(nodeID) {
 $.ajax({
 type: "POST",
 url: "tree.php",
 data: {activeNodeID : nodeID}, 
 success: function(data) { 
 alert("Success!");
  }
 }
);
}

function onButtonClick(e, data) {
 switch (data.name) {
  case "add":
   break;
  case "edit":
   break;
  case "delete":
   nodeid = data.context.id;
   ajaxCall(nodeid);
   //query
   break;                                   
   }
}


<?php
      if (isset($_POST['activeNodeID'])) {
       $nodeid = $_POST['activeNodeID'];
      }
      else {
       $nodeid = 0;
      }
      ?>

我没有这样做过,所以我不知道为什么,这是行不通的。有什么建议么? 编辑:也许我没有解释自己不够好。我知道PHP是服务器端JavaScript是客户端。我已经使用基于所有信息保存在我的分贝的JavaScript显示的组织结构图。我需要做的是:被点击的JavaScript的一个按钮时(编辑,删除,添加)获取活动节点的ID,并把它作为变量在PHP运行查询。例如,删除数据库的行。什么是做到这一点的最好方法是什么?

I haven't done this before, so I am not sure why this doesn't work. Any suggestions? Maybe I haven't explained myself well enough. I realise that php is server side and javascript is client side. I have an org chart that is displayed using javascript based on all the info is saved in my db. What I need to do is: when one of the javascript buttons is clicked (edit, delete, add) get the ID of the active node and use it as variable in php to run query. For example, delete the row in the db. What's the best way to do it?

推荐答案

有一个有点混乱这里,成功 value存储一个被调用时,Ajax调用回调是成功的。为了给你一个简单的例子,假设你有一个Ajax调用是这样的:

There's a bit of confusion here, the success value stores a callback that gets called when the Ajax call is successful. To give you a simple example, let's say you have an Ajax call like this:

function ajaxCall(nodeID) {
    $.ajax({
      type: "POST",
      url: "tree.php",
      data: {activeNodeID : nodeID}, 
      success: function(data) {
        console.log(data.my_message); 
      }
    });

此调用名为 tree.php PHP页面。在这个页面中,你做了一些计算,然后返回一个值,在此情况下JSON。所以页面看起来像这样(请注意,我是来简单,你应该验证输入值,总是):

This calls a PHP page called tree.php. In this page you do some computation and then return a value, in this case as JSON. So the page looks like this (note that I'm keeping it simple, you should validate input values, always):

$v = $_REQUEST['activeNodeID'];
$to_return = "the value sent is: " . $v;
return json_encode(array('my_message' => $to_return));

这个简单的PHP页面返回的字符串作为JSON。在JavaScript中指定的回调(即成功),你得到数据作为一个对象,包含了PHP的$ to_return 值<$ C C $>。你所写并没有真正有道理的,因为PHP是一种服务器端语言,不能从像JavaScript的浏览器进行处理。

This simple PHP page returns that string as a JSON. In your javascript, in the specified callback (that is success), you get data as an object that contains the PHP's $to_return value. What you have written does not really makes sense since PHP is a server language and cannot be processed from the browser like JavaScript.

这篇关于如何通过JavaScript变量使用AJAX到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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