我找到了答案!感谢所有对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 pre 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 statementfunction 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 $_POSTecho updatetree(trim($_REQUEST['node']),trim($_REQUEST['option']),trim($_REQUEST['page']));?>因此,回顾一下。Javascript获取变量,对PHP文件进行Ajax调用。Ajax加载PHP文件,该文件包含使PHP函数运行的echo语句。该PHP函数在同一文件中定义。函数return语句通过ajax将信息发送回javascript。Javascript使用该信息执行某些操作,例如将其加载到HTML页面上的div中。



