Question

I am using phpmailer on an old website to send an email once a form has been submit.

It is sending input text correctly but is not sending the correct select option.

Here is the PHP/HTML page i am using:

// array to create title select options
<?php
$selectArray1 = array ('Mr'=>'Mr',
                  'Mrs'=>'Mrs',
                  'Miss'=>'Miss',
                  'Ms'=>'Ms',
                  'Dr'=>'Dr');
?>

// below select box only sending the first option (Mr)
<label for="title">Title</label>
<select id="title" name="title[]">
<?php
foreach ($selectArray1 as $val_title){
echo "<option value=\"$val_title\">";
echo $val_title;
echo "</option>";
}
?>
</select>

//below input in sending correctly
<label for="name">Name</label></td>
<input id="name" name="name" maxlength="50" type="text" value="">

So no matter if i select say "Miss" from the dropdown, the email when received will always have "Mr" as the title.

Here is the mailer that is the action for the form when submited:

<?php

// Grab our config settings
require_once($_SERVER['DOCUMENT_ROOT'].'/Site/config.php');

// Grab the FreakMailer class
require_once($_SERVER['DOCUMENT_ROOT'].'/Site/lib/MailClass.inc');

// instantiate the class
$mailer = new FreakMailer();

// Set the subject
$mailer->Subject = 'Finance Enquiry - Site.co.uk';

// Body
$mailer->Body = 'Finance Enquiry from Site.co.uk' . "\n\n" . $_POST['title'] .      "\n\n" . $_POST['name'] . "\n\n" . $_POST['user-email'];


// Add an address to send to.
$mailer->AddAddress('mail@mail.co.uk', 'Site');

if(!$mailer->Send())
{
echo 'There was a problem sending this mail!';
}
else
{
echo 'Mail sent!';
}
$mailer->ClearAddresses();
$mailer->ClearAttachments();
?>

The $_POST['title'] above is where i am having the problem. How can i change the code so that the correct select option is sent from the users input?

Was it helpful?

Solution 3

It turns out this was a stupid mistake where there were conflicting input name/IDs.

ID title was assigned to another element on my form witch was causing the conflict.

OTHER TIPS

Why you have named select as "title[]", instead just rename it to "title" and check the code. Square brackets are only required, if you have to accept multiple values.

you have to use 'title' instead of 'title[]' as the name of the <select>:

This works:

<?php
$selectArray1 = array ('Mr'=>'Mr',
                  'Mrs'=>'Mrs',
                  'Miss'=>'Miss',
                  'Ms'=>'Ms',
                  'Dr'=>'Dr');
?>
<form action="test.php?done=1" method="POST">

<label for="title">Title</label>
<select id="title" name="title">
<?php
foreach ($selectArray1 as $val_title){
echo "<option value=\"$val_title\">";
echo $val_title;
echo "</option>";
}
?>
</select>

<input type="submit">
</form>

<?php

if( $_GET["done"] == 1 )
{
    echo "Output:".$_POST["title"];
}
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top