Jump to content

Extract Dropdown menu value to email (only index value arriving)


mcange77

Recommended Posts

Hi,

 

I'm not a confident coder but can normally manage the things I wish to achieve.

 

I have a PHP form, with a dropdown menu on it.

 

The menu is created using an array because I wish to make all the fields on the form sticky.

This I have accomplished.

 

The content of the form is sent by email to the recipient - no database's are involved.

 

I have successfully built the email body, for all the fields.

The drop down menu however provides me with the correct index location of the selected value - but not the selected value itself. How do I get this to show?

 

The PHP code that creates and populates my drop down menu is as follows:

_______________________________________________________________

<?php // drop down box for duration types

$arrayOfDurationTypes = array('Hours', 'Days', 'Weeks', 'Months');

 

if(isset($_POST['btnSend'])) {

  //$sticky = 4;

  $sticky = $_POST['lstHoursDaysWeeksMonths'];

}

else {

$sticky = '';

}

 

echo '<select name="lstHoursDaysWeeksMonths" size="1" id="lstHoursDaysWeeksMonths">'; //create the open list html tag code

 

//populate the drop down with each item of the array

for($counter = 0; $counter < sizeof($arrayOfDurationTypes); $counter++) //10 items in the array located in indexed positions from 0 to 9

{

$index = key($arrayOfDurationTypes); //the position in the array of the value the user has selected

$value = current($arrayOfDurationTypes); //the value located at the selected position

 

if ($counter == $sticky) {

echo '<option value="' . $index .'" selected>' . $value . '</option>'; }

else {

echo '<option value="' . $index .'">' . $value . '</option>';

}

 

next($arrayOfDurationTypes); //populates each item in arrayOfDurationTypes within the drop down list

}

echo '</select>';

?>

____________________________________________________________

 

This works on my web page as a sticky field - I have no concerns here.

 

The php line of script that then handles the fields data and compiles it into an email is as follows:

 

____________________________________________________________

 

$message .= "Required Duration: $lstHoursDaysWeeksMonths\n\n"; //returns correct index but not the value

____________________________________________________________

 

I've tried using the implode function, but as it is not an array being retrieved it does not work. There is just one item of data held in $lstHoursDaysWeeksMonths, and that is the index of the selected value.

I simply want the value to show in the email body not the index.

 

Please help - its driving me crazy.

 

Angela :-)

 

Link to comment
Share on other sites

In option-tags, whatever is set as the value-attribute is what you'll get in post.  So put what you want there.  If you don't want the index, then don't assign value="$index"; instead, assign value="$value".

 

<?php
$selected = $counter == $sticky ? ' selected="selected"';
$htmlval = htmlentities( $value, ENT_QUOTES );
echo sprintf( '<option value="%s"%s>%s</option>', $htmlval, $selected, $htmlval );
/* replaced by above
if ($counter == $sticky) {            
               echo '<option value="' . $index .'" selected>' . $value . '</option>';               }
            else {
               echo '<option value="' . $index .'">' . $value . '</option>';
               }
*/
?>

Link to comment
Share on other sites

Hi again,

 

I have worked out a solution....I was making it way to complicated......

 

Here is the code for the form list control.

___________________________________

  <label>

      <?php

if(isset($_POST['btnSend'])) { $stickyDay = $_POST['lstHoursDaysWeeksMonths'];

}

else { $stickyDay = 'Hours'; //the default is set to 'Hours'

}

?>

      <select name="lstHoursDaysWeeksMonths" size="1" id="lstHoursDaysWeeksMonths">

        <option value="Hours" <?php if ($stickyDay == 'Hours') { echo 'selected="selected" ';} ?>>Hours</option>

        <option value="Week" <?php if ($stickyDay == 'Week') { echo 'selected="selected" ';} ?>>Week</option>

        <option value="Day" <?php if ($stickyDay == 'Day') { echo 'selected="selected" ';} ?>>Day</option>

        <option value="Month" <?php if ($stickyDay == 'Month') { echo 'selected="selected" ';} ?>>Month</option>

      </select>

      </label>

___________________________

With the message built as follows;

$message .= "Required Duration: $lstNumOfHoursDaysWeeks ";

 

Now the list box works as a sticky form field if the user enters incorrect data and is asked to correct they dont have to reenter all fields. And the content of the list box is sent by an email.

 

Easy once you know how! LOL

 

Thanks for your help anyway.

 

Ange

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.