Contact Form Function

Hello I am writing the PHP code for my contact form however it is
working but not sending me any information in the field forms im not quite sure what is wrong.
and I get a blank page when I hit submit. how can I add a thanks for submitting or something?
here is my code:

HTML:

     <form id="contactform" action="contactengine.php" method="post"> 
                  <span style="color:#1d4189;"><h4>*Fill out Form</h4></span>
                <input  class="cntct-inputs" type="name" name="name" placeholder="Your Name"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  <select class="cntct-inputs select" name="how did you hear about us" placeholder="Your Name" > 
                  <option>How were you referred?</option>
                    <option>Email</option>
                    <option>Amazon</option>
                    <option>Previous User</option>
                    <option>Search Engine</option>
                  </select>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  <input class="cntct-inputs" type="email" name="email" placeholder="Your Email"/><br><br>
                  <input class="cntct-inputs" type="company" name="company" placeholder="Your Company"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  <input class="cntct-inputs" type="phone" name="phone" placeholder="Your Phone"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                  <input class="cntct-inputs" type="address" name="address" placeholder="Your Address"/><br><br>
                  <textarea class="cntct-inputs txt-area" name="comments">Your Comments...</textarea><br><br>
                  <input clas

s="hvr-skew-forward contact-sub" type="submit" name="submit" value="Submit"/>
              </form> 

PHP

<?php

$EmailTo = "csosa@iogproducts.com";
$Subject = "Nice & Simple Contact Form by CSS-Tricks";
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Company = Trim(stripslashes($_POST['Company'])); 
$Phone = Trim(stripslashes($_POST['Phone'])); 
$Address = Trim(stripslashes($_POST['Address'])); 
$Comments = Trim(stripslashes($_POST['Comments'])); 

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Company: ";
$Body .= $Company;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";
$Body .= "Address: ";
$Body .= $Address;
$Body .= "\n";
$Body .= "Comments: ";
$Body .= $Comments;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");


?>

$_POST is case sensitive. Your form has all the fields in lower case, your php code does not.

1 Like

ā€¦ and as far as the form being blank after submit - yes, itā€™s supposed to do that. If you want to keep the data in the fields, then use JavaScript to preventDefault() the submit and then submit the data via AJaX (in the background.)

HTH,

:slight_smile:

1 Like

I seeā€¦good to know.

Also, try to avoid using reserved words for name/id of elements. name and submit are reserved words.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar (1/3 of the way down the page.)

Giving elements these as name/id can cause lots of confusion. Try ā€˜userNameā€™ or ā€˜fullNameā€™ or ā€˜submitBtnā€™.

HTH,

:slight_smile:

ok so more specific names?

Correctā€¦ something that still describes what it is, but isnā€™t a reserved word. Naming a submit button ā€œsubmitā€ can cause problems when you need to submit().

V/r,

:slight_smile:

1 Like

I am trying to also capture my <select> element in my PHP code
I tested it the same as the others but it does not seem to work am I
missing something?

HTML:

 <select class="cntct-inputs select" name="Howdidyouhearaboutus" > 
                  <option>How were you referred?</option>
                    <option>Email</option>
                    <option>Amazon</option>
                    <option>Previous User</option>
                    <option>Search Engine</option>
                  </select>

PHP:

$Referral = Trim(stripslashes($_POST['Howdidyouhearaboutus'])); 

$Body .= "How did you hear about us: ";
$Body .= $Referral;
$Body .= "\n";
<select class="cntct-inputs select" name="Howdidyouhearaboutus" > 
    <option>How were you referred?</option>
    <option value="email">Email</option>
    <option value="amazon">Amazon</option>
    <option value="previous user">Previous User</option>
    <option value="search engine">Search Engine</option>
</select>

Options need to have values

2 Likes

I see thank You.

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