我使用 jquery ajax 从 javascript 调用 php 的代码似乎有问题。 ajax 调用似乎成功,但我没有从 php 函数返回正确的信息。
在 php 函数中,我创建了一个 SQL 查询。在执行删除查询之前,我将查询作为响应发送回以对其进行调试。以下是用于显示查询的 div 的 HTML。
<div id="thenode" style="position: absolute; top: 30px; left: 0px; width: 150px; background-color: white; z-index: 9999;"> </div>
这是 jquery ajax 调用。有两个变量被发送到 PHP 函数:用于要删除的节点的 nodeid,以及用于函数的选项删除。
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 真正用途的人来说,这个问题一定很容易找到。你能说出其中的问题吗?
最佳答案
我找到了答案!感谢所有对 SQL 调用提出建议的人。但这是我的问题的实际答案。
进行 ajax Javascript 到 PHP 调用有四个步骤。前两个步骤发生在 Javascript 中。另外两个步骤发生在 PHP 中。
第 1 步。在 Javascript 中决定 PHP 函数中需要哪些变量,并检索它们。
步骤 2. 对 PHP 函数进行 ajax 调用。 jquery 有一种将值传递给 PHP 的便捷方法。在用于 ajax 调用的数据项中,您有一个像这样的名称-值对的数组。
data: { node: selectnod, option: "delete" },
第 3 步:在 PHP 文件中准备好 PHP 函数。像这样编写函数。
function updatetree($node, $option) {
第 4 步:回显对该 PHP 文件中的 php 函数的调用。
通过这四个步骤,您应该可以成功调用 PHP,并能够从 PHP 函数将信息返回到 javascript。
这是 JavaScript 函数。
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。它还有一个 echo 语句来调用该函数。这似乎就是导致该函数运行的方法。 Ajax 本身不调用该函数。
<?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 获取变量,对 PHP 文件进行 ajax 调用。 Ajax 加载 PHP 文件,其中包含导致 PHP 函数运行的 echo 语句。该 PHP 函数是在同一个文件中定义的。函数return语句通过ajax将信息发送回javascript。 Javascript 对这些信息执行一些操作,例如将其加载到 HTML 页面上的 div 中。
关于php - 从 javascript 到 PHP 的 Jquery ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6804327/