簡體   English   中英

在jQuery append()中包含PHP函數的正確方法是什么?

[英]What is the proper way to include a PHP Function inside a jQuery append()?

我正在使用jquery將另一行追加到使用PHP的表單中。 原始代碼中的PHP使用一個函數來填充SELECT輸入。

表單中一個輸入的樣本;

<div class="flex_container">
<div class="flex_label_rpt">Person Type</div>
    <div class="flex_data">
        <select name="person_type[]">
            <?php get_persons_type_list($incident['person_type']) ?>
        </select>
    </div>
</div>
    ... Additional Fields ...
</div>

我可以使用相同的PHP函數填充使用jquery添加的新行嗎?

我嘗試過這樣的事情:

'<select name="person_type[]">'+
    "<?php get_persons_type_list($incident['person_type']) ?>"+
'</select>';

但是,注釋掉了,只是吐出了PHP代碼。

任何幫助將不勝感激。 我對PHP相當滿意,但對jquery卻不太了解。

謝謝。

- 編輯 - - - - - - - - - - - -

var maxField_p = 10; //Input fields increment limitation
var addButton_p = $('#incident_add_person'); //Add button selector
var wrapper_p = $('#item_row_person'); //Input field wrapper
var fieldHTML_p =
    '<select name="person_type[]">'+
    "<?php get_persons_type_list($incident['person_type']) ?>"+
    '</select>';

var x_p = 1; //Initial field counter is 1

$(addButton_p).click(function(){
    //Check maximum number of input fields
    if(x_p < maxField_p){ 
        x_p++; //Increment field counter
        $(wrapper_p).append(fieldHTML_p); //Add field html
    }
});

創建表單元素后,您需要使用ajax請求填充新行。

$.ajax({
    type: "GET",
    url: "script.php",
    dataType: 'JSON',
    beforeSend: function() {
        //You can show a preloader here
    }
}).done(function(data) {
    //Hide the preloader
    //Populate the form element 
}).fail(function() { 
    //Show an error message
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM