Search code examples
phpsendmailforms

Passing a URL to PHP contact form


I am a bit of a PHP Noob, so this might be basic. I have a contact form in PHP/Bootstrap using this example blog
https://jonbake.com/blog/bootstrap-3-contact-form-with-captcha/

I am trying to amend the sendmail.php file to include the URL which the contact form resides on.

I.e. We have 3 or so different contact forms and would like to determine where the user sent the contact email from.

I could just copy the file three times and change the subject for an example which would be fine. But ideally I would like just one sendmail.php file and to pass in the URL the form/email is being sent from.

Question
How do I get the URL and pass that into this PHP file and amend to the email?

EDIT
AJAX is used to send the POST to sendmail.php if this makes a difference on how the link can be passed.

   //send the feedback e-mail
    $.ajax({
      type: "POST",
      url: "../assets/library/sendmail.php",
      data: $("#feedbackForm").serialize(),
      success: function(data)
      {
        contactForm.addAjaxMessage(data.message, false);
        //get new Captcha on success
        $('#captcha').attr('src', '../assets/library/vender/securimage/securimage_show.php?' + Math.random());
      },
      error: function(response)
      {
        contactForm.addAjaxMessage(response.responseJSON.message, true);
      }
   });
    return false;
  }); 

Thanks


Solution

  • First make sure your form holds a .php extension in order for the following to work: (consult Nota).

    <input type="hidden" name="the_link" value="<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; ?>">
    

    passing the current URL inside a hidden attribute of your form (which should be a POST method).

    Then using something like:

    $link = $_POST['the_link'];
    

    in your sendmail.php file.

    If you're using a GET method, simply change POST to GET in my answer.


    Nota: If your currently using an .html file, you can instruct Apache to treat .html files as PHP.

    Examples: (Pulled/borrowed from http://www.besthostratings.com/articles/php-in-html-files.html).

    For web servers using PHP as apache module:

    AddType application/x-httpd-php .html .htm
    

    For web servers running PHP as CGI:

    AddHandler application/x-httpd-php .html .htm 
    

    In case you wish to do the ASP mimick:

    For PHP as module:

    AddType application/x-httpd-php .asp
    

    OR

    For PHP as CGI:

    AddHandler application/x-httpd-php .asp
    

    and another Q&A on Stack on the subject: