-1

I've been learning PHP for about two days now. So I'm really new to PHP. I have tried my best to understand but no matter what I write, my code never works. I got it to send an email successfully one time. But I'm not able to get it to send the contents in my form.

Here is my HTML form.

    <!-- FORM BEGINS -->

<form action="contact-form-handler.php" method="post">

<!-- Full Name -->

    <div class="formWrapper ">
        <label name="firstname" for="firstName"><span>*</span>First Name</label>
        <input type="text" name="memberFirstName" id="memberFirstName" onkeyup="activateButton()" class="inputText" tabindex="1" maxlength="256" onkeydown="return checkAlphaNumericText(event,this);" required="required"/>
    </div>

    <p class="fieldsetDivider"></p>

    <div class="formWrapper">
        <label for="lastName"><span>*</span>Last Name</label>
        <input type="text" name="memberLastName" id="memberLastName" onkeyup="activateButton()" class="inputText" tabindex="2" maxlength="256" onkeydown="return checkAlphaNumericText(event,this);" required="required"/>
    </div>

    <p class="fieldsetDivider"></p>

    <!-- Date of Birth-->

    <p class="fieldsetDivider"></p>

    <div class="formWrapper smallInput">
        <label for="firstInput"><span>*</span>Date of Birth
        <div class="labelClarify">(MM/DD/YYYY)</div>
        </label>        
        <div class="inlineInput">
            <input type="text"  name="month" id="month" maxlength="2" onkeyup="activateButton();autotab(this, 'day')" onkeypress="return isNumberKey(event)" class="inputText" placeholder="MM" required="required"/>
            <span>/</span>
        </div>      
        <div class="inlineInput">
            <input type="text"  name="day" id="day" maxlength="2" onkeyup="activateButton();autotab(this, 'year')" onkeypress="return isNumberKey(event)" class="inputText" placeholder="DD" required="required"/>
            <span>/</span> 
        </div>            
        <div class="inlineInput">
            <input type="text"  name="year" id="year" maxlength="4" onkeyup="activateButton()" onkeypress="return isNumberKey(event)" class="inputText" placeholder="YYYY" required="required"/>
        </div>              
        <a rel="Please enter your date of birth" class="formHelp tooltip-link" href="#">
            <img src="../../images/site3/common/form_icon_help.png">
        </a>
    </div>

    <p class="fieldsetDivider"></p>

    <!-- Member ID -->

    <div class="formWrapper">
        <label for="memberID"><span>*</span>Member ID</label>
     <div class="inlineInput">
        <input type="text" name="memberID" id="memberID" class="inputText" onkeyup="activateButton()" tabindex="3" maxlength="9" onkeypress="return isNumberKey(event)" required="required"/>
     </div>       
     <a rel="Please enter your member ID" class="formHelp tooltip-link" href="#">
        <img src="../../images/site3/common/form_icon_help.png">
        </a>
    </div>

    <p class="fieldsetDivider"></p>

    <!-- Zip Code -->

    <div class="formWrapper">
            <label for="zipCode"><span class="requiredStar">*</span>Zip Code</label>
            <div class="inlineInput">
            <input type="text" name="memberZipCode" id="memberZipCode" onkeyup="activateButton()" tabindex="4" class="inputText" maxlength="5" onkeypress="return isNumberKey(event)"/>
        </div>
    </div>

    <p class="fieldsetDivider"></p>

<!-- Button -->

    <div class="button">
        <button class="greenBtnLarge" type="submit" style="text-decoration:none;"><span>Next<span></button>
    </div>
</form>

<!-- FORM ENDS -->

Can someone please help me with a php code that would work for this form? I have found a couple template forms on the internet and I've spent hours trying to modify them to work with my form. But no luck.

And I can get the PHP and form to work together and make it send me an email from my server, so there's no problem with the server. An email will send. I just can't get the PHP code to send the contents of the form. It sends the email, but with no form details. Understand?

I really, really need help with this. Can you guys help me create a PHP code that will work with my HTML form I posted in this thread? I really need to get past this step in this project.

Thank you!

UPDATE!

Here is PHP template I found on the internet, but I don't know how to really modify it properly.... please help?

<?php 
$errors = '';
$myemail = 'example@example.com';//<-----Put Your email address here.
if(empty($_POST['firstname'])  || 
   empty($_POST['lastname']) || 
   empty($_POST['dob']))
{
    $errors .= "\n Error: all fields are required";
}

$name = $_POST['firstname']; 
$email_address = $_POST['lastname']; 
$message = $_POST['dob']; 

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", 
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
    $to = $myemail; 
    $email_subject = "Contact form submission: $name";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message"; 

    $headers = "From: $myemail\n"; 
    $headers .= "Reply-To: $email_address";

    mail($to,$email_subject,$email_body,$headers);
    //redirect to the 'thank you' page
    header('Location: contact-form-thank-you.html');
} 
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>
GVS
  • 229
  • 3
  • 16
  • I don't know how to properly link the names in my html form to my PHP code. – GVS Nov 07 '15 at 05:33
  • 1
    can you add the contents of `contact-form-handler.php`? – roullie Nov 07 '15 at 05:34
  • Where is the PHP code that sends the data? This sounds like what most tutorials would cover.. – chris85 Nov 07 '15 at 05:36
  • I added the php code. – GVS Nov 07 '15 at 05:37
  • Do you try to execute the code in local server? – Salah Nov 07 '15 at 05:37
  • 2
    Your inputs are `memberFirstName`, not `firstname`, etc. You need to look at the tutorials again to understand what they mean. Maybe take a look here, http://php.net/manual/en/reserved.variables.post.php. – chris85 Nov 07 '15 at 05:37
  • No I tried that but it wasn't working. I added the original code. – GVS Nov 07 '15 at 05:39
  • What do I need to change? – GVS Nov 07 '15 at 05:39
  • All of `$_POST['etc.` because your name attributes are what populate the keys of the POST arrays. – chris85 Nov 07 '15 at 05:40
  • So I just need to change {$_POST[} and it should send the details? – GVS Nov 07 '15 at 05:41
  • I dont know what the `{}`s are doing there, do you mean complex syntax? If so no, you need to use the actual named values from your form. – chris85 Nov 07 '15 at 05:42
  • Is this all I have to change? if(empty($_POST['memberFirstName']) || empty($_POST['memberLastName']) || empty($_POST['month'])) empty($_POST['day'])) empty($_POST['year'])) empty($_POST['MemberID']) || empty($_POST['MemberZipCode'])) { – GVS Nov 07 '15 at 05:45
  • ...and the variable assignments. – chris85 Nov 07 '15 at 05:52
  • Now when I submit form I get taken to example.com/contact-form-handler.php - And I get a message saying "sytax error line 7" – GVS Nov 07 '15 at 05:53
  • Line 7 is "" empty($_POST['day'])) "" – GVS Nov 07 '15 at 05:54

2 Answers2

1

You have a several code errors...

  1. Remove the <label> tags from your titles for <input type="text">... they are not needed.
  2. Make sure your form field name matches your email code $_POST['something'] for example
    You have <input type="text" name="memberFirstName" id="memberFirstName" /> in your form... BUT you have $_POST['firstname'] in your email code. Change $_POST to memberFirstName OR change your <input name and id to firstname

The same changes need to be made to all elements so they match precisely.

AND remember... PHP is case sensitive.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Kuya
  • 7,280
  • 4
  • 19
  • 31
0

The HTML form fields are not the same with the fields in php email handler

Ex.

The input name should be same as what the handler should get Since

<input type="text" name="memberFirstName" required="required"/>

The index of $_POST should be same memberFirstName

$_POST['memberFirstName']

This should be applied to all fields

It seems the HTML form is not related to the php handler at all.

Its better to use this form for testing instead

<form action="contact-form-handler.php" method="POST">
    First Name: <input name="firstname" type="text"><br>
    Last Name:<input name="lastname" type="text"><br>
    DOB: <input name="dob" type="text">
    <button type="submit">Send</button>
</form>

Note:

If you are using wamp or xampp local server you need to check if the email service is configured correctly otherwise no email will be sent Check this for reference: How to configure XAMPP to send mail from localhost?

Community
  • 1
  • 1
Salah
  • 139
  • 6