jQuery的Ajax调用从JavaScript到PHP [英] Jquery ajax call from javascript to PHP

查看:125
本文介绍了jQuery的Ajax调用从JavaScript到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有与code我有一个从JavaScript调用PHP的使用jQuery AJAX的问题。 Ajax调用似乎是成功的,但我不明白的PHP函数返回正确的信息。

There seems to be a problem with the code I have for calling php from javascript with jquery ajax. The ajax call seems to be successful but I don't get the correct information returned from the php function.

在PHP函数创建一个SQL查询。我发回的查询作为效应初探它在执行删除查询之前进行调试。下面是HTML的div来显示查询。

In the php function I create a SQL query. I send back the query as a reponse to debug it before performing a delete query. Here is the HTML for the div to show the query.

 <div id="thenode" style="position: absolute; top: 30px; left: 0px; width: 150px; background-color: white; z-index: 9999;">&nbsp;</div>

下面是jQuery的AJAX调用。有被发送到PHP函数两个变量:NODEID节点被删除,并选择删除功能

Here is the jquery ajax call. There are two variables being sent to the PHP function: nodeid for node to be delete, and option delete for the function.

function deleteitem()
{

     //get selected node
     var selectnod = getCookie('pnodid'); 

     //define php info and make ajax call
     $.ajax({
         url: "uptree.php",
         type: "POST",
         data: { node: selectnod, option: "delete" },
         cache: false,
         success: function (response) {
             $('#thenode').html(response);
         }
     });

}

下面是PHP函数。

<?php

function uptree() {

  $node = $_POST['node'];
  $option = $_POST['option'];

  if($node == '' || $option == '') {
    return '';
  }

  $dbco = mysql_connect('localhost', 'root', 'mmowebdb');
  if (!$dbco)
    {
    die('Could not connect: ' . mysql_error());
    }

  mysql_select_db("pagelinks", $dbco);

  $sql = "DELETE FROM dtree_table WHERE nid='$node'";

  return $sql;
}

?>

应该是简单,但这种Ajax调用返回一个空字符串,并导致在HTML中的div消失。这是我第一次在实际的项目中使用AJAX。这个问题必须容易找到人谁知道什么是AJAX确实。你能告诉的问题?

Should be straightforward but this ajax call returns an empty string and causes the div in the HTML to disappear. This is the first time I use ajax in an actual project. The problem must be easy to find for someone who knows what ajax really does. Can you tell the problems?

推荐答案

我找到了答案!感谢大家谁了关于SQL调用建议。但这里是实际的回答我的问题。

I found the answer! Thanks to all of you who had suggestions about the SQL call. But here is the actual answer to my question.

有在做一个Ajax Javascript来调用PHP的四个步骤。前两个步骤发生在Javascript的。另外两个步骤发生在PHP。

There are four steps in making an ajax Javascript to PHP call. The first two steps happen in the Javascript. The other two steps happen in the PHP.

步骤1.在Javascript中决定都需要在PHP函数什么变量,找回它们。

Step 1. In Javascript decide what variables are needed in the PHP function, retrieve them.

第2步:使AJAX调用PHP函数。 jQuery有值传递到PHP的便捷方式。你在为Ajax调用的数据项的名称 - 值对的数组是这样的。

Step 2. Make the ajax call to the PHP function. jquery has a convenient way of passing values to PHP. You have a an array of name-value pairs like this in the data item for the ajax call.

 data: { node: selectnod, option: "delete" },

第三步:让你的PHP函数在PHP文件准备好。写这样的功能。

Step 3. Have your PHP function ready in a PHP file. Write the function like this.

function updatetree($node, $option) {

步骤4回声呼叫内的PHP文件的PHP函数。

Step 4. Echo a call to the php function within that PHP file.

通过这四个步骤,你应该有一个PHP成功的呼叫,并能够从PHP函数返回信息的JavaScript。

With these four steps you should have a succesful call to PHP and be able to return information to javascript from the PHP function.

下面是javascript函数。

Here is the javascript function.

function deleteitem()
{

     //Get selected node to send to PHP function
     var selectnod = getCookie('pnodid'); 

     //Define php info, specify name of PHP file NOT PHP function
     //Note that by loading the PHP file you will probably execute any code in that file
     //that does not require a function call
     //Send PHP variables in the data item, and make ajax call
     //On success perform any action that you want, such as load a div here called thenode
     $.ajax({
         url: "uptree.php",
         type: "POST",
         data: { node: selectnod, option: "delete" },
         cache: false,
         success: function (response) {
             $('#thenode').html(response);
         }
     });

}

下面是PHP文件中的 uptree.PHP 。它有定义的函数,名为 updatetree 。它也有回音语句来调用该函数。这只是似乎是使函数来运行的方式。阿贾克斯本身不调用该函数。

Here is the PHP file uptree.PHP. It has a function defined, called updatetree. It also has an echo statement to call that function. This just seems to be the way to cause the function to run. Ajax itself doesn't call the function.

<?php

//Function defined here
//The variables will come from the ajax data statement
function updatetree($node, $option) {

  if($node == '' || $option == '') {
    return 'Select an item in the tree.';
  }

  $dbco = mysql_connect('localhost', 'root', 'mmowebdb');
  if (!$dbco)
    {
    die('Could not connect: ' . mysql_error());
    }

  mysql_select_db("pagelinks", $dbco);

  $sql = '';
  switch($option) {
     case 'delete':
        $sql = "DELETE FROM dtree_table WHERE nid='$node'";
        break;
     case 'add':
        list($pagename, $address) = explode(",", $page);
        $pagename = trim($pagename);
        $address = trim($address);
        $sql = "INSERT INTO dtree_table (nid, pid, name, url) values (NULL, ".$node.", '".$pagename."', '".$address."')";
        break;
     case 'update':
        break;
  }

  if (!empty($sql)) return $sql;
}

//echo statement to run function, variables sent by ajax are retrieved with $_REQUEST
//they could have also been retrieved with $_GET or $_POST
echo updatetree(trim($_REQUEST['node']),trim($_REQUEST['option']),trim($_REQUEST['page']));

?>

因此​​,要回顾一下。使用Javascript获得变量,使得AJAX调用PHP文件。阿贾克斯加载其中包含echo语句,导致PHP函数运行PHP文件。该PHP函数类型是指在同一文件中定义。该函数返回语句通过AJAX将信息发送回的JavaScript。 Javascript的做一些事的信息,例如加载到HTML页面上一个div。

So to recap. Javascript gets variables, makes ajax call to PHP file. Ajax loads PHP file which contains echo statement that causes PHP function to run. That PHP function is defined in that same file. The function return statement sends information back to javascript through ajax. Javascript does something with that information, e.g. load it into a div on the HTML page.

这篇关于jQuery的Ajax调用从JavaScript到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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