The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Returning selected form field in an email, "select" option not included when form is returned
Barbra
post Nov 25 2015, 09:14 AM
Post #1





Group: Members
Posts: 1
Joined: 25-November 15
Member No.: 23,771



Hello,

I have a form in a webpage with a "select" option that is not being returned in the email generated by the form, but all the other fields are returned successfully. Can someone tell me how to get the selection for Club to be included in the email?

The HTML:

CODE

<form method="post" action="mail.php">
<div class="row uniform">
<div class="6u 12u$(xsmall)">
<label for="name">Full Name</label>
<input name="name" type="text" class="required" id="name" value="" />
</div>
                                                
<div class="6u$ 12u$(xsmall)">
<label for="email">Email Address</label>
<input name="email" type="email" class="required" id="email" value="" />
</div>
                                                
<div class="6u 12u$(xsmall)">
<label for="date">Date of Birth</label>
<input name="date" type="text" class="required" id="date" value="" />
</div>

<div class="6u$ 12u$(xsmall)">
<label for="age">Age as of July 1, 2016</label>
<input name="age" type="text" class="required" id="age" value="" />
</div>
                                                
<div class="6u 12u$(xsmall)">
<label for="phone">Phone Number</label>
<input name="phone" type="tel" class="required" id="phone" value="" />
</div>
                                                
<div class="12u$">
<label for="club">Current Club Membership</label>
<div class="select-wrapper">
<select name="club" class="required" id="club" onchange=    "document.getElementById('text_content').value=this.options[this.selectedIndex].text">
<option value="">-</option>
<option value="Colonial Road Runners">Colonial Road Runners</option>
<option value="Peninsula Track Club">Peninsula Track Club</option>
<option value="Tidewater Striders">Tidewater Striders</option>
</select>
</div>
                                                    
</div>
<div class="12u$">
<label for="message">Message</label>
<textarea name="message" id="message" rows="6"></textarea>
</div>

<div class="12u$">
<ul class="actions">
<li><input type="submit" name="submit" value="Send Message" class="special" /></li>
<li><input type="reset" value="Reset" /></li>
</ul>
</div>
</div>
</form>


And the php:
CODE

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $date = $_POST['date'];
        $age = $_POST['age'];
        $phone = $_POST['phone'];
                $message = $_POST['message'];
              $from = 'From: xxx@gmail.com';
                $to = 'xxx@gmail.com';
                $subject = 'New Registration';
        
        $body.='email: '. $_POST['email']."\n";
        $body.='date: '. $_POST['date']."\n";
        $body.='age: '. $_POST['age']."\n";
        $body.='phone: '. $_POST['phone']."\n";
        $body.='club: '. $_POST['club']."\n";
        $body = "From: $name
                 E-Mail: $email
                 Date of Birth: $date
                 Age 7/2016: $age
                 Phone:  $phone
                 Club:  $club
                 Message: $message";
        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }


    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        header("Location: thankyou.html");
   die();
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Nov 25 2015, 11:11 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(Barbra @ Nov 25 2015, 03:14 PM) *

I have a form in a webpage with a "select" option that is not being returned in the email generated by the form, but all the other fields are returned successfully. Can someone tell me how to get the selection for Club to be included in the email?

It's printed correctly here (assuming the user selected something in the SELECT menu):

CODE
$body.='club: '. $_POST['club']."\n";

but further down the script tries to print a variable $club, which isn't defined:

CODE
Club:  $club

but the first occasion should work.

The PHP script contains more duplicate values than just for "club".
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Nov 25 2015, 12:21 PM
Post #3


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



I can see that the variables are defined at the top of the PHP script, except for $club. Add this line of code after the '$phone = $_POST['phone'];' line:
CODE
$club = $_POST['club'];

The script is not very well written, example: The tests for blank values should be done at the very beginning not half way down. And this section makes no sense to me:
CODE
        $body.='email: '. $_POST['email']."\n";
        $body.='date: '. $_POST['date']."\n";
        $body.='age: '. $_POST['age']."\n";
        $body.='phone: '. $_POST['phone']."\n";
        $body.='club: '. $_POST['club']."\n";
        $body = "From: $name
                 E-Mail: $email
                 Date of Birth: $date
                 Age 7/2016: $age
                 Phone:  $phone
                 Club:  $club
                 Message: $message";
$email, $date, $age, $phone are defined variable that are not used here, the first 3 lines of the '$body .=' code. But the very last $body will overwrite all the previous '$bodys.=' lines. Anyway, just add the 1 line of code I posted and you should be good to go.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 25th April 2024 - 03:41 PM