1

I use the same form on multiple pages within one website. Each page has a different category, so when someone send a question I want to know on which page the form was used. How to include the URL of the page on which the form was used? I want to modify just .php file, not all files on all pages.

Here is the code

    <?php

      $addressto = "info@x.co.uk";
      $subject = "Message from the website x.co.uk";
      $content = "First Name: ".$_POST['first-name']."\n"
                   ."Last Name: ".$_POST['last-name']."\n"
                   ."Company: ".$_POST['company']."\n"
                   ."Email: ".$_POST['email']."\n"      
                   ."Tel.: ".$_POST['phone']."\n"     
                   ."Event Date: ".$_POST['date']."\n"     
                   ."Location: ".$_POST['location']."\n"     
                   ."No of guests: ".$_POST['guests']."\n"     
                   ."Message: ".$_POST['message']."\n"
                   ."Referer: ".$_POST['findus']."\n";

        $email = $_POST['email'];
        if (mail($addressto, $subject, $content, 'From: <' . $email . '>')) {
            header("Location: message-sent.html");
        }

    ?>

Thank you

Piotr Ciszewski
  • 1,691
  • 4
  • 30
  • 53
  • 1
    I would either use sessions or AJAX. The amount of editing depends on how your application has been built, but you could make an include file to store the URL's in a session variable in PHP. – Martin Feb 09 '19 at 14:40
  • Thanks @Martin. Unfortunately pages are in HTML, not PHP – Piotr Ciszewski Feb 10 '19 at 13:16
  • I'm confused. The code you provided is in PHP. And surely, you're using some form of server side language regardless, since you're going to store the form data in a database? Else it doesn't make any sense. You could use cookies also. – Martin Feb 10 '19 at 13:23
  • @Martin, the code itself is on a separate script file, which the html page refers to using
    . There is no database and the only php in the whole website structure is a single file, which is the code I posted in my question.
    – Piotr Ciszewski Feb 10 '19 at 13:25
  • 1
    In that case you're not going to avoid having to edit your "html" files. You HAVE to parse the URL value in one way or another. Be it an extra PHP file you include at the top of each page that fetches the URL and stores it into a session variable, a hidden input field that contains the URL as a value that you post with your form or a cookie etc. etc. – Martin Feb 10 '19 at 13:31

2 Answers2

1

You can use a hidden field with value of current url

Geme
  • 354
  • 2
  • 3
  • 13
  • Thanks, I know that but I wanted to avoid changing forms on every website. I just want to change one php file. It's not a matter of my laziness but a lesson for future. – Piotr Ciszewski Feb 10 '19 at 10:01
0

Well, if you want to change only the included .phṕ file, without adding anything to all pages, you can use this code (described here):

$actual_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

NOTE: It is important to note that, as described in the link, a user can change the variables "HTTP_HOST" and "REQUEST_URI".

Lucisu
  • 23
  • 4