[英]Javascript is not passing in the values into my php code to run
當我運行表格和使用action="addCustomer.php"
的sql時,它工作正常,但是當我使用js時,數據未通過,並且控制台中未出現錯誤,它表示正在接聽來自原始頁面的時間軸中的變量,但是js不是嗎? 誰能看到原因?
我正在使用以下形式:
<form id="dataForm" method="GET">
<div>
<h2 id="formheader">Add Order Details:</h2>
<label>First Name:</label>
<input class="inputForm" id="inputFirstName" type="text" name="firstname">
</div>
<div>
<label>Last Name:</label>
<input class="inputForm" id="inputLastName" type="text" name="lastname">
</div>
<div>
<label>Address: </label>
<input class="inputForm" id="inputAdress" type="text" name="address">
</div>
<div>
<label>Post Code:</label>
<input class="inputForm" id="inputPostcode" type="text" name="postcode">
</div>
<div>
<section class="inputForm">
<label>Delievery Type:</label>
<select name="deliverytype">
<option ="3-5 Days">3-5 Days</option>
<option ="Next Day">Next Day</option>
</select>
</section>
<input type="hidden" id="calc" value="" name="calc">
</div>
<div id="theSubmit">
<button id="checkoutButton">Submit</button>
</div>
</form>
和下面的js:
function addCustomer(){
var xmlhttp = new XMLHttpRequest();
var firstName = document.getElementById("inputFirstName").value;
var lastName = document.getElementById("inputLastName").value;
var address = document.getElementById("inputAdress").value;
var postcode = document.getElementById("inputPostcode").value;
var delievery = document.getElementById("deliverytype").value;
if(firstName != "" && lastName != "" && address !="" && postcode !=""){
var url = "addCustomer.php?FIRSTNAME=" + firstName + "&LASTNAME=" + lastName + "&ADDRESS=" + address + "&POST_CODE=" + postcode + "&DELIVERY_TYPE=" + delievery;
xmlhttp.open("GET", url, false);
xmlhttp.send();
}
else{
alert("enter Some Data");
}
}
submitButton = document.getElementById("checkoutButton");
submitButton.addEventListener("click", addCustomer);
和以下php sql代碼:
$name = $_GET['firstname'];
$lastname = $_GET['lastname'];
$userAddress = $_GET['address'];
$userPostCode = $_GET['postcode'];
$delivery = $_GET['deliverytype'];
$totalCost = $_GET['calc'];
if($name !="" && $lastname !="" && $userAddress !="" && $userPostCode !="" ){
$sql = "INSERT INTO USERS (FIRSTNAME, SECONDNAME, ADDRESS, POST_CODE, DELIVERY_TYPE) VALUES ('$name', '$lastname', '$userAddress', '$userPostCode', '$delivery') ";
$conn->exec($sql);
echo "worked";
}
嘗試以小寫形式發送變量:
var url = "addcustomer.php?firstname=" + firstName + "&lastname=" + lastName + "&address=" + address + "&post_code=" + postcode + "&delivery_type=" + delievery;
您以任何方式發送變量的任何更改都應該在數據庫中發生,而不是在瀏覽器的控制台日志中發生,請閱讀有關SQL注入的信息 ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.