2

I have a html form on my index page and after the fields are been filled and submit button is clicked, the form action moves to a new page I named "indexform.php" but does not execute the php code on the "indexform.php" page.

Below is my html form code:

<form method="post" action="indexform.php">

<input name="name" type="text" class="form-control" placeholder="Your Name">

<input name="email" type="text" class="form-control" placeholder="Your Email">

<input name="subject" type="text" class="form-control" placeholder="Subject">

<input name="budget" type="text" class="form-control" placeholder="Your Budget">

<textarea name="message" id="" cols="30" rows="7" class="form-control" placeholder="What kind of content marketing services are you looking for?"></textarea>

<input type="submit" formaction="indexform.php" name="submit" value="Send Message">

</form>

Below is my php code on indexform.php page:

<?php

$mail_to_send_to = "joseph@digitage.net";
$from_email = "joseph@digitage.net";
$sendflag = $_REQUEST['sendflag'];
if ($sendflag == "send") {
    $email = $_REQUEST['email'];
    $message = $_REQUEST['message'];
    $name = $_REQUEST['name'];
    $budget = $_REQUEST['budget'];
    $headers = "From: $from_email" . "\r\n" . "Subject: $subject" . "\r\n" .
            "Reply-To: $email" . "\r\n";
    $a = mail($mail_to_send_to, "Message from a Home page contact form", $message, $headers);
    if ($a) {
        print("Message was sent, you can send another one");
    } else {
        print("Message wasn't sent, please check that you have changed emails in 
the bottom");
    }
}
?>

I don't get any message after the form action is been performed.

Rotimi
  • 4,783
  • 4
  • 18
  • 27

2 Answers2

0

You don't need the formaction="index.php" attribute in your button element. As long as you have the action="indexform.php" in your <form> tag, it will send the POST request to that URL.

<form method="post" action="indexform.php">
    <input name="name" type="text" class="form-control" placeholder="Your Name" />
    <input name="email" type="text" class="form-control" placeholder="Your Email" />
    <input name="subject" type="text" class="form-control" placeholder="Subject" />
    <input name="budget" type="text" class="form-control" placeholder="Your Budget" />
    <textarea name="message" cols="30" rows="7" class="form-control" placeholder="What kind of content marketing services are you looking for?"></textarea>
    <input type="submit" name="submit" value="Send Message" />
</form>

I noticed in your PHP, you're checking for the input field 'sendflag', but this has no input directive in your HTML, so really every time you check for 'sendflag', it will always be NULL. Also, you should really be using $_POST to fetch your submitted input fields instead of $_REQUEST. This is called "POST" because you're sending an HTTP POST request to the specified URL - you selected "POST" with your method="post" attribute.

Try this instead:

<?php

$mail_to_send_to = "joseph@digitage.net";
$from_email = "joseph@digitage.net";

if (!empty($_POST)) {
    $email = $_POST['email'];
    $message = $_POST['message'];
    $name = $_POST['name'];
    $budget = $_POST['budget'];
    $headers = "From: $from_email" . "\r\n" . "Subject: $subject" . "\r\n" .
        "Reply-To: $email" . "\r\n";
    $a = mail($mail_to_send_to, "Message from a Home page contact form", $message, $headers);
    if ($a) {
        echo "<p>Message was sent, you can send another one</p>";
    } else {
        echo "<p>Message wasn't sent, please check that you have changed emails in the bottom</p>";
    }
}
?>

A good way to check if any forms have been submitted is to use if (!empty($_POST)), using the empty() function.

If you wanted to, you could write a form with method="get", and then all of your input fields would appear in the URL like this:

indexform.php?name=my+name&email=me%40example.com&subject=My+Subject    ... etc.

And then of course you would retrieve them using the $_GET array. I know this is probably not what you want, I'm just explaining the difference between the two main HTTP requests.

And if you wanted, you could also use a <button> element as your form submitter, and it would render the same as your <input type="submit">.

<button>Send Message</button>
Tanner Babcock
  • 3,232
  • 6
  • 21
  • 23
-1

POST variables are accessed via $_POST superglobal not through $_REQUEST, change $_REQUEST to $_POST, this should fix the issue.

thisisayush
  • 292
  • 1
  • 8