简体   繁体   中英

form action not triggering instead redirecting to php script

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@example.com"; // 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:" . "\n\n" . $_POST['message'];
  $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_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

在此处输入图像描述

Im running this in localhost ubuntu apache server.

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@example.com"; // 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:" . "\n\n" . $_POST['message'];
  $message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_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');
}
?>

There isn't a variable named 'submit' in your form. If you want to check if the form was submitted, just check the variable that you post, in your case, remove the code below

if(!isset($_POST['submit']))
{
    //This page should not be accessed directly. Need to submit the form.
    echo "error; you need to submit the form!";
}

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.

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

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