Hey everyone,

Forgive me if this is a dumb question but I can't figure out why my data that I've typed in or selected in the form doesn't show when I actually send out an email once I click the submit button. I get an email stating the subject of the email that I sent but the content isn't there..why is this?

<?php
error_reporting (E_ALL ^ E_NOTICE); 

$to = "gene.howell9@gmail.com";
$subject = "Submitions from College/University Admission Services Form";

$Sname = $_POST["Sname"];
$Fname = $_POST["Fname"];
$Mname = $_POST["Mname"];
$Lname = $_POST["Lname"];
$email = $_POST["email"];
$history = $_POST["history"];
$age = $_POST["age"];
$sex = $_POST["sex"];
$country = $_POST["country"];
$SchoolsAttended = $_POST["SchoolsAttended"];
$CriminalRecord = $_POST["CriminalRecord"];
$record = $_POST["record"];
$status = $_POST["status"];

if($_POST['submit']){
    if($Sname == '' || $Fname == '' || $Mname == '' || $Lname == '' || $email == '' || $history == '' || $age == '' || $sex == '' || $country == '' || $SchoolsAttended == '' || $CriminalRecord == '' || $status == ''){
        $feedback = "<b>Fill out all the fields please before submitting</b>";
    }else{
    mail($to, $subject, $body, $header);
    $feedback ="<b>Thanks for your submition! Your form has been sent!</b>";
    }
}

$body = <<<EMAIL

Form submitted from $Fname $Lname.

Surname field: $Sname
First name field: $Fname
Middle name field: $Mname
Last name field: $Lname
History field: $history
Age: $age
Sex: $sex
Country: $country
Schools Attended: $SchoolsAttended
Criminal Record: $CriminalRecord
If yes was selected, list of details: $record
Marital Status: $status

From, $Fname

EMAIL;

$header = "From: $email";

?>

Here is the form as well:

<form style="padding: 10px;" method="post" action="?">
Surname: <input type="text" maxlength="12" name="Sname"><br /><br />
First Name: <input type="text" maxlength="12" name="Fname"><br /><br />
Middle Name: <input type="text" maxlength="12" name="Mname"><br /><br />
Last Name: <input type="text" maxlength="36" name="Lname"><br /><br />
Email: <input type="text" maxlength="36" name="email"><br /><br />
History: <input type="text" maxlength="50" name="history"><br /><br />
Age: <input type="text" size="1" maxlength="2" name="age"><br /><br />
Sex: <input type="radio" value="Male" name="sex">Male<input type="radio" value="Female" name="sex">Female<br /><br />
Country of Origin: <select name="country">
                    <option value="Select">-Select a Country-</option>
                    <option value="United States">United States</option>
                    <option value="Africa">Africa</option>
                    <option value="Canada">Canada</option>
                </select><br /><br />
Schools Attended: <input type="text" name="SchoolsAttended"><br /><br />
Do you have a criminal record? <input type="radio" value="yes" name="CriminalRecord">Yes <input type="radio" value="no" name="CriminalRecord">No<br /><br />
If yes was selected, please list them in the field provided: <input type="text" name="record"><br /><br />
 Marital Status: <select name="status">
                    <option value="select">-Select your status-</option>
                    <option value="single">Single</option>
                    <option value="married">Married</option>
                    <option value="divorced">Divorced</option>
                    <option value="widowed">Widowed</option>
                </select><br /><br />
<input class="button" type="submit" value="submit" name="submit"> <input class="button" type="reset" value="Reset" name="reset" />
</form>

Thanks in advance!
-Geneh23

Member Avatar for LastMitch

@geneh23

Forgive me if this is a dumb question but I can't figure out why my data that I've typed in or selected in the form doesn't show when I actually send out an email once I click the submit button.

There's no dumb question. It's much easier just start over! I feel like you didn't follow the format for email form.

Choose either examples:

http://php.about.com/od/phpapplications/ss/form_mail_3.htm

or

http://www.freecontactform.com/email_form.php

Both are very easy to modify to fit your email content.

The reason why I said easier modify because I already test it out with your data!

@LastMitch,

