【问题标题】:Add a number of input fields based on a dynamic value in PHPPHP中基于动态值添加多个输入字段
【发布时间】:2015-12-02 11:55:28
【问题描述】:

早上好。

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

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

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

第一步:填写用户入住详情

 <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 回显一个选择字段。这就是我卡住的地方,我认为我的理论是正确的,但我不确定语法。

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

【问题讨论】:

    标签: php jquery ajax wordpress


    【解决方案1】:

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

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

    【讨论】:

    • 我能问一下“day_course[]”的目的是什么,而不仅仅是一个名字或ID?
    • 它将允许您从$_POST 中的此输入中捕获数组;当然,您可以使用您想要的任何名称,这只是一种非常快捷的方法。这适用于 PHP 提交,您也可以将其与 jQuery ajax() 一起使用:stackoverflow.com/questions/19529443/…。当然它可以是 html select 而不是 input
    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多