簡體   English   中英

一頁上有多個聯系表

[英]Multiple contact forms on one page

我目前正在創建一個虛擬站點,並希望在一頁上創建多個表單。

因此,當您單擊每個服務時,您可以發送表格以請求報價。 我得到的第一個表格工作正常,但是,當我發送第二個表格時,它只是發送第一個表格中的數據?

對於第二種形式,我將ID替換為contactform2,而不是如下所示的contactform。


  <div id="registerpremium" class="modal fade">
    <div class="modal-dialog modal-lg" role="document">
      <div class="modal-content">
        <div class="modal-header col-md-12">
          <img class=" logo_h modallogo" src="img/logo.svg">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
          </button>
        </div>
  <div class="modal-body signupmessage">
        <img src="img/marketing.svg" alt="" class="img-fluid modalicon">
        <h3 class="col-md-12  formtext py-4">eCommerce Premium</h3>
        <p class="text-center">Please fill in the form below, we will then send you an email to confirm where to send your products.</p>
          <form id="contactform" method="post" name="contactform"  onsubmit="return false;">
            <div class="form-group pt-3">
              <label class="sr-only pt-5" for="name">Name</label>
              <input type="text" class="form-control form-control-danger form-control-lg" name="name" id="Name" placeholder="Name*">
            </div>
            <div class="form-group">
              <label class="sr-only" for="mail">Email</label>
              <input type="email" class="form-control form-control form-control-lg" name="email" id="mail" placeholder="Email*">
            </div>
             <div class="form-group">
              <label class="sr-only" for="mail">Phone Number</label>
              <input type="phone" class="form-control form-control form-control-lg" name="phone" id="phone" placeholder="Phone Number">
            </div>
            <div class="row">
            <div class="col-md-12">
            <div class="form-group">
                <input type="checkbox" id="gdpr" name="gdpr" class="form-control md-textarea"></textarea>
                <label for="gdpr" name="gdprlabel">I have read and consent to my data being collected as outlined in the <a href="test" target="_blank" class="disclaimerLink">Disclaimer</a></label>
            </div>
            </div>
          </div>
            <div class="modal-footer py-4">
              <button type="submit" class="button button-header bg">Submit</button>
            </div>
          </form>
          </div>
        </div>
      </div>
  </div>

對於第二種形式,我只是將ID替換為contactform2

      $('#contactform2').validate({
        rules: {
          name: {
            required: true
          },
            email: {
                required: true,
                email: true
            },
            gdpr: {
            required: true
          }

        },
        errorPlacement: function(){
              return false;
          },
        submitHandler: function(){
          var contactform = $('#contactform2').serialize();
              $.ajax({
                type: "POST",
                url: 'ajax.php',
                data: { 
                    contactform: contactform
                },
                success: function(data){
                  $('#contactform2').hide();
                  $('.signupmessage').html('<p id="successmessage">' + data + '</p>');
                },
                error: function(xhr,textStatus,err){
                  console.log("readyState: " + xhr.readyState);
                  console.log("responseText: "+ xhr.responseText);
                  console.log("status: " + xhr.status);
                  console.log("text status: " + textStatus);
                  console.log("error: " + err);
                }
              });
        }
      });

如果刪除第二個聯系表單,PHP將用於第二個表單,這就是我在第一個表單中使用的表單。 我知道第一個表格發送的原因是因為我在主題,標題等中使用了test而不是test2。


<?php 

        if (isset($_POST['contactform2'])) {
            parse_str($_POST['contactform2'], $contactformData);

            $name = $contactform2Data['name'];
            $email = $contactform2Data['email'];
            $phone = $contactform2Data['phone'];
            $to  = 'hello@samuelrhys.com'; //change to desired recepient 
            $subject = "test2";
            $message = "test2". "\n" .$name. "\n" .$email. "\n" .$phone. "\n" .$message;
            $headers = "test2" . "\r\n"; //change domain to that of Poppyfields
            $result = mail($to, $subject, $message, $headers);
            if ($result) {
                echo "Thanks for contacting us, we'll be in touch soon!";
            } else {
                echo "Uh oh. We couldn't deliver your request at this time.";
            }

        }

?>

我的第二個表格html


  <div id="registerstandard" class="modal fade">
    <div class="modal-dialog modal-lg" role="document">
      <div class="modal-content">
        <div class="modal-header col-md-12">
          <img class=" logo_h modallogo" src="img/logo.svg">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
          </button>
        </div>
  <div class="modal-body signupmessage">
        <img src="img/marketing.svg" alt="" class="img-fluid modalicon">
        <h3 class="col-md-12  formtext py-4">eCommerce Standard</h3>
        <p class="text-center">Please fill in the form below, we will then send you an email to confirm where to send your products.</p>
          <form id="contactform2" method="post" name="contactform2"  onsubmit="return false;">
            <div class="form-group pt-3">
              <label class="sr-only pt-5" for="name">Name</label>
              <input type="text" class="form-control form-control-danger form-control-lg" name="name" id="Name" placeholder="Name*">
            </div>
            <div class="form-group">
              <label class="sr-only" for="mail">Email</label>
              <input type="email" class="form-control form-control form-control-lg" name="email" id="mail" placeholder="Email*">
            </div>
             <div class="form-group">
              <label class="sr-only" for="mail">Phone Number</label>
              <input type="phone" class="form-control form-control form-control-lg" name="phone" id="phone" placeholder="Phone Number">
            </div>
            <div class="row">
            <div class="col-md-12">
            <div class="form-group">
                <input type="checkbox" id="gdpr" name="gdpr" class="form-control md-textarea"></textarea>
                <label for="gdpr" name="gdprlabel">I have read and consent to my data being collected as outlined in the <a href="http://www.hopkinsons.net/privacy-policy" target="_blank" class="disclaimerLink">Disclaimer</a></label>
            </div>
            </div>
          </div>
            <div class="modal-footer py-4">
              <button type="submit" class="button button-header bg">Submit</button>
            </div>
          </form>

          </div>
        </div>
      </div>
  </div>

您需要在HTML中更改name標簽的值,以便從PHP訪問訪問請求!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM