PHP Form - sending "Name" field to email

Hi all,

first of all I appreciate you reading my post, I’m a total PHP newbie, I’ve found some PHP templates and i’m trying to manipulate them in order to learn but I’m having trouble getting something to work!

I have a Test/Quiz set up using PHP I’ve got it working and emailing me the results but I can’t seem to get the form input where the user enters their name into the email that’s sent… I’m sure what I’ve tried is laughable to you pros but I’m stumped!

heres my code

index.php
[php]

<title>Nationwide Utilities - Sales Test - Week One</title>

<link rel="stylesheet" type="text/css" href="css/style.css" />
<div id="page-wrap">

	<h1>Test</h1>
	
	<form action="grade.php" method="post" id="quiz">

Please Enter your Name:

        <ol>
        
            <li>
            
                <h3>Question.</h3>
                
                <div>
                    <input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
                    <label for="question-1-answers-A">A) Correct </label>
                </div>
                
                <div>
                    <input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
                    <label for="question-1-answers-B">B) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
                    <label for="question-1-answers-C">C) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
                    <label for="question-1-answers-D">D) Wrong</label>
                </div>
            
            </li>
            
            <li>
            
                <h3>Question...</h3>
                
                <div>
                    <input type="radio" name="question-2-answers" id="question-2-answers-A" value="A" />
                    <label for="question-2-answers-A">A) Wrong</label></div>
                
                <div>
                    <input type="radio" name="question-2-answers" id="question-2-answers-B" value="B" />
                    <label for="question-2-answers-B">B) Correct</label>
                </div>
                
                <div>
                    <input type="radio" name="question-2-answers" id="question-2-answers-C" value="C" />
                    <label for="question-2-answers-C">C) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-2-answers" id="question-2-answers-D" value="D" />
                    <label for="question-2-answers-D">D) Wrong</label>
                </div>
            
            </li>
            
            <li>
            
                <h3>Question...</h3>
                
                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-A" value="A" />
                    <label for="question-3-answers-A">A) </label>
                    Wrong</div>
                
                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-B" value="B" />
                    <label for="question-3-answers-B">B) </label>
                    Correct</div>
                
                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-C" value="C" />
                    <label for="question-3-answers-C">C) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-3-answers" id="question-3-answers-D" value="D" />
                    <label for="question-3-answers-D">D) Wrong</label>
                </div>
            
            </li>
            
            <li>
            
                <h3>Question...</h3>
                
                <div>
                    <input type="radio" name="question-4-answers" id="question-4-answers-A" value="A" />
                    <label for="question-4-answers-A">A) </label>
                Wrong                    </div>
                
                <div>
                    <input type="radio" name="question-4-answers" id="question-4-answers-B" value="B" />
                    <label for="question-4-answers-B">B) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-4-answers" id="question-4-answers-C" value="C" />
                    <label for="question-4-answers-C">C) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-4-answers" id="question-4-answers-D" value="D" />
                    <label for="question-4-answers-D">D) </label>
                Correct                    </div>
            
            </li>
            
            <li>
            
                <h3>Question...</h3>
                
                <div>
                    <input type="radio" name="question-5-answers" id="question-5-answers-A" value="A" />
                    <label for="question-5-answers-A">A) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-5-answers" id="question-5-answers-B" value="B" />
                    <label for="question-5-answers-B">B) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-5-answers" id="question-5-answers-C" value="C" />
                    <label for="question-5-answers-C">C) Wrong</label>
                </div>
                
                <div>
                    <input type="radio" name="question-5-answers" id="question-5-answers-D" value="D" />
                    <label for="question-5-answers-D">D) Correct</label>
                </div>
            
            </li>
        
        </ol>
        
        <input type="submit" value="Submit my Answers" />




[/php]

grade.php
[php]

<title>Test</title>

<link rel="stylesheet" type="text/css" href="css/style.css" />
<div id="page-wrap">

	<h1>Test</h1>
	
    <?php
        
       $answer1 = $_POST['question-1-answers'];
        $answer2 = $_POST['question-2-answers'];
        $answer3 = $_POST['question-3-answers'];
        $answer4 = $_POST['question-4-answers'];
        $answer5 = $_POST['question-5-answers'];
    
        $totalCorrect = 0;
        
        if ($answer1 == "A") { $totalCorrect++; }
        if ($answer2 == "B") { $totalCorrect++; }
        if ($answer3 == "B") { $totalCorrect++; }
        if ($answer4 == "D") { $totalCorrect++; }
        if ($answer5 == "D") { $totalCorrect++; }
        
        echo "<div id='results'>$totalCorrect / 5 correct</div>";
        echo "<div id='results'>Your results have been submitted</div>";
    ?>
<?php $mailTo = "[email protected]"; $msgSubject = "Test"; $msgBody = "The has been complete\n\n"; $msgBody .= $fields{"Name"} . "\n"; $msgBody .= "The final score was "; $msgBody .= $totalCorrect . " correct answers from 5 questions\n\n "; $msgBody .= "The Answers Given were:\n"; $msgBody .= $answer1 . "\n"; $msgBody .= $answer2 . "\n"; $msgBody .= $answer3 . "\n"; $msgBody .= $answer4 . "\n"; $msgBody .= $answer5 . "\n\n"; $msgBody .= "THIS IS AN AUTOMATED EMAIL\n\n"; $xHeaders = "From:"; mail ($mailTo, $msgSubject, $msgBody, $xHeaders); ?>
</div>
[/php]

If I’m reading everything correctly…

I think you need to change. .

[php]$msgBody .= $fields{“Name”} . “\n”;[/php]

to:

[php]$msgBody .= $_POST[‘Name’] . “\n”;[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service