1

How to set the mail subject to "Certain text" + name from form input

<a href="mailto:someone@yoursite.com?subject=Testing+(Name)">Email Us</a> 

This is my code

<form action="/action_page.php" target="_blank">
        <input type="text" placeholder="Name" required name="Name">
        <input type="text" placeholder="Email" required name="Email">
        <input type="text" placeholder="Contact" required name="Contact">
        <input type="text" placeholder="Message" required name="Message">
    <button type="submit">
      <a href="mailto:someone@yoursite.com?subject=Testing+(Name)">Email Us</a> 
    </button>
  </form>

For e.g. The user input Name as "Cyrus", then my mailto subject should be Testing(Cyrus).

moshimoshi
  • 51
  • 6
  • Possible duplicate of [Can I set subject/content of email using mailto:?](https://stackoverflow.com/questions/4782068/can-i-set-subject-content-of-email-using-mailto) – Abhishek Pandey Feb 19 '19 at 07:35
  • I think you should be looking into using JavaScript to update the DOM when the user edits the 'Name' input element. – Michael Murphy Feb 19 '19 at 08:52

1 Answers1

1

you can use this php page I have named demo-contacts.php

<?php
if( isset($_POST['name']) )
{
    $to = 'youremail@yoursite.com'; // Replace with your email

    $subject = $_POST['subject'];
    $message = $_POST['message'] . "\n\n" . 'Regards, ' . $_POST['name'] . ' - ' . $_POST['email'] . '.';
    $headers = 'From: ' . $_POST['name'] . "\r\n" . 'Reply-To: ' . $_POST['email'] . "\r\n" . 'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);

    if( $_POST['copy'] == 'on' )
    {
        mail($_POST['email'], $subject, $message, $headers);
    }
}
?>

and this form on a separate page named contact.php

<form action="demo-contacts.php" method="post" id="sky-form" class="sky-form">
          <header>Contact <strong>Form</strong></header>
          <fieldset>
            <div class="row">
              <section class="col col-6">
                <label class="label">Name</label>
                <label class="input"> <i class="icon-append icon-user"></i>
                  <input type="text" name="name" id="name">
                </label>
              </section>
              <section class="col col-6">
                <label class="label">E-mail</label>
                <label class="input"> <i class="icon-append icon-envelope-alt"></i>
                  <input type="email" name="email" id="email">
                </label>
              </section>
            </div>
            <section>
              <label class="label">Subject</label>
              <label class="input"> <i class="icon-append icon-tag"></i>
                <input type="text" name="subject" id="subject">
              </label>
            </section>
            <section>
              <label class="label">Message</label>
              <label class="textarea"> <i class="icon-append icon-comment"></i>
                <textarea rows="4" name="message" id="message"></textarea>
              </label>
            </section>
            <section>
              <label class="checkbox">
                <input type="checkbox" name="copy" id="copy">
                <i></i>Send a copy to my e-mail address</label>
            </section>
          </fieldset>
          <footer>
            <button type="submit" class="button">Send message</button>
          </footer>
          <div class="message"> <i class="icon-ok"></i>
            <p>Your message was successfully sent!</p>
          </div>
        </form>

I also used this stylesheet for the form which you can feel free to download from my website at this link http://www.dewetcomputers.com/js/form/sky-forms.css

And this minified Jquery which you can download http://www.dewetcomputers.com/js/form/jquery.form.min.js

Also if you would like to see what the form looks like you can see it working here http://www.dewetcomputers.com/contact.php

<link rel="stylesheet" href="js/form/sky-forms.css" type="text/css" media="all">

Feel free to ask any other questions or if you run into any other problems

Brendan
  • 119
  • 11
  • Sorry I didn't understand the question I didn't realize he wanted to change the form dynamically. to dynamically change the email you will need to use php and html so that you can save the user input into variables which you will access when creating your email. I will edit my answer to include the php and html to achieve this – Brendan Feb 25 '19 at 23:53