drq22639 2014-10-21 23:06
浏览 74
已采纳

PHP HTML输入将多个参数传递给jQuery AJAX调用

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 I want it to trigger the AJAX call and pass the value of the input field as well as the value from another PHP variable.

Here's the input in question:

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

I have a PHP variable - $uuid - which I also need to pass to my AJAX script, as well as the value of the input text field.

Here's my script as it currently stands:

<script type="text/javascript">
$(document).ready(function() {
    $("#storeManager").change(function(){
        $.ajax({
        url: "editProject.php",
        data: {
            storeManager: storeManager, 
            uuid: uuid
            },
        success: function(data) {
            $("#storeManager").html(data).addClass("has-success");
        },
        error: function (data) {
            $("#storeManager").html(data).addClass("has-error");
        }
        }); 
    });
});

</script>

I'm new to jQuery and AJAX and have tried everything I can think of but not sure how to add the required parameters?

  • 写回答

2条回答 默认 最新

  • dongming8867 2014-10-22 00:09
    关注

    Hopefully I understand what you are getting at.

    I am making an assumption that the markup you are providing lives in a file that is being processed by PHP and that $uuid exists in the context.

    <?php
        $uuid = '22dcf5f0-cbca-4dd7-8b5e-f9ca68a301ff';
    ?>
    
    <td><input type="text" class="form-control" id="storeManager" name="storeManager" value="Peter McMahon"></td>
    
    <script type="text/javascript">
    $(document).ready(function() {
        $("#storeManager").change(function(){
            var storeManager = $("#storeManager").val();
            $.post('editProject.php', { storeManager: storeManager, uuid: '<?=$uuid?>' }, function(data) {
                // cannot set $("#storeManager").html() - do something else with data.
                // if you alter the #storeManager html input in any way, you will need to rebind this eventListener.
                $("#storeManager").addClass("has-success");
            }).fail(function () {
                // no data available in this context
                $("#storeManager").addClass("has-error");
            });
         }); 
    });
    
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制