根据PHP中的动态值添加许多输入字段 [英] Add a number of input fields based on a dynamic value in PHP
问题描述
早上好.
我正在使用ajax和wp_mail在WordPress发送中创建一个两阶段表单.
I am creating a 2 stage form in WordPress sending using ajax and wp_mail.
目的是允许用户填写有关高尔夫球场访问的基本信息,然后单击按钮以继续执行步骤2.步骤2将允许用户填写他们希望参加的课程的详细信息根据您将要停留的天数.
The purpose is to allow the user to fill in basic information about a visit to a golf course and then click a button to proceed to step 2. Step 2 will allow the user to fill in details of the courses they wish to attend based on the number of days thy will be staying.
到目前为止,我有以下内容.
So far I have the following.
第1步:填写用户留下的详细信息
Step 1 : Fill in the details of the users stay
<form id="booking-form">
<div class="step1">
<h4>1. Your Personal Details</h4>
<label for="name">First Name</label>
<input type="text" id="firstname">
<label for="surname">Surname</label>
<input type="text" id="surname">
<label for="phone">Telephone</label>
<input type="text" id="telephone">
<label for="phone">Mobile</label>
<input type="text" id="mobile">
<label for="phone">Email</label>
<input type="text" id="email">
<h4>Booking Details</h4>
<label for="noofgolfers">Number of Golfers</label>
<input type="number" id="noofgolfers">
<label for="arrival">Arrival Date</label>
<input type="text" id="arrival">
<label for="leaving">Departure Date</label>
<input type="text" id="leaving">
<!-- User will click here, this will send he data via ajax to wp_mail() -->
<input type="text" id="step" value="Proceed to step 2 of 2">
</div>
<div class="step2">
<h4>Please Choose Golf Course</h4>
<div id="step2content">
</div>
</div>
</form>
收集数据并将其发送给函数
Collect the data and send it to the function
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 apply the returned HTML to #step2content
console.log(result);
}
});
});
});
});
发送邮件并计算添加到阶段2表单所需的字段数的功能.
Function that sends the mail and calculates the number of fields required to add to the stage 2 form.
function implement_ajax_bookingrequest(){
if(isset($_POST['firstname']))
{
//gets all the posted values from the form
$firstname = ($_POST['firstname']);
$surname = ($_POST['surname']);
$telephone = ($_POST['telephone']);
$mobile = ($_POST['mobile']);
$email = ($_POST['email']);
$noofgolfers = ($_POST['noofgolfers']);
$arrival = ($_POST['arrival']);
$leaving = ($_POST['leaving']);
//construct the email
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<h2>Booking Confirmation From ' . $firstname . ' ' . $surname . '</h2>
<table width="500">
<tr>
<td>Telephone</td>
<td>' . $telephone . '</td>
</tr>
<tr>
<td>Mobile</td>
<td>' . $mobile . '</td>
</tr>
<tr>
<td>Email</td>
<td>' . $email . '</td>
</tr>
<tr>
<td>Number of golfers</td>
<td>' . $noofgolfers . '</td>
</tr>
<tr>
<td>Arrival</td>
<td>' . $arrival . '</td>
</tr>
<tr>
<td>Departure</td>
<td>' . $leaving . '</td>
</tr>
</table>
</body>
</html>
';
//send the email
$to = 'coda@knoppys.co.uk';
$subject = 'Booking confirmation ' . $firsname . ' ' . $surname;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Ayrshire Golf Website' . "\r\n";
wp_mail( $to, $subject, $message, $headers);
}
//count the number of days
$date1=date_create($arrival);
$date2=date_create($leaving);
$diff=date_diff($date1,$date2);
$days = $diff->format("%a");
//run a loop to echo out an input for each of the days.
die();
}
add_action('wp_ajax_bookingrequest', 'implement_ajax_bookingrequest');
add_action('wp_ajax_nopriv_bookingrequest', 'implement_ajax_bookingrequest');
我想对天数(该位)进行计数,然后运行一个循环(我猜这是我最好的选择),该循环将为每个计算出的$ days回显一个选择字段.这就是我陷入困境的地方,我认为我的理论是正确的,但是我不确定语法.
I would like to function to count the number of days (got that bit) then run a loop (which im guessing is my best option) that will echo a select field for each of the calculated $days. This is where Im getting stuck, I think my theory is right but I’m not sure on the syntax.
任何帮助将不胜感激,谢谢.
Any help would be appreciated, thanks.
推荐答案
是的,您可以使用循环和数组来存储每天的输入(请注意输入名称中[]
的注意事项)
Yep, you can use a loop and an array to store each day input (pay attentiona at []
in the input name):
for ( $i = 0; $i <= $days; $i++ ) {
echo '<input name="day_cource[]">';
}
这篇关于根据PHP中的动态值添加许多输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!