[英]Can't call posted value from dynamic php and ajax form
我建立了一個頁面,當您單擊“添加產品”時,該頁面會自動生成新的div。 每個div包含一個具有唯一名稱和ID的表單。 我使用此PHP和AJAX多級表單教程制作了表單-http: //www.codingcereal.com/2009/09/autopopulate-select-dropdown-box-using-jquery/
問題是,通過PHP提交表單時,我似乎無法調用這些值。 我能想到的唯一原因與動態生成的表單有關。
有任何想法嗎? 讓我知道您是否需要更多信息。
var i = 0;
$('a#add-product').click(function(event){
i++;
$('<div />').addClass('product').attr('id', 'product'+i)
.append($('<h2><img src="<?php echo base_url();?>img/product.png" alt="" />Product '+i+'</h2>'))
.append($('<div class="info-line"><label>Division</label><p><select id="selection-'+i+'"><option value="">- Select a Division -</option><option value="abrasives">Abrasives</option><option value="tapes">Bonding, Surface Protection & Tapes</option><option value="packaging">Packaging</option></select></p></div>'))
.append($('<div class="info-line"><label>Category</label><p><select id="selectionresult-'+i+'"></select><span id="result-'+i+'"> </span></p></div>'))
.append($('<div class="info-line"><label>Product</label><p><select id="selectionresult2-'+i+'"></select><span id="result2-'+i+'"> </span></p></div>'))
.append($('<a class="remove" href="#add-product" id="remove-product'+i+'"><img src="<?php echo base_url();?>img/remove-product.jpg" alt="" />Remove Product</a>'))
.appendTo("#products");
// START OF ADDITIONAL PRODUCT DROP DOWNS
$("#selectionresult-"+i).hide();
$("#selectionresult2-"+i).hide();
$("#selection-"+i).change( function() {
$("#selectionresult-"+i).hide();
$("#selectionresult2-"+i).hide();
$("#result-"+i).html('Retrieving ...');
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "<?php echo base_url();?>dropdown.php",
success: function(msg){
if (msg != ''){
$("#selectionresult-"+i).html(msg).show();
$("#result-"+i).html('');
}
else{
$("#result-"+i).html('<em>No item result</em>');
}
}
});
});
$("#selectionresult-"+i).change( function() {
$("#selectionresult2-"+i).hide();
$("#result2-"+i).html('Retrieving ...');
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "<?php echo base_url();?>dropdown.php",
success: function(msg){
if (msg != ''){
$("#selectionresult2-"+i).html(msg).show();
$("#result2-"+i).html('');
}
else{
$("#result2-"+i).html('<em>No item result</em>');
}
}
});
});
// END OF ADDITIONAL PRODUCT DROP DOWNS
//START OF PRODUCT IMAGE PREVIEWS
var productSelection = document.getElementById("selectionresult2-"+i);
var productPreview = document.getElementById("product"+i+"image");
//Accessories
$('#selectionresult2-'+i).change(function () {
if (productSelection.value == "3M Disc Pad Face 1"){
productPreview.src = "<?php echo base_url();?>img/3m-disc-pad1.jpg";
}
else if (productSelection.value == "Belt 1"){
productPreview.src = "<?php echo base_url();?>img/belt1.jpg";
}
else {
productPreview.src = "<?php echo base_url();?>img/spacer.gif";
}
});
//Belts
$('#selectionresult2-'+i).change(function () {
if (productSelection.value == "3M Disc Pad Face 1"){
productPreview.src = "<?php echo base_url();?>img/3m-disc-pad1.jpg";
}
else if (productSelection.value == "Belt 1"){
productPreview.src = "<?php echo base_url();?>img/belt1.jpg";
}
else {
productPreview.src = "<?php echo base_url();?>img/spacer.gif";
}
});
});
如果要在每個div中生成一個新表單,則在提交頁面時,將不會提交您創建的每個表單,而只是提交按鈕屬於的表單。
如果您沒有在每個div中放置新表單,那么所有新字段都將屬於您提交的字段,那么您應該能夠獲取它們的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.