該應用程序用於跟蹤有多少電纜存貨。如何讓php/ajax從mysql獲得一個整數
因此,我努力工作的應用程序部分如下。
當用戶從下拉框中選擇一些內容時,它會查詢MySQL數據庫並返回一個整數。每個選擇只有一個整數。例如,用戶從下拉框中選擇一條5米長的橙色電纜,它將顯示剩餘電纜的數量。它存儲在mysql數據庫中。如果有人可以請求提供一些幫助,我有一個噩夢讓代碼工作。
這裏是主頁
<html>
<head>
<title>Cable Management</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<?php include("Header.php"); ?>
<div id="main">
<h1>Cable Management</h1>
<h2>Bournemouth Office</h2>
<p>Enter how many cables you want to remove or add from the Bournemouth office</p>
<script>
function showcables(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcable.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<INPUT TYPE = "TEXT" NAME='amount' VALUE ="">
<select name="cables" onchange="showcables(this.value)">
<option value="">Select a cable type</option>
<option value="0.25 Metre Orange">0.25 Metre Orange</option>
<option value="0.3 Metre Orange">0.3 Metre Orange</option>
<option value="0.5 Metre Orange">0.5 Metre Orange</option>
<option value="1 Metre Orange">1 Metre Orange</option>
<option value="2 Metre Orange">2 Metre Orange</option>
<option value="3 Metre Orange">3 Metre Orange</option>
<option value="5 Metre Orange">5 Metre Orange</option>
<option value="10 Metre Orange">10 Metre Orange</option>
<option value="2 Metre Black">2 Metre Black</option>
<option value="3 Metre Black">3 Metre Black</option>
<option value="5 Metre Black">5 Metre Black</option>
<option value="10 Metre Black">10 Metre Black</option>
<option value="RJ 11">RJ11</option>
<option value="RJ 11 to BT">RJ11 to BT</option>
</select>
<INPUT TYPE = "button" Name = "Remove" VALUE = "Remove">
<INPUT TYPE = "button" Name = "Add" VALUE = "Add">
</form>
<br>
<div id="txtHint"><b>The amount of cables of that type will be listed here</b></div>
</select>
</div>
<div class="footer-clear"></div><!-- add this at the end of wrapper -->
</div> <!-- i added this closing tag, it was missing (for wrapper) -->
<?php include("Footer.php"); ?> <!-- move footer to go outside of wrapper -->
</body>
</html>
這裏是getcable.php文件提前
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','root','','cables');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"bmouthoff");
$sql="SELECT * FROM bmouthoff WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Amount'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
感謝
你應該考慮使用jQuery,而不是原生的XMLHttpRequest –
@瘸了 - 你應該考慮學習ning JavaScript,而不是將jQuery用於最平凡的事情。 –
並在某處設置測試用例並解釋您正在嘗試解決的確切問題,我不確定是否有人會爲您重寫您的代碼。 – awm