[英]How to get input values from a dynamically generated form
My question for the previous step is here Add a number of input fields based on a dynamic value in PHP 我对上一步的问题是在此处根据PHP中的动态值添加许多输入字段
However, to continue on from this I have a new issue and Im not sure what to search for on here to find an answer so excuse me if this has already been answered. 但是,要继续进行下去,我遇到了一个新问题,我不确定在这里要搜索什么以找到答案,因此,如果已经回答了,请原谅。
I currently have a number $date which is calculated from 2 date fields. 我目前有一个数字$ date,它是根据2个日期字段计算得出的。 The user picks a date and is then presented with a <select>
field for each day. 用户选择一个日期,然后每天显示一个<select>
字段。 However, I wish to collect the values form the selections made and send them to my form via jQuery and ajax. 但是,我希望从所做选择中收集值,然后通过jQuery和Ajax将它们发送到我的表单中。
Step 1 (lets say the user has selected 3 days and is presented with 3 <select>
field's) 步骤1(假设用户选择了3天并显示3个<select>
字段)
<select id="course_select_1">
<option value="value 1">Value 1</option>
<option value="value 2">Value 2</option>
</select>
<select id="course_select_2">
<option value="value 1">Value 1</option>
<option value="value 2">Value 2</option>
</select>
<select id="course_select_3">
<option value="value 1">Value 1</option>
<option value="value 2">Value 2</option>
</select>
For ref, here is the PHP that generates the select boxes. 对于参考,这是生成选择框的PHP。
for ( $i = 1; $i <= $days; $i++ ) {
echo '
<p>Day ' . $i . ' </p>
<select id="course-select' . $i . '">
<option>Choose Course...</option>
<option value="No golf">No golf</option>
</select>
';
}
For ref, this is the JS I use to collect my form data from teh previous form and the method I am used to using to collect data from fixed fields. 对于ref,这是我用来从以前的表单中收集表单数据的JS,以及我用来从固定字段中收集数据的方法。
jQuery(document).ready(function(){
jQuery('#step').click(function() {
jQuery('.step1').fadeOut();
jQuery('.step2').fadeIn();
var firstname = jQuery('#firstname').val();
var surname = jQuery('#surname').val();
var telephone = jQuery('#telephone').val();
var mobile = jQuery('#mobile').val();
var email = jQuery('#email').val();
var noofgolfers = jQuery('#noofgolfers').val();
var arrival = jQuery('#arrival').val();
var leaving = jQuery('#leaving').val();
jQuery(function(){
jQuery.ajax({
url:"http://www.ayrshiregolf.com/wp-admin/admin-ajax.php",
type:'POST',
data:'action=bookingrequest&firstname=' + firstname +
'&surname=' + surname +
'&telephone=' + telephone +
'&mobile=' + mobile +
'&email=' + email +
'&noofgolfers=' + noofgolfers +
'&arrival=' + arrival +
'&leaving=' + leaving,
success:function(result){
//got it back, now assign it to its fields.
jQuery('#step2content').html(result);
//console.log(result);
}
});
});
});
});
Step 2 : User chooses the options and we then submit the form again using the same method (jquery to get the data and send to my function in PHP). 第2步:用户选择选项,然后我们使用相同的方法(通过jquery获取数据并将其发送到PHP中的函数)再次提交表单。
How do I get the values from the fields if I don't know how many there are going to be? 如果我不知道会有多少值,如何从字段中获取值?
Thanks 谢谢
Use the jQuery serialize
method. 使用jQuery serialize
方法。
Also, you can use the jQuery post
method (it's easier): 另外,您可以使用jQuery post
方法(更简单):
If your form
has the ID myForm
: 如果您的form
具有ID myForm
:
jQuery('#step').click(function() {
jQuery('.step1').fadeOut();
jQuery('.step2').fadeIn();
jQuery.post("http://www.ayrshiregolf.com/wp-admin/admin-ajax.php",
jQuery("#myForm").serialize(),
function(result){
jQuery('#step2content').html(result);
}
});
});
To identify values in the PHP
page, you need to add the name
attribute to your form fields: 要在PHP
页面中标识值,您需要将name
属性添加到表单字段中:
<input type="text" name="myTextInput">
<?php
echo $_POST["myTextInput"]; //will output the value of the textarea named myTextInput
jQuery.ajax({
url:"http://www.ayrshiregolf.com/wp-admin/admin- ajax.php",
type:'POST',
data:$('form').serialize(),
success:function(result){
//got it back, now assign it to its fields.
jQuery('#step2content').html(result);
//console.log(result);
});
This will send all the data in the form 这将以表格形式发送所有数据
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.