title: PHP+JS Form - Paste page URL into the email form after submitting
body: <p>I have a basic form and after sending the form I need to insert the URL of the page into the e-mail so that I know where the user filled in the form.</p>
  <p>I tried more options that I found in this forum, but unfortunately none of them worked.</p>
  <p>How do I edit the code to send it correctly and add the URL to the email.</p>
  <p>Thank you very much in advance for your help</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$("#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: "&amp;formfname=" + formfname + "&amp;formlname=" + formlname + "&amp;formemail=" + formemail + "&amp;formphone=" + formphone + "&amp;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');
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.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%;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form id="contactForm" name="contactForm" data-toggle="validator" class="row" accept-charset='UTF-8'&gt;
    &lt;div id="msgContactFormSubmit" class="hidden"&gt;&lt;/div&gt;
    &lt;div class="col-sm-6"&gt;
      &lt;div class="row pad"&gt;
        &lt;div class="form-group col-sm-6"&gt;
          &lt;label for="formfname"&gt;&lt;strong&gt;Jméno&lt;/strong&gt;&lt;div class="help-block with-errors"&gt;&lt;/div&gt;&lt;/label&gt;
          &lt;input name="formfname" id="formfname" placeholder="YYY" class="form-control" type="text" required data-error="Vložte jméno"&gt;
        &lt;/div&gt;
        &lt;!-- end form-group --&gt;
        &lt;div class="form-group col-sm-6"&gt;
          &lt;label for="formlname"&gt;&lt;strong&gt;Příjmení&lt;/strong&gt;&lt;div class="help-block with-errors"&gt;&lt;/div&gt;&lt;/label&gt;
          &lt;input name="formlname" id="formlname" placeholder="XXX" class="form-control" type="text" required data-error="Vložte příjmení"&gt;
        &lt;/div&gt;
        &lt;!-- end form-group --&gt;
        &lt;div class="form-group col-sm-6"&gt;
          &lt;label for="formemail"&gt;&lt;strong&gt;E-mailová adresa&lt;/strong&gt;&lt;div class="help-block with-errors"&gt;&lt;/div&gt;&lt;/label&gt;
          &lt;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"&gt;
        &lt;/div&gt;
        &lt;!-- end form-group --&gt;
        &lt;div class="form-group col-sm-6"&gt;
          &lt;label for="formphone"&gt;&lt;strong&gt;Telefonní číslo&lt;/strong&gt;&lt;div class="help-block with-errors"&gt;&lt;/div&gt;&lt;/label&gt;
          &lt;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"&gt;
        &lt;/div&gt;
        &lt;!-- end form-group --&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="form-group col-sm-6"&gt;
      &lt;label for="forminfo"&gt;&lt;strong&gt;Další informace&lt;/strong&gt;&lt;/label&gt;
      &lt;textarea class="form-control" placeholder="Informace" name="forminfo" id="forminfo" rows="5"&gt;&lt;/textarea&gt;
    &lt;/div&gt;
    &lt;!-- end form-group --&gt;
    &lt;div class="form-group col-sm-12"&gt;
      &lt;button type="submit" id="submit" class="btn btn-shutter-out-horizontal col-sm-6 offset-sm-3"&gt;ODESLAT&lt;/button&gt;
    &lt;/div&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>In PHP I have this:</p>
  <pre><code>&lt;?php
  $errormsg = &quot;&quot;;
  if (empty($_POST[&quot;formfname&quot;])) { $errormsg .= &quot;Jméno je povinné. &quot;;} else {$formfname = $_POST[&quot;formfname&quot;];}
  if (empty($_POST[&quot;formlname&quot;])) {$errormsg .= &quot;Příjmení je povinné. &quot;;} else {$formlname = $_POST[&quot;formlname&quot;];}
  if (empty($_POST[&quot;formemail&quot;])) {$errormsg .= &quot;Email je povinný. &quot;;} else {$formemail = $_POST[&quot;formemail&quot;];}
  if (empty($_POST[&quot;formphone&quot;])) {$errormsg .= &quot;Telefonní číslo je povinné &quot;;} else {$formphone = $_POST[&quot;formphone&quot;];}
  
  $success = '';
  $body_message = '';
  if (!$errormsg){
      $headers =  'From: '. $formfname .' '. $formlname .' &lt;'. $email .'&gt;' . &quot;\r\n&quot; .
                  'Reply-To: ' . $email . &quot;\r\n&quot; .
                  'X-Mailer: PHP/' . phpversion() . &quot;\r\n&quot; .
                  'Content-type: text/html; windows-1250' ;
      $to = &quot;xxx@yyy.cz&quot;;
      $subject = &quot;Kontaktní formulář&quot;;
  
      //telo mailu
      $body_message .= &quot;&lt;strong&gt;Jméno:&lt;/strong&gt; &quot; . $formfname.&quot;&lt;br&gt;\n&quot;;
      $body_message .= &quot;&lt;strong&gt;Přímení:&lt;/strong&gt; &quot; . $formlname.&quot;&lt;br&gt;\n&quot;;
      $body_message .= &quot;&lt;strong&gt;E-mailová adresa:&lt;/strong&gt; &quot; . $formemail.&quot;&lt;br&gt;\n&quot;;
      $body_message .= &quot;&lt;strong&gt;Telefon:&lt;/strong&gt; &quot; . $formphone.&quot;&lt;br&gt;\n&quot;;
      $body_message .= &quot;&lt;strong&gt;Další informace:&lt;/strong&gt; &quot; . $forminfo.&quot;&lt;br&gt;\n&quot;;
  
      $success = @mail($to, $subject, $body_message, $headers);
  }
  
  if ($success){
     echo &quot;success&quot;;
  }else{
      echo &quot;Něco se pokazilo: &quot; . $errormsg;
  }?&gt;
  </code></pre>