1

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

vicente
  • 2,613
  • 4
  • 22
  • 27
StuartM
  • 6,743
  • 18
  • 84
  • 160
  • 1
    `$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";` borrowed from this Q&A http://stackoverflow.com/q/6768793/ – Funk Forty Niner Dec 18 '14 at 18:02
  • @Fred-ii- Will this not return the full URL to the actual sendmail.php file as opposed to the contact form URL which is what I am looking for? If I add this to the contact form page, how do I pass it to the sendmail.php one? – StuartM Dec 18 '14 at 18:11
  • @Fred-ii- Just to confirm, yes this returns the link of sendmail php file. So how do I include this in the contact form and pass it over to sendmail to be used in the email? – StuartM Dec 18 '14 at 18:13
  • You could use sessions if passing to more than 2 post calls. Just assign a session variable to it. – Funk Forty Niner Dec 18 '14 at 18:13
  • You could also use a hidden attribute in the form itself. – Funk Forty Niner Dec 18 '14 at 18:17
  • I have updated the question with the JS for the call/POST to sendmail.php if this helps. Can you provide an answer with how to pass the link to that file please? – StuartM Dec 18 '14 at 18:17
  • Ajax is not my strong point. I tried opening the link you gave, but FF spat out *This Connection is Untrusted*, so I won't be able to fetch anything from there. Simply put, add a hidden attribute in your form `` then in your sendmail.php file, add `$link = $_POST['the_link'];` which will capture the current page it was taken from. You may need to play around with `http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]` a bit, removing the `http://`. It shouldn't be there, made a slight error ;) – Funk Forty Niner Dec 18 '14 at 18:28
  • Yes the cert on that site run out recently, but you can bypass that if need be. I was trying to do that, but I cannot get the syntax right for passing the link in as the input value. The above does not work, the page will not load. – StuartM Dec 18 '14 at 18:29
  • I edited my comment/code above which should have read as `` or `$_SERVER['HTTP_HOST']$_SERVER['REQUEST_URI']` - give me a minute, I will test it. Make sure your form is `.php` for it to work with this. BRB. – Funk Forty Niner Dec 18 '14 at 18:30
  • Ok, I got it, please submit as an answer and I will accept – StuartM Dec 18 '14 at 18:32
  • `">` that worked for me. – Funk Forty Niner Dec 18 '14 at 18:34
  • It has been posted Stuart, *cheers* – Funk Forty Niner Dec 18 '14 at 18:36

2 Answers2

2

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:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
1

You can set a flag in each contact form and process the email's body accordingly:

contact_form_1.html

<form>
    <!-- code -->
    <input name="referer" type="hidden" value="contact_form_1">
    <!-- code -->
</form>

sendmail.php

<?php
    // code
    $mailbody.="Referer: ".$_POST["referer"];
    // code
?>
kos
  • 510
  • 3
  • 18