我第一次使用 AJAX 向我的 mysql 数据库发送一个简单的查询,该数据库将添加一个“产品”。但是它似乎没有将请求发送到我的数据库。我有 3 个文件 addProduct.php 是表单的基础,test.js 是请求的 javascript/ajax,我的 addProductTodatabase 是查询的位置。
(以防万一我搞砸了文件结构)
- addProduct.php 位于 F:\xampp\htdocs\cw test.js 位于
- test.js 位于 F:\xampp\htdocs\cw\javaScript
- addProductTodatabase.php 位于 F:\xampp\htdocs\cw\mysql
表单使用此代码..
<form name ="myForm" class = "hidden" id = "addProduct">
<h2 class = "under"> Next step: </h2>
<h4> Fill in the following information </h4>
<label class = "under" > Product Name <input type="text" name="ProductName" id = "productName"> </label>
<label class = "under" > Product category <input type="text" name="ProductCategory" id = "productCat"> </label>
<label class = "under" > Suitable age plus <input type="text" name="ProductAge" id = "productAge"> </label>
<label class = "under" >Add a Product discrition </label>
<textarea id = "productArea" name ="ProductDis" rows="15" cols="100"></textArea>
<label class = "under" > Product Price <input type="text" name="ProductPrice" id = "productPrice"> </label>
<label class = "under" > Number available <input type="text" name="ProductInStock" id = "productAvailable"> </label>
<label class = "under" > Image location <input type ="file" name= "ImageLocation" id= "imageLocation"> </label>
<input type ="button" onclick = "queryTest()" value = "Submit to database">
</form>
<div id = "loading">this should update </div>
test.js 使用此代码..
function queryTest()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('loading').innerHTML = "Please wait...";
//document.getElementById("loading").innerHTML=xmlhttp.responseText;
}
}
//document.getElementById('loading').innerHTML = "Please wait...";
// gets the variables and passes it onto the php page in the url bar
var productName = document.getElementById('productName').value;
var productCat = document.getElementById('productCat').value;
var productAge = document.getElementById('productAge').value;
var productDis = document.getElementById('productArea').value;
var productPrice = document.getElementById('productPrice').value;
var productStock = document.getElementById('productAvailable').value;
var productImage = document.getElementById('imageLocation').value;
var queryString = "?ProductName=" + productName + "&ProductCategory=" + productCat;
queryString += "&ProductAge=" + productAge + "&ProductDis=" + productDis;
queryString += "&ProductPrice=" + productPrice + "&ProductInStock=" + productStock;
queryString += "&ImageLocation=" + productImage;
xmlhttp.open("GET","../mysql/addProductTodatabase.php" + queryString,true);
xmlhttp.send(null);
}
和 addProductToDatabase 使用这个...
<?php
include "databaseLogIn.php";
//get variables from page before
$ProductName = $_GET["ProductName"];
$ProductCategory = $_GET["ProductCategory"];
$ProductAge = $_GET["ProductAge"];
$ProductDis = mysql_real_escape_string($_GET["ProductDis"]);
$ProductPrice = $_GET["ProductPrice"];
$ProductInStock = $_GET["ProductInStock"];
include "/imageHander/imagePointerDownloader.php";
//adds to table
$sql ="INSERT INTO Product ( ProductName, ProductCategory, SuitableAge, ProductDiscription, ProductPrice, ProductAvailable)
VALUES ('$ProductName','$ProductCategory','$ProductAge','$ProductDis','$ProductPrice','$ProductInStock')";
mysql_query($sql,$con);
// gets the product id
$query="SELECT * FROM Product";
$result=mysql_query($query);
$ItemID=mysql_numrows($result);
$PictureCaption = "this is a picture";
// adds picture caption
//adds a image to the table related to the product
$sql="INSERT INTO ProductPictures (ItemID, PictureCaption, ImagePointer)
VALUES ('$ItemID', '$PictureCaption', '$newFile')";
//echo $sql;
mysql_query($sql,$con)
mysql_close($con)
?>
欢迎任何帮助,感谢您的阅读。