Thank you for being a big help! does this look right to you?

 <?php
    $to = $_REQUEST['myemailaddress@myemail.com'];
    $from = $_REQUEST['Email'];
    $name = $_REQUEST['Name'];
    $headers = "From: $from";
    $subject = "Submitions from College/University Admission Services Form";

    $fields = array();
    $fields{"Sname"} = "Sname";
    $fields{"Fname"} = "Fname";
    $fields{"Mname"} = "Mname";
    $fields{"Lname"} = "Lname";
    $fields{"email"} = "email";
    $fields{"history"} = "history";
    $fields{"age"} = "age";
    $fields{"sex"} = "sex";
    $fields{"country"} = "country";
    $fields{"SchoolsAttended"} = "SchoolsAttended";
    $fields{"CriminalRecord"} = "CriminalRecord";
    $fields{"record"} = "record";
    $fields{"status"} = "status";

    $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){    $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

    $headers2 = "From: noreply@mywebsite.org"; 
    $subject2 = "Thank you for contacting us"; 
    $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.mywebsite.org";

    if($_POST['submit']){
        if($Sname == '' || $Fname == '' || $Mname == '' || $Lname == '' || $email == '' || $history == '' || $age == '' || $sex == '' || $country == '' || $SchoolsAttended == '' || $CriminalRecord == '' || $status == ''){
        $feedback = "<b>Please fill out ALL of the fields before submitting</b>";
    }else{
        $send = mail($to, $subject, $body, $headers); 
        $send2 = mail($from, $subject2, $autoreply, $headers2); 
        if($send){
        echo "<b>Thanks for your submition! Your form has been sent!</b>";
        } else {
        print "We encountered an error sending your mail, please notify mywebsite@mywebsite.org"; 
        }
        }
    }

 ?>

Would this work?

<?php
error_reporting (E_ALL ^ E_NOTICE); 

if(!isset($_POST['submit']))
{
    echo "Error! You need to submit the form!";
}
$Sname = $_POST["Sname"];
$Fname = $_POST["Fname"];
$Mname = $_POST["Mname"];
$Lname = $_POST["Lname"];
$email = $_POST["email"];
$history = $_POST["history"];
$age = $_POST["age"];
$sex = $_POST["sex"];
$country = $_POST["country"];
$SchoolsAttended = $_POST["SchoolsAttended"];
$CriminalRecord = $_POST["CriminalRecord"];
$record = $_POST["record"];
$status = $_POST["status"];

//Validate
if(empty($Sname)||empty($Fname)||empty($Mname)||empty($Lname)||empty($email)||empty($history)||empty($age)||empty($sex)||empty($country)||empty($SchoolsAttended)||empty($CriminalRecord)||empty($record)||empty($status))
{
    echo "All fields are mandatory!";
    exit;
}

if(IsInjected($email))
{
    echo "Bad email value!";
    exit;
}

$email_from = '$email';
$email_subject = "Test";
$email_body = "You have received a new message from the user $Fname.\n".
    "Here is the message:\n 
        Surname field: $Sname\n
        First name field: $Fname\n
        Middle name field: $Mname\n
        Last name field: $Lname\n
        History field: $history\n
        Age: $age\n
        Sex: $sex\n
        Country: $country\n
        Schools Attended: $SchoolsAttended\n
        Criminal Record: $CriminalRecord\n
        If yes was selected, list of details: $record\n
        Marital Status: $status".

$to = "myemail@myemail.com";
$headers = "From: $email \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. display thank-you message.
echo "<b>Thanks for your submition! Your form has been sent!</b>";


// Function to validate against any email injection attempts
function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}

?>

So I changed my processing around and now my form.php page wont show anymore..It just shows as a blank page. I'm not sure why... here is my code Thanks!

<?php

