doudg60800 2014-10-22 04:53
浏览 57
已采纳

将结果从AJAX调用传递给PHP脚本

I'm working on my first HTML form that performs an AJAX HTTP POST using jQuery. When a user makes a change to an input text field and tabs out of the field it triggers the AJAX script which in turn calls a PHP script which performs a database update.

The AJAX call can be successful but the database update could be unsuccessful (e.g. database related error) - I would like to insert the result of the PHP script into an alert. I can echo out any errors in in my PHP script, but I'm not sure how to get that into the appropriate alert.

Here's my Javascript:

 <script type="text/javascript">
   $(document).ready(function() {
    $("#storeManager").change(function(){
        var storeManager = $("#storeManager").val();
        $.post('editProject.php', { storeManager: storeManager, id: '1E1DDA14-D2C6-4FC8-BA5F-DBCCC7ABAF7F' }, function(data) {
            $("#managerRow").addClass("success");

        }).fail(function () {
            // no data available in this context
            $("#managerRow").addClass("danger");
            $("#ajaxAlert").addClass("alert alert-danger");
        });
     }); 
});
</script>

Here's the HTML table that contains the input field that triggers the AJAX call:

    <table class="table table-striped table-bordered table-hover">
    <tbody>
    <tr>
    <td>Store</td>
    <td>Acme Widgets Inc</td>
    </tr>
    <tr>
    <td>Retailer</td>
    <td>Acme Corp</td>
    </tr>
    <tr>
    <td>Store ID</td>
    <td>9876543</td>
    </tr>
    <tr>
    <td>State</td>
    <td>NSW</td>
    </tr>
    <tr class="" id="managerRow">
    <td>Manager</td>
    <td>

    <input type="text" class="form-control" id="storeManager" name="storeManager" value="Peter Johns">

    </td>
    </tr>
    <tr>
    <td>Phone</td>
    <td>9222 3456</td>
    </tr>
    </tbody>
    </table>

    <div class="" id="ajaxAlert" role="alert"></div>

What I would like to do is, if there is any error from the editProject.php script that it stores in a $error variable and can echo out, to then insert this into the ajaxAlert and add a class: alert:

<div class="alert alert-danger" id="ajaxAlert" role="alert">The error from the database update from the php script appears here</div>

I'm new to jQuery and AJAX and everything I've tried hasn't updated the alert with the new class and alert text and I can't seem to find a similar to example that demonstrates this.

  • 写回答

3条回答 默认 最新

  • dongyou26216708 2014-10-22 05:03
    关注

    You can use the append() in jquery. Try using

    fail(function () {
            // no data available in this context
            $("#managerRow").addClass("danger");
            //append error to the div using its ID
            $('#ajaxAlert').append('error from database');
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