根据PHP

时间:2015-12-02 11:55:28

标签: php jquery ajax wordpress

早上好。

我正在使用ajax和wp_mail在WordPress中创建一个2阶段表单。

目的是让用户填写有关访问高尔夫球场的基本信息,然后单击按钮进入步骤2.步骤2将允许用户填写他们希望参加的课程的详细信息根据你将要入住的天数。

到目前为止,我有以下内容。

第1步:填写用户留下的详细信息

 <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>

收集数据并将其发送到函数

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 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回显一个select字段。这就是我陷入困境的地方,我认为我的理论是正确的,但我不确定语法。

任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

是的,您可以使用循环和数组来存储每一天的输入(在输入名称中注意[]):

for ( $i = 0; $i <= $days; $i++ ) {
  echo '<input name="day_cource[]">';
}