Jump to content

Why isn't my form sending?


jazza96

Recommended Posts

Hi

I am trying to build a contact form and my form isnt sending. 

Here is my code. 

(its in a modal window)(I have my email address in the $to variable in the code)

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Jarrod';
    $to = 'email';
    $subject = "Landing page";
 
    $body = "From: $name\n Email: $email\n Message:\n $message";
    ?>
 
    <?php
      if ($_POST['submit']){
        if(mail ($to, $subject, $body, $from)) {
          echo '<p>Your message has been sent!</p>';
        }else{
          echo '<p>Something went wrong, go back!</p>';
        }
      }
    ?>
 
 
 
    <div class="modal fade" id="contact" role="dialog">
      <div class=" modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h2>Contact us</h2>
          </div>
          <div class="modal-body">
            <form method="post" action="index1.php">
            <div class="form-horizontal">
              <label for="Name">Your Name</label>
              <input type="text" class="form-control" id="name" placeholder="Name">
 
              <label for="Email">Your Email</label>
              <input type="Email" class="form-control" id="email" placeholder="Email">
 
              <label for="Message">Message</label>
              <textarea class="form-control" id="message" placeholder="Message"></textarea>  
  
              <input type="submit" value="Send">Send</button> 
            </form>
          
 
          <div class="modal-footer">
            <a class="btn btn-default" data-dismiss = "modal">Close</a>
          </div>
        </div>
      </div>
    </div>
Link to comment
Share on other sites

I don't see an action set on your form, that might be something to look at. An action is the url/address that the form is sending too.

 

example: 

<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>
Link to comment
Share on other sites

 

I don't see an action set on your form, that might be something to look at. An action is the url/address that the form is sending too.

 

example: 

<form action="action_page.php">
First name:<br>
<input type="text" name="firstname" value="Mickey">
<br>
Last name:<br>
<input type="text" name="lastname" value="Mouse">
<br><br>
<input type="submit" value="Submit">
</form>

I have added an action to the form. 

 

<div class="modal-body">
            <form method="post" action="index.php">
            <div class="form-horizontal">
              <label for="Name">Your Name</label>
              <input type="text" class="form-control" id="Name" placeholder="Name">
 
              <label for="Email">Your Email</label>
              <input type="Email" class="form-control" id="Email" placeholder="Email">
 
              <label for="Message">Message</label>
              <textarea class="form-control" id="Message" placeholder="Message"></textarea>  
  
              <button type="button" class="btn btn-primary">Send</button> 
            </form>
          </div>
          </div>
 
The php code is on the index.php file. 
Link to comment
Share on other sites

Add names to your form fields not id (Otherwise they will be blank)

also to the submit button

<input type="text" class="form-control" name="name" placeholder="Name">
 
              <label for="Email">Your Email</label>
              <input type="Email" class="form-control" name="email" placeholder="Email">
 
              <label for="Message">Message</label>
              <textarea class="form-control" name="message" placeholder="Message"></textarea>  
  
              <input type="submit" name="submit" value="Send">Send</button>
Link to comment
Share on other sites

 

Add names to your form fields not id (Otherwise they will be blank)

also to the submit button

<input type="text" class="form-control" name="name" placeholder="Name">
 
              <label for="Email">Your Email</label>
              <input type="Email" class="form-control" name="email" placeholder="Email">
 
              <label for="Message">Message</label>
              <textarea class="form-control" name="message" placeholder="Message"></textarea>  
  
              <input type="submit" name="submit" value="Send">Send</button>

Tried it and it didn't work unfortunately :(

Link to comment
Share on other sites

Do you have error reporting turned on? It looks like you're attempting to assign values in $_POST to variables before you're actually checking to see if the form has been submitted. That could be throwing you off.

 

I am getting 4 undefined errors.

 

Notice: Undefined index: name in /Applications/MAMP/htdocs/Jumbotron/index.php on line 124

 

Notice: Undefined index: email in /Applications/MAMP/htdocs/Jumbotron/index.php on line 125

 

Notice: Undefined index: message in /Applications/MAMP/htdocs/Jumbotron/index.php on line 126

 

Notice: Undefined index: submit in /Applications/MAMP/htdocs/Jumbotron/index.php on line 135

Link to comment
Share on other sites

I am getting 4 undefined errors.

 

Do you get the error messages after the form submits?

 

Note: to avoid seeing the errors before the form is submitted, move the initial variable declarations inside the construct that tests whether the form was submitted:

<?php
if ($_POST['submit']){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Jarrod';
    $to = 'email';
    $subject = "Landing page";
 
    $body = "From: $name\n Email: $email\n Message:\n $message";
 
    if(mail ($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent!</p>';
    }else{
        echo '<p>Something went wrong, go back!</p>';
    }
}
?>
Link to comment
Share on other sites

Do you get the error messages after the form submits?

 

Note: to avoid seeing the errors before the form is submitted, move the initial variable declarations inside the construct that tests whether the form was submitted:

<?php
if ($_POST['submit']){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Jarrod';
    $to = 'email';
    $subject = "Landing page";
 
    $body = "From: $name\n Email: $email\n Message:\n $message";
 
    if(mail ($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent!</p>';
    }else{
        echo '<p>Something went wrong, go back!</p>';
    }
}
?>

 

Do you get the error messages after the form submits?

 

Note: to avoid seeing the errors before the form is submitted, move the initial variable declarations inside the construct that tests whether the form was submitted:

<?php
if ($_POST['submit']){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Jarrod';
    $to = 'email';
    $subject = "Landing page";
 
    $body = "From: $name\n Email: $email\n Message:\n $message";
 
    if(mail ($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent!</p>';
    }else{
        echo '<p>Something went wrong, go back!</p>';
    }
}
?>

 

The errors are appearing when I refresh the page. 

 

I put in that code and I am no longer getting this error: 

Notice: Undefined index: submit in /Applications/MAMP/htdocs/Jumbotron/index.php on line 135

 

but I am still getting the other error's

Notice: Undefined index: name in /Applications/MAMP/htdocs/Jumbotron/index.php on line 124

Notice: Undefined index: email in /Applications/MAMP/htdocs/Jumbotron/index.php on line 125

Notice: Undefined index: message in /Applications/MAMP/htdocs/Jumbotron/index.php on line 126

Edited by jazza96
Link to comment
Share on other sites

Side note: the id attribute in your original code is used to connect the form label with the corresponding field. 

<label for="Name">Your Name</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name">
 
Of course, the <label> tag wasn't successfully connected since the for attribute doesn't match the id attribute. One was set to "Name" and the other is set to "name"...note the case difference.
 
 
With that said, you could just surround the input field with the label tag:
<label>Your Name
<input type="text" class="form-control" name="name" placeholder="Name"></label>
 
 
More information about the <label> tag can be found here:
Link to comment
Share on other sites

 

Ah, it looks like you need to name the submit button also. Try changing this

<input type="submit" value="Send">Send</button>
 
To this
<input type="submit" name="submit" value="Send">
 
 
 

The name attribute tells PHP to create the POST variable used here:

if($_POST['submit']) {

It worked!! Thanks :)

One other problem is that the message is not sending now to my nominated email address. 

Link to comment
Share on other sites

$from should contain an actual email address on your server as well, or at least the correct domain. There are a lot of spam filters out there that won't accept just "From: Jarrod" where "From: email@this_site.com" would work, as long as "this_site.com" is the actual site name sending the email.

Link to comment
Share on other sites

Have you sent email from this site successfully in the past?

 

Might want to post the updated code you are using now

No, I have not. It is just on my localhost server (MAMP). 

 

<?php
 
    error_reporting(-1);
ini_set('display_errors', 'On');
 
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'email';
    $to = 'email';    /* I have my different email in these fields. */
    $subject = "Landing page";
 
    $body = "From: $name\n email: $email\n message:\n $message";
    ?>
 
    <?php 
    
      if(mail ($to, $subject, $body, $from)) {
        echo '<p>Your message has been sent</p>';
    }else{
        echo '<p>Something went wrong, go back!</p>';
    }
      
    ?>
 
 
 
    <div class="modal fade" id="contact" role="dialog">
      <div class=" modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h2>Contact us</h2>
            <p>Do you have a question, suggestion or comment?</p>
          </div>
          <div class="modal-body">
            <form method="post" action="index.php">
            <div class="form-horizontal">
              <label for="Name">Your Name</label>
              <input type="text" class="form-control" name="name" placeholder="Name">
 
              <label for="Email">Your Email</label>
              <input type="Email" class="form-control" name="email" placeholder="Email">
 
              <label for="Message">Message</label>
              <textarea class="form-control" name="message" placeholder="Message"></textarea>  
  
              <input type="submit" name="submit" value="Send" action="index.php"></input>
            </form>
          </div>
          </div>
 
          <div class="modal-footer">
            <a class="btn btn-default" data-dismiss = "modal">Close</a>
          </div>
      </div>
    </div>
Link to comment
Share on other sites

$from should contain an actual email address on your server as well, or at least the correct domain. There are a lot of spam filters out there that won't accept just "From: Jarrod" where "From: email@this_site.com" would work, as long as "this_site.com" is the actual site name sending the email.

Yes, I put an email address in the $from variable and it still didn't work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.