【问题标题】:AJAX function syntax - I don't understand wellAJAX函数语法——我不太懂
【发布时间】: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文件吗?

标签: php html ajax


【解决方案1】:

数据语法错误。 应该是:

data: {"InvoiceNo":209};

编辑#1

另外,你json_decode()服务器端的数据了吗?

编辑#2

另外,您的控制台是否出现任何错误?

【讨论】:

  • 数据可以被序列化,所以它应该按照示例中的方式工作
  • Eisa Adil,我尝试了新的数据版本 --- data: {"InvoiceNo":209}, --- 行为没有变化。我不知道 json_decode() 是什么意思。我编辑了我的问题以包含其中包含 SQL 的 php 代码。
【解决方案2】:

你没有带有id="idName" 的元素,因此结果不会放在任何地方。 Derp。阅读 cmets!

如果它是一个输入元素,那么你实际上在做的是:

<input id="idName">blah blah blah</input>

显然,这是无效的!

改用.val(vClientName)

【讨论】:

  • @EisaAdil 考虑到给出的代码包含整个&lt;body&gt; 元素,我对此表示怀疑。
  • 不,因为他还在那里包含了("InvoiceForm.php") 文件。
  • Eisa Adil,我的输入字段是通过 InvoiceForm.php 输入的。我尝试了 .val(vClientName) 并且没有任何变化。页面上没有错误或从函数返回。
【解决方案3】:

您正在使用“文档就绪”jQuery 函数,而不是简单地调用$.ajax。省略封闭的$(function() { }),只需在点击事件处理程序中调用$.ajax

这里有更多关于“文档就绪”语法的信息:http://www.faridesign.net/2012/03/shorthand-for-jquery-document-ready-event/

【讨论】:

  • Eisa Adil 也是正确的。 data 属性应该是一个 JavaScript 对象。
猜你喜欢
  • 2021-07-25
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多