php+js表单-提交后将页面url粘贴到电子邮件表单中

lmyy7pcs  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(335)

我有一个基本表单,在发送表单后,我需要将页面的url插入电子邮件中,以便知道用户在表单中填写的位置。
我尝试了在这个论坛上找到的更多选择,但不幸的是没有一个有效。
如何编辑代码以正确发送并将url添加到电子邮件中。
事先非常感谢您的帮助

$("#contactForm").validator().on("submit", function(event) {
  if (event.isDefaultPrevented()) {
    //handle the invalid form...
    formError();
    submitContactFormMSG(false, "Vyplňte prosím formulář správně!");
  } else {

    var filter = /^((\+[1-9]{1,4}[ \-]*)|(\([0-9]{2,3}\)[ \-]*)|([0-9]{2,4})[ \-]*)*?[0-9]{3,4}?[ \-]*[0-9]{3,4}?$/;

    if (filter.test($("#formphone").val())) {
      //everything looks good!
      event.preventDefault();
      submitcontactForm();
    } else {
      submitContactFormMSG(false, "Vložte prosím správný telefon!");
      return false;
    }
  }
});

function submitcontactForm() {
  //Variables With Form Content
  var formfname = $("#formfname").val();
  var formlname = $("#formlname").val();
  var formemail = $("#formemail").val();
  var formphone = $("#formphone").val();
  var forminfo = $("#forminfo").val();

  $.ajax({
    type: "POST",
    url: "/forms/kontaktni-formular.php",
    data: "&formfname=" + formfname + "&formlname=" + formlname + "&formemail=" + formemail + "&formphone=" + formphone + "&forminfo=" + forminfo,
    success: function(text) {
      if (text == "success") {
        contactFormSuccess();
      } else {
        formError();
        submitContactFormMSG(false, text);
      }
    }
  });
}

function contactFormSuccess() {
  $("#contactForm")[0].reset();
  submitContactFormMSG(true, "Vaše zpráva byla úspěšně odeslána :-)")
}

function formError() {
  $(".help-block.with-errors").removeClass('hidden');
}

function submitContactFormMSG(valid, msg) {
  if (valid) {
    var msgClasses = "h3 text-center col-12 text-success";
  } else {
    var msgClasses = "h3 text-center col-12 text-danger";
  }
  $("#msgContactFormSubmit").removeClass().addClass(msgClasses).text(msg);
  $('html,body').animate({
      scrollTop: $("#msgContactFormSubmit").offset().top - 80
    },
    'slow');
}
.form-control {
  border: 1px solid #99CC33;
  border-radius: 10px;
}

.has-error .form-control {
  border-color: #ff0000;
}

.help-block {
  margin: 0;
  color: #ff0000;
  font-size: 12px;
  position: absolute;
  top: 22px;
}

label:first-child {
  margin-bottom: 20px;
}

.form-group {
  position: relative;
}

.contact-form {
  width: 90%;
  margin-left: 5%;
}
<form id="contactForm" name="contactForm" data-toggle="validator" class="row" accept-charset='UTF-8'>
  <div id="msgContactFormSubmit" class="hidden"></div>
  <div class="col-sm-6">
    <div class="row pad">
      <div class="form-group col-sm-6">
        <label for="formfname"><strong>Jméno</strong><div class="help-block with-errors"></div></label>
        <input name="formfname" id="formfname" placeholder="YYY" class="form-control" type="text" required data-error="Vložte jméno">
      </div>
      <!-- end form-group -->
      <div class="form-group col-sm-6">
        <label for="formlname"><strong>Příjmení</strong><div class="help-block with-errors"></div></label>
        <input name="formlname" id="formlname" placeholder="XXX" class="form-control" type="text" required data-error="Vložte příjmení">
      </div>
      <!-- end form-group -->
      <div class="form-group col-sm-6">
        <label for="formemail"><strong>E-mailová adresa</strong><div class="help-block with-errors"></div></label>
        <input name="formemail" id="formemail" placeholder="xxx@yyy.cz" pattern=".*@\w{2,}\.\w{2,}" class="form-control" type="email" required data-error="Vložte odpovídající e-mail">
      </div>
      <!-- end form-group -->
      <div class="form-group col-sm-6">
        <label for="formphone"><strong>Telefonní číslo</strong><div class="help-block with-errors"></div></label>
        <input name="formphone" id="formphone" placeholder="+420 XXXXXXXXX" pattern="^\+?\d{9,16}" class="form-control" type="text" required data-error="Vložte správné telefonní číslo">
      </div>
      <!-- end form-group -->
    </div>
  </div>
  <div class="form-group col-sm-6">
    <label for="forminfo"><strong>Další informace</strong></label>
    <textarea class="form-control" placeholder="Informace" name="forminfo" id="forminfo" rows="5"></textarea>
  </div>
  <!-- end form-group -->
  <div class="form-group col-sm-12">
    <button type="submit" id="submit" class="btn btn-shutter-out-horizontal col-sm-6 offset-sm-3">ODESLAT</button>
  </div>