if(isset($_POST['email'])) {

    // CHANGE THE TWO LINES BELOW
    $email_to = "myemailaddress@myemail.com";
    $email_subject = "test";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    // validation expected data exists
    if(!isset($_POST['Sname']) ||
        !isset($_POST['Fname']) ||
        !isset($_POST['Mname']) ||
        !isset($_POST['Lname']) ||
        !isset($_POST['email']) ||
        !isset($_POST['history']) ||
        !isset($_POST['age']) ||
        !isset($_POST['sex']) ||
        !isset($_POST['country']) ||
        !isset($_POST['SchoolsAttended']) ||
        !isset($_POST['CriminalRecord']) ||
        !isset($_POST['record']) ||
        !isset($_POST['status'])) {
       $feedback="Fill out all the fields please before submitting";       
    }

    $Sname = $_POST["Sname"];
    $Fname = $_POST["Fname"];
    $Mname = $_POST["Mname"];
    $Lname = $_POST["Lname"];
    $email = $_POST["email"];
    $history = $_POST["history"];
    $age = $_POST["age"];
    $sex = $_POST["sex"];
    $country = $_POST["country"];
    $SchoolsAttended = $_POST["SchoolsAttended"];
    $CriminalRecord = $_POST["CriminalRecord"];
    $record = $_POST["record"];
    $status = $_POST["status"];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$Sname)) {
    $error_message .= 'The Sir Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$Fname)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$Mname)) {
    $error_message .= 'The Middle Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$Lname)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$history)) {
    $error_message .= 'The history you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$age)) {
    $error_message .= 'The age you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$sex)) {
    $error_message .= 'The gender you selected does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$country)) {
    $error_message .= 'The country you selected does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$SchoolsAttended)) {
    $error_message .= 'The school you entered does not appear to be valid.<br />';
  }
   if(!preg_match($string_exp,$CriminalRecord)) {
    $error_message .= 'The criminal record you entered does not appear to be valid.<br />';
  }
   if(!preg_match($string_exp,$record)) {
    $error_message .= 'The record you entered does not appear to be valid.<br />';
  }
   if(!preg_match($string_exp,$status)) {
    $error_message .= 'The status you selected does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Sir Name: ".clean_string($Sname)."\n";
    $email_message .= "First Name: ".clean_string($Fname)."\n";
    $email_message .= "Middle Name: ".clean_string($Mname)."\n";
    $email_message .= "Last Name: ".clean_string($Lname)."\n";
    $email_message .= "Email: ".clean_string($email)."\n";
    $email_message .= "History: ".clean_string($history)."\n";
    $email_message .= "Age: ".clean_string($age)."\n";
    $email_message .= "Sex: ".clean_string($sex)."\n";
    $email_message .= "Country: ".clean_string($country)."\n";
    $email_message .= "Schools Attended: ".clean_string($SchoolsAttended)."\n";
    $email_message .= "Criminal Record: ".clean_string($CriminalRecord)."\n";
    $email_message .= "If yes was selected, list of details: ".clean_string($record)."\n";
    $email_message .= "Status: ".clean_string($status)."\n";


// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers); 
$feedback ="<b>Thanks for your submition! Your form has been sent!</b>"; 
}
die();
?>
            <p id="feedback"><?php echo $feedback; ?></p>
                <form style="padding: 10px;" method="post" action="?">
Surname: <input type="text" maxlength="12" name="Sname"><br /><br />
First Name: <input type="text" maxlength="12" name="Fname"><br /><br />
Middle Name: <input type="text" maxlength="12" name="Mname"><br /><br />
Last Name: <input type="text" maxlength="36" name="Lname"><br /><br />
Email: <input type="text" maxlength="36" name="email"><br /><br />
History: <input type="text" maxlength="50" name="history"><br /><br />
Age: <input type="text" size="1" maxlength="2" name="age"><br /><br />
Sex: <input type="radio" value="Male" name="sex">Male<input type="radio" value="Female" name="sex">Female<br /><br />
Country of Origin: <select name="country">
                    <option value="Select">-Select a Country-</option>
                    <option value="United States">United States</option>
                    <option value="Africa">Africa</option>
                    <option value="Canada">Canada</option>
                </select><br /><br />
Schools Attended: <input type="text" name="SchoolsAttended"><br /><br />
Do you have a criminal record? <input type="radio" value="yes" name="CriminalRecord">Yes <input type="radio" value="no" name="CriminalRecord">No<br /><br />
If yes was selected, please list them in the field provided: <input type="text" name="record"><br /><br />
 Marital Status: <select name="status">
                    <option value="select">-Select your status-</option>
                    <option value="single">Single</option>
                    <option value="married">Married</option>
                    <option value="divorced">Divorced</option>
                    <option value="widowed">Widowed</option>
                </select><br /><br />
<input class="button" type="submit" value="submit" name="submit"> <input class="button" type="reset" value="Reset" name="reset" />
</form>
Member Avatar for LastMitch

@geneh23

What are you trying to do? Why are you changing your content? When I gave you those 2 links, either one you can used you should just follow the format and enter the same detail that you have with it. I'm not going to post the form that I did with your data. So you have to follow the format.

It's getting late.

@LastMitch I'm trying to submit a form that has the data that I either selected or imputed and have the data that I put, show up in an email. ...and I thought that's what I did..I used the form from the two links that you gave me..and I substituted my info to fit within what the form had...

Line 124 tells the script to die before showing the form.

I removed the die(); now I get this: "We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

The age you entered does not appear to be valid.

Please go back and fix these errors."

I don't know why I am getting this error since it's an input field and I entered two numbers..could someone help me figure out why this is? Thanks in advance!

After line 55 ($string_exp = "/^[A-Za-z .'-]+$/";), put $number_exp = "/^[0-9]+$/";
On line 71 change (!preg_match($string_exp,$age)) to (!preg_match($number_exp,$age))
The age was being validated as a string.

Member Avatar for LastMitch

@geneh23

What change you made? There shouldn't be a problem with age or any category.

The issue is with this $string_exp = "/^[A-Za-z .'-]+$/";

You are missing something

It should look similiar to this

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

it should be one of those combination.

so every category is reading the same preg_match

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.