kienthucnet Posted October 13, 2015 Share Posted October 13, 2015 Hi, I have a form the uses PHP mail to email inputted form data to an email address. The form has has 4 select fields which contain the same 10 colours in each. For example, when a user selects Light Green from the first select field, the value of #66FF66 is assigned to a variable e.g. (color-select-1-hex, color-select-2-hex, color-select-3-hex and color-select-4-hex). What I need is advice on how I go about creating a loop statement which runs through each of the 4 select field user inputs, creates a new variable (e.g. color-select-1-name) and assigns 'Light Green' to it. I need to keep color-select-1-hex equal to #66FF66. What is the recommended approach here? Thanks for any help! Quote Link to comment https://forums.phpfreaks.com/topic/298565-php-loop-statements-for-form-variables/ Share on other sites More sharing options...
Ch0cu3r Posted October 13, 2015 Share Posted October 13, 2015 What is the recommended approach here? To not use separate variables. You should be using arrays You would start by naming your <select> fields as color[] <select name="color[]"> ... color options ... </select> This will mean when you submit the form $_POST['color'] will be an array containing all four selected values from the select fields To loop over the values you simple use a foreach loop echo "Your chosen colors are: "; foreach($_POST['color'] as $color) { echo "$color<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/298565-php-loop-statements-for-form-variables/#findComment-1523148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.