</form>

在php中,我有:

<?php
$errormsg = "";
if (empty($_POST["formfname"])) { $errormsg .= "Jméno je povinné. ";} else {$formfname = $_POST["formfname"];}
if (empty($_POST["formlname"])) {$errormsg .= "Příjmení je povinné. ";} else {$formlname = $_POST["formlname"];}
if (empty($_POST["formemail"])) {$errormsg .= "Email je povinný. ";} else {$formemail = $_POST["formemail"];}
if (empty($_POST["formphone"])) {$errormsg .= "Telefonní číslo je povinné ";} else {$formphone = $_POST["formphone"];}

$success = '';
$body_message = '';
if (!$errormsg){
    $headers =  'From: '. $formfname .' '. $formlname .' <'. $email .'>' . "\r\n" .
                'Reply-To: ' . $email . "\r\n" .
                'X-Mailer: PHP/' . phpversion() . "\r\n" .
                'Content-type: text/html; windows-1250' ;
    $to = "xxx@yyy.cz";
    $subject = "Kontaktní formulář";

    //telo mailu
    $body_message .= "<strong>Jméno:</strong> " . $formfname."<br>\n";
    $body_message .= "<strong>Přímení:</strong> " . $formlname."<br>\n";
    $body_message .= "<strong>E-mailová adresa:</strong> " . $formemail."<br>\n";
    $body_message .= "<strong>Telefon:</strong> " . $formphone."<br>\n";
    $body_message .= "<strong>Další informace:</strong> " . $forminfo."<br>\n";

    $success = @mail($to, $subject, $body_message, $headers);
}

if ($success){
   echo "success";
}else{
    echo "Něco se pokazilo: " . $errormsg;
}?>
b4wnujal

b4wnujal1#

您可能正在寻找 $_SERVER['HTTP_HOST']$_SERVER['REQUEST_URI'] 在php中。假设你的地址是 https://example.com/foo.php?id=1 . 然后 HTTP_HOST 返回 example.comREQUEST_URI 返回 /bar.php?id=1 . 有两种选择:
第一个选项,使用php。但是,只有当php代码与表单位于同一站点时,这才有效。它可能是这样的:

$body_message .= "<strong>Site:</strong> ".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."<br>\n";

而不是使用url,你可以简单地使用任何你想要的可能更可读的东西。比如:

$body_message .= "<strong>Site:</strong> Get more information about us<br>\n";

第二个选项,使用隐藏表单字段。

<?php
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo '<input type="hidden" name="formurl" id="formurl" value="'.$url.'">';

显然,您还必须将新表单字段添加到javascript中:

var formurl= $("#formurl").val();
//other code...
data: "&formfname=" + formfname + "&formlname=" + formlname + "&formemail=" + formemail + "&formphone=" + formphone + "&forminfo=" + forminfo + "&formurl=" + formurl,
//other code...

编辑:您必须编辑php文件。否则脚本不知道还有另一个表单字段。只剩下两行了:

<?php
//There's a new form field, so tell PHP it's there. No $errormsg is needed because it has nothing to do with the user input
if (empty($_POST["formurl"])) {$formurl = "no url given";} else {$formurl= $_POST["formurl"];}

//other code...

//Then append the URL to the message
$body_message .= "<strong>Site:</strong> ".$formurl."<br>\n";

也就是说,我总是更喜欢第一种选择而不是第二种。与所有输入字段一样,用户可以轻松地操纵隐藏表单字段的值。因此,即使他是从example.com/contact.php发送的,在邮件中,它似乎是从example.com/thisurldoesnotexist.php发送的。第二个解决方案可以是一个选项,例如,php代码位于与html表单不同的站点上。

相关问题