Ajax Multiple提交表单并获取有关表单信息的电子邮件

时间:2016-12-12 05:34:46

标签: php jquery ajax forms

我在通过ajax提交表单时遇到问题。我提交相同的表格两次,我想通过电子邮件发送相同的表格。在第一次提交时,我想要名称,电子邮件,联系信息以及表格的第二次提交,我想要有关用户的城市相关信息。表单工作正常,但只有当用户在第二次尝试时提交表单时才收到邮件。当用户通过ajax提交表单时,我没有收到有关用户名和电子邮件等的任何电子邮件信息。此外,我正在使用该表单中的Google验证码。代码工作正常,我没有在第一次提交表单时收到任何电子邮件。需要帮忙..!!任何帮助将不胜感激,谢谢.. !!

这是我的代码: -

 if(isset($_POST['contraform']) && $_POST['contraform']=='submited'){



                $secret   = "6Lf0MAoUAAAAAMWzxj-kbdy-u32onIYhZ0TdzpDc";
                $response = $_POST['g-recaptcha-response'];
                $remoteip = $_SERVER['REMOTE_ADDR'];
                $url      = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
                $result   = json_decode($url, TRUE);
                if ($result['success'] == 1) {


                  if(isset($_POST["source"])){
                   $source = $_POST["source"];
                  }
                  if(isset($_POST["uname"])){
                     $name = $_POST["uname"];
                  }
                    if(isset($_POST["uemail"])){
                    $email = $_POST["uemail"];
                  }
                  if(isset($_POST["uphonefull"])){
                     $phone = $_POST["uphonefull"];
                  }
                  if(isset($_POST["utreatments"])){
                   $treatment = $_POST["utreatments"];
                  }

                  if(isset($_POST["form_name"])){
                   $form_name = $_POST["form_name"];
                  }
                  if(isset($_POST["ucity"])){
                   $city = $_POST["ucity"];
                  }

                  if(isset($_POST["uhospital"])){
                   $hospital = $_POST["uhospital"];
                  }


                 if(isset($_POST["thanks"])){
                   $thanks = $_POST["thanks"];
                  }

                  header_contact_forms($source,$name,$email,$phone,$treatment,$form_name, $city,$hospital);   
             }     


 }   

这是我的表单代码: -

   <form name="contraform" id="contraform" method="post" action="<?php bloginfo("template_directory"); ?>/forms/process.php">
