Javascript + PHP +动态文本字段+ Jquery [英] Javascript + PHP + Dynamic Text Fields + Jquery
问题描述
我正在处理一个Web表单,该表单可以根据用户期望的多个日期动态添加文本字段.从本质上讲,这就是发生的事情……用户进入表单,它为用户提供了一个主要的日期文本字段,如果用户如此希望他可以通过单击添加日期"按钮添加一个日期文本字段. javascript如下:
I am working on a web form that dynamically adds text fields as the user desires for multiple dates. In essence this is what happens... The user goes to the form, it has a primary date text field for the user, IF the user so desires he may add a date text field through clicking a button that says "Add Date". The javascript is as follows:
function addDate() {
current++;
var strToAdd = '<p><input type="text" id="date'+current+'" name="classDate'+current+'" class="required" /> <input type="text" id="alt'+current+'" name="classDate'+current+'" class="required" /></p>'
//console.log(strToAdd)
$('.dynDate').append(strToAdd)
$('#date'+current+'').datepicker({altField: '#alt'+current+'', altFormat: 'DD, d MM, yy'});
}
这会将两个日期字段添加到表单,同时将每个新日期字段的jquery日期选择器放在表单上.完全像这样: http://jqueryui.com/demos/datepicker/#alt-field ...
This adds two date fields to the form along with putting the jquery date picker on the form for each new date field. Exactly like this: http://jqueryui.com/demos/datepicker/#alt-field ...
无论如何,我的问题是如何使用动态文本字段中的PHP将数据放入MYSQL数据库中?我会遍历它们还是什么?我想我真的不确定如何很好地处理动态字段.
Anyway, my question is how do I put the data into the MYSQL database with PHP from the dynamic text fields? Do I loop through them or what? I guess I am really not sure how to manipulate dynamic fields very well.
感谢您的帮助!
亚伦
推荐答案
命名每个日期字段,就像将其添加到PHP数组(而不是id)一样:
Name each date field as if it's being added to a PHP array (not the ids):
<input type="text" id="date'+current+'" name="classDate[]" class="required" />
使用[]语法(某种array_push()快捷方式),classDate将自动成为$ _POST中的数组.然后只需对该数组执行for循环即可.
Using the [ ] syntax (sort of an array_push() shortcut), classDate will automatically become an array in $_POST. Then just do a for loop on that array.
这篇关于Javascript + PHP +动态文本字段+ Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!