如何使用ajax访问当前数据并将数据发送到另一个php文件


How to access current data and send data to another php file using ajax?

#ajax函数在php文件中我需要发送id,并将两个输入字段值发送到我的updateReq.php文件##

function updateFunction(del,inp1,inp2){
   var ajaxRequest; 
   try{
      ajaxRequest = new XMLHttpRequest();
   }catch (e){
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      }catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         }catch (e){
            alert("Your browser broke!");
            return false;
         }
      }
   }
   ajaxRequest.onreadystatechange = function(){
      if(ajaxRequest.readyState == 4){
         var ajaxDisplay = document.getElementById('ajaxDiv');
         ajaxDisplay.innerHTML = ajaxRequest.responseText;
      }
   }
   var queryString = "?del=" + del;
   queryString += "&inp1=" +inp1;
   queryString += "$inp2=" +inp2;
   ajaxRequest.open("GET", "updateReq.php" + queryString, true);
   ajaxRequest.send(null); 
}

在我的文件中,我使用这一部分来访问ajax函数

ajax函数和bleow代码在同一个php文件上

<?php
while($row = mysql_fetch_array($qry_result)){
   $display_string .= "<td><input type='text' id='inp1' name='inp1' value='$row[cname]' /></td>";
   $display_string .= "<td><input type='number' id='inp2' name='inp2' value='$row[rank]'/></td>";
   $display_string .= "<td><a href='#' onclick='deleteFunction($row[id])'>Delete?</a></td>";
   $display_string .= "<td><a href='#' onclick='updateFunction( $row[id], $row[cname] ,$row[rank])'>Update?</a></td>";
   $display_string .= "</tr>";
}
$display_string .= "</table>";
echo $display_string;
?>

这是我的updateReq.php

<?php
// Retrieve data from Query String
include 'config.php';
$id = $_GET['del'];
$inp1 = $_GET['inp1'];
$inp2 = $_GET['inp2'];
// Escape User Input to help prevent SQL Injection
$id = mysql_real_escape_string($id);
$query=mysql_query("update rcategories set cname='$inp1' rank='$inp2' where id='$id'") or die("can not update");
?>

您的代码中有一个错误。。

queryString += "$inp2=" +inp2;更改为queryString += "&inp2=" +inp2;

HTH

请检查您的queryString您使用的是$而不是&