<input type="hidden" name="form_name" id="form_name" value="Header Contact Form" class="basicformname" >
            <input type="hidden" value="<?php echo $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];  ?>" name="source" id="source" />
            <input type="hidden" value="submited" name="basicform"/>

              <div id="sf1" class="frm">
                <fieldset>
                  <!--<legend>Take a step for a Good health.</legend>-->
                  <div class="wor-alert"><p class="text-center fill-fields">Please Fill All This Fields*</p></div>
                  <div class="form-group">
                    <div class="input-group">
                      <span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-user"></i></span>
                      <input type="text" placeholder="Name" id="uname" name="uname" class="form-control" autocomplete="off">
                      <span class="nameerr" id="unameerr"><?php echo @$_SESSION['unameerr'] ? $_SESSION['unameerr']:''; unset($_SESSION['unameerr']); ?></span>
                    </div>

                  </div>
                  <div class="form-group">

                    <div class="input-group">
                      <span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-envelope"></i></span>
                      <input type="email" placeholder="Email" id="uemail" name="uemail" class="form-control" autocomplete="on">
                      <span class="emailerr"  id="uemailerr"><?php echo @$_SESSION['uemailerr'] ? $_SESSION['uemailerr']:''; unset($_SESSION['uemailer']); ?></span>
                    </div>

                  </div>
                  <div class="form-group">
                      <input type="text" placeholder="Phone Number" id="uphone" name="uphone" class="uphone form-control" autocomplete="off">
                      <span class="phoneerr"  id="uphoneerr"><?php echo @$_SESSION['uphoneerr'] ? $_SESSION['uphoneerr']:''; unset($_SESSION['uphoneerr']); ?></span>
                      <input type="hidden" class="uphonefull" name="uphonefull">
                  </div>
                  <div class="form-group">
                    <div class="input-group">
                      <span class="input-group-addon" id="basic-addon"><i class="abc-icon abc-user-md"></i></span>
                       <span class="plain-select">
                         <select name="utreatments" data-style="btn-primary" id="utreatments" class="form-control selectpicker">
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>
                          <option value="value">value</option>

                        </select>
                      </span>
                    </div>

                  </div>
                  <div class="col-md-12 text-center"> 
                  <div class="head-captcha">                  
                      <div id="recaptcha3"></div>
                      <input type="hidden" class="hiddenRecaptcha required" name="bhiddenRecaptcha" id="bhiddenRecaptcha">
                    </div>
                  </div>

                  <div class="clearfix" style="height: 10px;clear: both;"></div>


                  <div class=" text-center">

                      <button class="btn open1" type="button" id="head-frm-btn">Next <span class="fa fa-arrow-right"></span></button> 

                  </div>

                </fieldset>
              </div>

              <div id="sf2" class="frm" style="display: none;">
                <fieldset>
                  <!--<legend>What treatment are you looking for?</legend>-->

                  <div class="form-group">
                     <span class="plain-select">
                      <select name="ucity" id="ucity" class="form-control">
                        <option value="none">Select City</option>
                        <option value="Mumbai">Mumbai</option>
                        <option value="Pune">Pune</option>
                        <option value="Banglore">Banglore</option>
                        <option value="Nagpur">Nagpur</option>
                        <option value="Goa">Goa</option>
                      </select>
                    </span>
                  </div>
                  <div class="form-group">
                     <span class="plain-select">
                      <select name="uhospital" id="uhospital" class="form-control">
                        <option value="">Select Hospital</option>
                        <option value="Hospital One">Hospital One</option>
                        <option value="Hospital Two">Hospital Two</option>
                        <option value="Hospital Three">Hospital Three</option>
                        <option value="Hospital Four">Hospital Four</option>
                        <option value="Hospital Five">Hospital Five</option>
                      </select>
                    </span>
                  </div>

                  <div class="clearfix" style="height: 10px;clear: both;"></div>



                  <div class="clearfix" style="height: 10px;clear: both;"></div>

                  <div class=" text-center">

                      <button class="btn back2" type="button"><span class="fa fa-arrow-left"></span> Back</button> 
                      <button class="btn open2" type="button" id="head-frm-btn-two">Next <span class="fa fa-arrow-right"></span></button> 

                  </div>

                </fieldset>
              </div>

              <div id="sf3" class="frm" style="display: none;">
                <fieldset>
                  <!--<legend>Almost done</legend>-->

                  <div class="waittext">
                      <p>Our customer care executive will get in contact with you soon. Please bear with us.</p>
                  </div>
                  <div class="clearfix" style="height: 10px;clear: both;"></div>

                  <div class=" text-center">

                      <button class="btn back3" type="button"><span class="fa fa-arrow-left"></span> Back</button> 
                      <input class="btn open3" value="Submit" type="submit" name="head_form_submit"> 


                  </div>

                </fieldset>
              </div>
            </form>

以下是Ajax表单提交代码: -

var eml = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   $("#head-frm-btn, #head_form_submit").click(function () {
        $(".wor-alert").hide();
        var source = $("#source").val();
        var uname = $("#uname").val();
        var uemail = $("#uemail").val();
        var uphone = $("#uphone").val();
       // var ucity = $("#ucity").val();
        //var uhospital = $("#uhospital").val();
        var utreatments = $("#utreatments").val();
        var uphonefull = $(".uphonefull").val();
        var form_name  = $("#form_name").val();


        var basicform = "submited";


        // Returns successful data submission message when the entered information is stored in database.
        var dataString = 'uname=' + uname + '&uemail=' + uemail + '&uphonefull=' + uphonefull +'&utreatments='+utreatments+'&basicform='+basicform+'&form_name ='+form_name  + '&source=' + source;







        if (uname == '' || uemail == '' || uphone == '' || eml.test($.trim(uemail)) == false) {

            $(".wor-alert").stop().fadeIn('slow');
       return false;

        }else {
            // AJAX Code To Submit Form.
            $.ajax({
                type: "POST",
                url: "wp-content/themes/theme-name/forms/process.php",
                data: dataString,
                cache: false,
                success: function (response) {
                   $('#response').html(response);


                }
            });
        }





        return false;
    });


header_contact_froms function  :- 



function  header_contact_forms($source,$name,$email,$phone,$treatment,$form_name, $city,$hospital){


    include('mail.php');
if($city){include('thankyou.php');}



}

0 个答案:

没有答案