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?

dtsnx44260
dtsnx44260 而不是uuid:uuid你不只是做uuid:'<?=$uuid?>'假设你的文件是.php而$uuid变量在上下文中。
接近 6 年之前 回复
doulian4467
doulian4467 使用ajaxrespoonse来更新刚刚更改的输入并没有多大意义。你想做什么?您也不能在输入上使用html()方法。当html在客户端时,任何php变量都不再可用。php在服务器上运行,而不是在客户端
接近 6 年之前 回复

2个回答

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>



你需要像这样得到html输入的值,你也可以将$ uuid值放在输入字段中(你 如果你愿意,可以隐藏它)所以你可以用同样的方式调用它:</ p>

  .... 
data:{
storeManager:$(#storeManager).val( );
uuid:$(#uuid).val();
}
....
</ code> </ pre>

希望它有所帮助!</ p>

</ div>

展开原文

原文

You need to get the value of your html input like this, and also you can put the $uuid value in a input field (you can hide it if you want) so you can call it in same way:

....
data:{
    storeManager:$(#storeManager).val();
    uuid: $(#uuid).val();
}
....

Hope it helped!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问