Three ways to transfer between pages: TEXT, JSON, XML.
This chapter will explain the AJAX Web page transfer with the text situation
First: Body part code
<title>ajax in text and a table with delete function </title><script src= "Jquery-2.0.0.min.js" ></script>< Style type= "Text/css" >.sc{width:70px;height:30px;background-color: #3FF; color: #F00; text-align:center; line-height:30px;vertical-align:middle;font-size:20px;}. Sc:hover{cursor:pointer;background-color: #096;} </style>
Second: Click "Show Data" to use the chuli.php code
<?php include ("DBDA.class.php"); $db =new Dbda (); $sql = "SELECT * from info"; Write your own statement/* $attr = $db->query ($sql); $str = ""; foreach ($attr as $v) {$str = $str. Implode ("^", $v); $str = $str. "|";} $str =substr ($str, 0,strlen ($STR)-1); Intercept the final "|" * //Call the function inside the encapsulated class $re = $db->strquery ($sql); Echo $re;
The AJAX call returns the Strquery () function referenced by the string
The AJAX call returns the string function strquery ($sql, $type =0, $ku = "MyDB") {$db =new mysqli ($this->host, $this->uid, $this- >pwd, $ku);! Mysqli_connect_error () or Die ("Connection Failed"), $result = $db->query ($sql);//When querying a statement if ($type ==0) {$attr = $result->fetch_ All (); $str = "", for ($i =0; $i <count ($attr); $i + +) {for ($j =0; $j <count ($attr [$i]); $j + +) {$str = $str. $attr [$i] [$j]; $str = $str. " ^";} $str =substr ($str, 0,strlen ($STR)-1); $str = $str. "|";} $str =substr ($str, 0,strlen ($STR)-1); return $str;} Delete and change the statement else{if ($result) {return "OK";} Else{return "NO";}}}
Third: JS code function for "display data" running in JScript
function ShowAll () {$.ajax ({ async:false, url: "chuli02.php", DataType: "TEXT", success:function (data) { var str= "<tr align= ' center ' ><td> codename </td><td> name </td><td> Gender </td><td> National </td><td> Birthdays </td><td> actions </td><tr> "; var hang=data.split ("|"); with "|" Intercept for (var i=0;i
IV: Click "Delete" in the table to run the code in the background delete.php
<?php$code=$_post["Code"];include ("DBDA.class.php"); $db =new Dbda (); $sql = "Delete from info where code= ' {$code} '"; $r = $db->query ($sql, 1); 1 for additions and deletions, the default 0 means query if ($r) {echo "OK";} else{ echo "NO";}
V: JS function code for "delete" in JScript
function Bindck () { $ (". SC"). Click (function () { var code=$ (this). attr ("BS"); $.ajax ({ URL: "delete03.php", Data:{code:code}, type: "POST", DataType: "TEXT", success:function (data) { if ( data== "OK") { showall (); Bindck (); } else{ alert ("Delete failed! "); } } }) }) }
Sixth: Code for calls that combine two function codes in JScript
$ ("#btn"). Click (function () {showall (); Bindck (); })
Web page transfer in Ajax (i) text--database table with delete function display exercises