通过AJAX将URL查询字符串传递给PHP

时间:2019-04-12 15:09:13

标签: php mysql ajax

我正在创建一个网站,供用户使用其采购订单号和零件号查找发票号。

我已经设置了HTML表单,该表单将查询字符串传递到PHP文件,然后查询MySQL服务器,但是出现内部服务器错误。表单运行正常,正在生成带有查询字符串的URL。

这是我生成查询字符串的代码:

<html>
   <body>

      <script language = "javascript" type = "text/javascript">
         <!--
            //Browser Support Code
            function ajaxFunction(){
               var ajaxRequest;  // The variable that makes Ajax possible!

               try {
                  // Opera 8.0+, Firefox, Safari
                  ajaxRequest = new XMLHttpRequest();
               }catch (e) {
                  // Internet Explorer Browsers
                  try {
                     ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                  }catch (e) {
                     try{
                        ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                     }catch (e){
                        // Something went wrong
                        alert("Your browser broke!");
                        return false;
                     }
                  }
               }

               // Create a function that will receive data 
               // sent from the server and will update
               // div section in the same page.

               ajaxRequest.onreadystatechange = function(){
                  if(ajaxRequest.readyState == 4){
                     var ajaxDisplay = document.getElementById('ajaxDiv');
                     ajaxDisplay.innerHTML = ajaxRequest.responseText;
                  }
               }

               // Now get the value from user and pass it to
               // server script.

               var partnumber = document.getElementById('partnumber').value;
               var ponumber = document.getElementById('ponumber').value;
               var queryString = "?partnumber=" + partnumber ;

               queryString +=  "&ponumber=" + ponumber;
               ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
               ajaxRequest.send(); 
            }
         //-->
      </script>

      <form name = 'myForm'>
         Part Number: <input type = 'text' id = 'partnumber' /> <br />
         PO Number: <input type = 'text' id = 'ponumber' />
         <br />

         <input type = 'button' onclick = 'ajaxFunction()' value = 'Find Invoice Number'/>

      </form>

      <div id = 'ajaxDiv'>Your result will display here</div>
   </body>
</html>

这是ajax-example.php文件

   //Connect to MySQL Server
   mysql_connect($dbhost, $dbuser, $dbpass);

   //Select Database
   mysql_select_db($dbname) or die(mysql_error());

   // Retrieve data from Query String
   $partnumber = $_GET['partnumber'];
   $ponumber = $_GET['ponumber'];

   // Escape User Input to help prevent SQL Injection
   $partnumber = mysql_real_escape_string($partnumber);
   $ponumber = mysql_real_escape_string($ponumber);

   //build query
   $query = "SELECT * FROM data WHERE partnumber = '$partnumber' and `po#` = '$ponumber'";

   //Execute query
   $qry_result = mysql_query($query) or die(mysql_error());

   //Build Result String
   $display_string = "<table>";
   $display_string .= "<tr>";
   $display_string .= "<th>Part Number</th>";
   $display_string .= "<th>PO Number</th>";
   $display_string .= "<th>Invoice Number</th>";
   $display_string .= "</tr>";

   // Insert a new row in the table for each person returned
   while($row = mysql_fetch_array($qry_result)) {
      $display_string .= "<tr>";
      $display_string .= "<td>$row[partnumber]</td>";
      $display_string .= "<td>$row[po#]</td>";
      $display_string .= "<td>$row[invoicenumber]</td>";
      $display_string .= "</tr>";
   }
   echo "Query: " . $query . "<br />";

   $display_string .= "</table>";
   echo $display_string;
?>

0 个答案:

没有答案