skip to Main Content

what i’m trying to do is get the submitted name, email and message to my php script then send email message. The problem is my form action doesn’t trigger instead it reloads the page.

UPDATE
Don’t know what im missing from my html form here:

<form method="post" action="">
    <div class="input-group">
        <input type="text" id="name" name="name" class="input-demo" placeholder="Your Name">
        <span id="invalid-name">
            Please enter at least 2 chars
        </span>
    </div>
    <div class="input-group">
        <input id="email" type="email" name="email" class="input-demo" placeholder="Email Address">
        <span id="invalid-email">
            Please enter valid email
        </span>
    </div>
    <div class="input-group">
        <textarea id="message" name="message" placeholder="Message">
        </textarea>
        <span id="invalid-message">
            Please write something for us
        </span>
    </div>
    <div>
    </div>
    <input type="submit" name="submit" value="Book a Demo">
</form>

UPDATE
php script first get values and contruct email message then finally send:

<?php
if (isset($_POST['submit'])) {
  include 'index.php';
  $to = "[email protected]"; // this is your Email address
  $from = $_POST['email']; // this is the sender's Email address
  $name = $_POST['name'];
  $subject = "Form submission";
  $subject2 = "Copy of your form submission";
  $message = $name . " wrote the following:" . "nn" . $_POST['message'];
  $message2 = "Here is a copy of your message " . $first_name . "nn" . $_POST['message'];

  $headers = "From:" . $from;
  $headers2 = "From:" . $to;
  mail($to, $subject, $message, $headers);
  mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
  echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
  // You can also use header('Location: thank_you.php'); to redirect to another page.
} else {
  echo 'isset was false';
}
?>

Here’s my folder structure

enter image description here

Im running this in localhost ubuntu apache server.

3

Answers


  1. You need to change field name from ‘name’ to any other word..

    Try below code:

    <input type="text" id="name" name="full_name" class="input-demo" placeholder="Your Name">

    Thanks

    Login or Signup to reply.
  2. The action attribute of the form element you pasted is not set:

    <form method="post" action="">
    

    With this attribute unset or empty, the form will submit to the current page because the browser has no way of knowing where to submit it to.
    Assuming that form-to-email.php is the script to process, it should look like the following:

    <form method="post" action="form-to-email.php">
    

    Alternatively, you can access the PHP code in the HTML page by renaming index.html to index.php (PHP code in .html files won’t be executed) and executing the PHP script from there, like so:

    <?php
    include "form-to-email.php";
    ?>
    

    This should allow you to leave the action attribute blank and still have a functional form.

    Login or Signup to reply.
  3. your HTML is fine for one small thing, you didn’t specify where to send the form after hitting submit. You do this by specifying the PHP script within the action parameter in <form>

    <form method="post" action="form-to-email.php">
        <div class="input-group">
            <input type="text" id="name" name="name" class="input-demo" placeholder="Your Name">
            <span id="invalid-name">
                Please enter at least 2 chars
            </span>
        </div>
        <div class="input-group">
            <input id="email" type="email" name="email" class="input-demo" placeholder="Email Address">
            <span id="invalid-email">
                Please enter valid email
            </span>
        </div>
        <div class="input-group">
            <textarea id="message" name="message" placeholder="Message">
            </textarea>
            <span id="invalid-message">
                Please write something for us
            </span>
        </div>
        <div>
        </div>
        <input type="submit" name="submit" value="Book a Demo">
    </form>
    

    After reading your questions on someone elses answer I finally find out what you want, you can stop the site from staying on your phpscript page, by adding a header(Location: index.html) at the end of the script, so when it completed the registration or when it failed you send it back towards index. Alternatively you can include the PHP inside your index file but you will have to change your index from .html to .php

    Example with redirect in your php script

    <?php
    if (isset($_POST['submit'])) {
      include 'index.php';
      $to = "[email protected]"; // this is your Email address
      $from = $_POST['email']; // this is the sender's Email address
      $name = $_POST['name'];
      $subject = "Form submission";
      $subject2 = "Copy of your form submission";
      $message = $name . " wrote the following:" . "nn" . $_POST['message'];
      $message2 = "Here is a copy of your message " . $first_name . "nn" . $_POST['message'];
    
      $headers = "From:" . $from;
      $headers2 = "From:" . $to;
      mail($to, $subject, $message, $headers);
      mail($from, $subject2, $message2, $headers2); // sends a copy of the message to the sender
      echo "Mail Sent. Thank you " . $name . ", we will contact you shortly.";
      header('Location: index.html');
    } else {
      echo 'isset was false';
      header('Location: index.html');
    }
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search