[RESOLVED] Select option text rather than option value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kimmcms
    New Member
    • Jul 2014
    • 2

    [RESOLVED] Select option text rather than option value

    Hello everyone.

    I am a real newbie to php, and this is my first attempt at writing anything.

    I have created a Contact Form in html with a drop down selection box, which sends the completed form content to a different recipient depending on which option is selected in the drop down box.

    All is working fine (much to my amazement and with a little help from others) EXCEPT, I am stuck with one part. When the form content is sent to the recipient, it shows the option value rather than the option text, and I cannot work my head around how to get it to show the text.

    The relevant part of the Contact Form HTML code is:

    Code:
    <select name="Nature" size="1" id="Nature">
    		    <option>Please Select</option>
    <option value="recipient_1">General Enquiries</option>		  
    <option value="recipient_2">Hall Hire</option>
    <option value="recipient_3">Marquee Hire</option>
    <option value="recipient_4">Parish Church</option>
    <option value="recipient_5">Parish Council</option>
    <option value="recipient_6">Recreation Area</option>
    <option value="recipient_7">Other</option>
    </select>
    and the PHP code is:

    Code:
     /* Email Variables */
    $emailSubject = 'website_enquiry'; 
    $recipients = array(
    
    'recipient_1' => 'kim@example.com', 
    'recipient_2' => 'kim2@example.com', 
    'recipient_3' => 'kms3@example.com',
    'recipient_4' => 'kim4@example.com',
    'recipient_5' => 'kim5@example.com',
    'recipient_6' => 'kim6@example.com',
    'recipient_7' => 'kim7@example.com'
    );
    
    $my_email = $recipients[$_POST['Nature']];
    
    
    /* Data Variables */
    $Name = $_POST['Name'];
    $Telephone = $_POST['Telephone'];
    $Email = $_POST['Email'];
    $Nature = $_POST['Nature'];
    $Questions = $_POST['Questions'];
    
    
    $body = <<<EOD
    <br><hr><br>
    Name: $Name <br>
    Telephone: $Telephone <br>
    Email: $Email <br>
    Nature: $Nature <br>
    Questions: $Questions <br>
    EOD;
    $headers = "From: $Email\r\n";
    $headers .= "Content-type: text/html\r\n";
    $success = mail($my_email, $emailSubject, $body,
    $headers);
    So, basically, in the php above, within the 'body' i.e. the information being emailed, I have "Nature: $Nature", which returns the option value. I am trying to get it to return the option text itself to clarify the nature of the enquiry ( there are a couple of recipients who will receive emails for more than 1 type of enquiry and I need to show them which one it covers)Example: Assuming a user selects Hall Hire, it will currently send "recipient 2". I want it to send "Hall Hire" i.e the option text itself rather than the option value.

    Is there a simple way of doing this ? Can anyone guide me please ?
    Last edited by kimmcms; Jul 4 '14, 04:01 PM. Reason: Issue resolved.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    When the form content is sent to the recipient, it shows the option value rather than the option text, and I cannot work my head around how to get it to show the text.
    that’s simple: you can’t. the form will only transmit the assigned value (name-value pair), nothing else.

    what you can do is putting the value of interest in the value attribute, i.e. if you need "Hall Hire" but not "recipient_ 2" just make "Hall Hire" the value.

    another way were to use a data mapper (in this case a simple array would suffice) where you have the option’s value as array key and the option’s text as array value.

    Comment

    • kimmcms
      New Member
      • Jul 2014
      • 2

      #3
      [RESOLVED] select option text rather than option value

      Doh !

      Thanks Dormilich - couldn't see the wood for the trees when trying to protect the email addresses.

      Have simply changed i.e. 'recipient_2' to 'Hall_Hire' in both html & php and it works perfectly.

      Thanks so much.

      Regards

      Comment

      Working...