2014-10-21 113 views
0

我正在處理我的第一個使用jQuery執行AJAX HTTP POST的HTML表單。當用戶對輸入文本字段進行更改並跳出該字段時,我希望它觸發AJAX調用並傳遞輸入字段的值以及另一個PHP變量的值。PHP HTML輸入將多個參數傳遞給jQuery AJAX調用

這裏是有問題的輸入:

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

我有一個PHP變量 - $ UUID - 這也是我需要傳遞給我的AJAX腳本,以及輸入文本字段的值。

這裏是我的腳本,因爲它目前爲:

<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> 

我是新來的jQuery和AJAX,並已嘗試一切我能想到的,但不知道如何添加所需的參數?

+0

用ajax respoonse更新剛剛更改的輸入沒有意義。你想做什麼?你也不能在輸入中使用'html()'方法。當html也在客戶端時,任何php變量都不再可用。 php運行在服務器上,而不是在客戶端 – charlietfl 2014-10-21 23:17:22

+0

而不是'uuid:uuid'你會不會只是'uuid:'​​''假設你的文件是'.php'並且'$ uuid'變量在上下文中。 – logikal 2014-10-21 23:57:23

回答

1

希望我明白你在做什麼。

我假定您提供的標記生存在PHP正在處理的文件中,並且$ uuid存在於上下文中。

<?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

你需要讓你這樣的HTML輸入的值,也可以在輸入字段把$ UUID值(你可以將其隱藏,如果你想要的),所以你可以把它在相同的方式:

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

希望它有幫助!