【发布时间】:2013-12-20 02:07:43
【问题描述】:
我结合了我在各种网站(包括这个)上找到的许多示例,并创建了以下 HTML 代码,其中包含所需的 AJAX 功能。
我将此添加到我的 head 标记中(这阻止了我遇到的页面错误之一):
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
我的 HTML 的其余部分:
<script type="text/javascript" language="javascript">
$(document).ready(function(){
$("button").click(function(){
$(function ()
{
//var vInvoiceNo=document.getElementById('idInvoiceNo').value;
$.ajax({
url:'InvoiceViewFunction.php', //where is the SQL
data: "InvoiceNo=209", //value SQL needs to run the WHERE -- This will eventually be a variable I get from the form
dataType: 'json',
success: function(data) //the data that returns from SQL
{
//Populating variables
var vInvoiceNo = data[0];
var vClientName = data[2];
//Update form content
$('#idName').html(vClientName); //the idName is an input field on my html form
}
});
});
});
});
</script>
<body>
<form action="InvoiceViewFunction.php" method="post">
<?php include("InvoiceForm.php"); ?> <!--content of the html form with tables, tr, td etc-->
<button class="Button" type="button">Get Invoice with AJAX function</button>
</form>
</body>
</html>
加载页面时没有错误,但单击按钮时没有任何返回。我不太明白我在做什么。我写了我所理解的cmets。我没有足够的“功能”知识来解决它。这是一个功能动物园,所有功能都在功能业务中。救命!
这是来自 InvoiceViewFunction.php 的代码:
<?php
//Connect to database
include("../ConfigFiles/ConnectDB_local_i.php");
//Populating the variables
$InvoiceNo = $_POST["nInvoiceNo"];
//Reading a specific invoice from DB
echo "<br>Trying to read from DB with invoice = <br>" . $InvoiceNo . "<br>"; //This tells the correct number just fine.
$query = "SELECT * FROM `invoicedata_table` WHERE InvoiceNo = '$InvoiceNo'";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
//if($result->num_rows > 0)
//{
// while($row = $result->fetch_assoc())
// {echo stripslashes($row['ClientName']) . "<br>";}
//}
//else
//{echo 'NO RESULTS';}
echo json_encode($result);
//Close the DB connection
$mysqli->close();
?>
【问题讨论】:
-
表单中的SQL查询,呵呵...
-
尝试将数据作为
data: { "InvoiceNo":"209" }, -
你也可以发布PHP文件吗?