简体   繁体   中英

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.

Ie 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

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:

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
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM