Submit Button not Sending

Hi folks.
This is my 1st attempt at making a form. Unfortunately I’m struggling to get it working.
The form itself works great, just not the send part.
I’ve tried to follow other forms from other forums as best I can, but I’m missing something.
I’ve messed about with:

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

Here I’ve seperated the forms.
I did have
<form action="contactform" method="post">

I’ve added and removed the PHP tags. I’ve also played around with by moving them to a number of locations.

The code I have at the moment is:

   <div>
    <div class="contactform"><span style="color:#2428EB">
    
    <strong><u><p style="font-size: 25px;">You can use this form to contact us</p></u></strong>
        
    
      <form name="contactform">
      <form action="contactform">
    <form method="post">
    
    <label for="firstname">First Name</label><br>
    <input type="text" name="firstname" id="firstname" size="25"  placeholder="John" /><br>

    <label for="lastname">Last Name</label><br>
    <input type="text" name="lastname" id="lastname" size="25" placeholder="Someone" /><br>

    <label for="email">Email</label><br>
    <input type="email" name="email" id="email" size="25" placeholder="john.someone@website.com" /><br><br>
    <label for="message">Message</label><br>
    <textarea id="message" name="message" rows="6" cols="35" placeholder="Please enter your message here" ></textarea><br>

    <button type="submit">If you're happy to send, then click here</button>
      </form></form></form>    
     </span>
      <script>
     $(function() {
  $("form[name='contactform']").validate({
    rules: {
      firstname: "required",
      lastname: "required",
        message: "required",
      email: {
        required: true, 
        email: true
      },
    },
     messages: {
      firstname: "You missed out your first name",
      lastname: "oops, you forgot to enter your last name",
            email: "Don't forget you need to enter your email address",
        message: "Didn't you want to say something?",
    },
     submitHandler: function(form) {
      form.submit();
    }
  });
});  
    </script>        
<script>
  <?php 
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];         
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent=" From: $firstname\n $lastname\n Message: $message";
$recipient = "*******@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo  "<br><br><br>Your contact form was sent.Thank You! <br>We will try to answer you as soon as we can"
?>
</script> 
 </body>
</html>

Sorry if I’m making novice mistakes.
If anyone can point me in the right direction.

You should only have one form tag.

Is the code for sending the email in a separate PHP file, and is it named suitably? You quote the action as “contactform” - shouldn’t that be “contactform.php”?

As for actually sending the email, lots of commercial email servers won’t just send emails “from” the address that your user typed into the forum. A proper email server will be configured to only send emails from a domain that it’s supposed to be sending from, otherwise it’s a thing called “Open Relay” which is A Bad Thing. Use an email address that you have configured the server to support, and use the “reply-to” header if you want to make it easier to reply.

I should also mention that the built-in PHP mail() function isn’t considered to be a good way of sending email, you should look at a library such as PHPMailer. That might be particularly needed if you’re using gmail as your mail server, as that will need a secure connection on an alternate port. Is your development system configured correctly to support sending emails?

As mentioned, you form element(s) is wrong. Have just one form element with all the attributes in it.

Aside form that, there are things that could be done differently.
You can’t rely on client side validation alone, it should be backed up by server side validation too.
I would probably do away with the js validation, as basic things like that are now built into browsers if you use the correct attributes in the inputs, such as the required keyword. That will leave you to concentrate on the server side validation.

It looks like you have the PHP form processing in with the HTML from page. That is doable, but if you do it, do it differently. Put the PHP at the beginning of the file, before any HTML and wrap the processing in a condition, to only fire on submission:-

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // Form processing here
}
?>
<!DOCTYPE html>
<!-- HTML form page here -->

In this way you don;t even need the action attribute, as it will default to itself.
It also means that if a submission fails the server side validation, you can return the user to the form and list the errors they made.
A successful submission can be redirected to a success page via headers.

Thanks guys for your help.
As said I’m a total novice on this, so I’ll be a while trying get my head round your suggestions.

I’ll report back once I’ve had ago, probably with more issues.

Finally I found a solution that works. Why not before beats me.

<?php 
header("Location: http://www.website.co.uk/formreply.html");
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent ="
$firstname \n
$lastname \n
$email \n
$message";
$recipient = "aname@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader)
?>

The header redirect should be the last line. Also, this code is vulnerable to an Email Header Injection Attack. Never ever trust user supplied data. Do not create variables for nothing.

2 Likes

Thanks for posting benanaman.
I’ll move the headed to the end.

Hi again benanmen.

I moved the header redirect as you suggested, but this gives me an error
'syntax error, unexpected ‘header’ (T_STRING)

You’re missing a semicolon on the end of your mail line.

Thanks m_hutley but it’s definatelt there.

Your error 100% says it’s not.

Your code block says it’s not.

Show us your updated code.

<?php 

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent ="
$firstname \n
$lastname \n
$email \n
$message";
$recipient = "edit@gmail.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader)
header("Location: http://www.edit.co.uk/formreply.html");
?>
$mailheader = "From: $email \r\n";                   v
mail($recipient, $subject, $formcontent, $mailheader)   <--- Oh yeah?
                                                     ^
header("Location: http://www.edit.co.uk/formreply.html");
?>

So when you say “Its definitely there”, what you mean is “Its not there, it doesnt exist, and woops, sorry.”

Ye sorry. I misunderstood you.
I thought you meant this one

header("Location: http://www.edit.co.uk/formreply.html");

Now that is sorted, how about dealing with this?

In this case it shouldn’t be too hard. It just means doing something that you should be doing already.

1 Like

So the PHP lesson here is “Syntax error, unexpected [something]” means, "Look immediately before [something]. You’ve forgotten something. Semicolon, close parenthesis, close brace… something was expected between the previous statement and this one.

Thanks m_ . Leason learned.

Sam it’s definately on my to do.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.