私は初めてAJAXを使用して、「製品」を追加する単純なクエリをmysqlデータベースに送信します。しかし、それは私のデータベースにリクエストを送信していないようです。フォームのベースとなるaddProduct.php、リクエストのjavascript / ajaxであるtest.js、クエリの場所であるaddProductTodatabaseの3つのファイルがあります。
(念のため、ファイル構造を台無しにしました)
- 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)
?>
どんな助けでも歓迎します、読んでくれてありがとう。