Email values only from form fields that are selected

Hi. I have form fields that when a user selects a checkbox they then enter a value in the option selection box. All works well, but I now need these values emailed, but only the ones that have been selected in the checkbox.

Here is the code

<li class="span-7"> 
                 
          <label for="2_sofa">2 Seater Sofa:</label>
         <input type="checkbox" name="chk1" value="1" OnClick="fncEnable(1)">
         <label for="2_sofa">Quantity</label>
         <select id="txt1" name="txt1" value="" DISABLED CLASS="disabledclass" />
         <option value=" " >- Please select -</option>
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
                    <option value="4">4</option>
		 </select>
                 </li>
                 
                 <li class="span-7 last"> 
         
         <label for="3_sofa">3 Seater Sofa:</label>
         <input type="checkbox" name="chk2" value="2" OnClick="fncEnable(2)">
         <label for="3_sofa">Quantity:</label>
         <select id="txt2" name="txt2" value="" DISABLED CLASS="disabledclass" />
         <option value=" " >- Please select -</option>
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
                    <option value="4">4</option>
		 </select>
         
                 
                 </li>
                 

Here is the javascript that works it out

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
   <!--
   function fncEnable(chknum)
   {
               if(document.frm["chk"+chknum].checked == true)
            {
                  document.frm["txt"+chknum].className = 'enabledclass';
                  document.frm["txt"+chknum].disabled = false;
                  
            }
            else
            {
                  document.frm["txt"+chknum].className = 'disabledclass';
				  document.frm["txt"+chknum].value = '';
                  document.frm["txt"+chknum].disabled = true;
                 
            }
   }  
   //-->
   </SCRIPT>

I use the following php to email other form field values and need something like the following. Any ideas woud be most appreciated. Thanks

if(trim($_POST['floor']) === '')  {
			$floorError = 'Please enter floor number';
			$hasError = true;
		} else {
			$floor = trim($_POST['floor']);
		}

There’s no need to filter them beforehand. Instead, process them with PHP.

If a checkbox value exists (chk1) then you do something with the form value (txt1)


for ($i = 0; $i < $numberOfCheckboxes; i += 1) {
    if (isset($_POST['chk' . $i])) {
        // do stuff with ['txt' . $i]
    }
}

That ensures that your form will work in all circumstances, regardless of whether client-side scripting is working or not.

After your server-side code is working well, you then apply over the top a thin veneer of JavaScript that helps the end-user by disabling or hiding the text fields until after a checkbox is checked.

Once you have your PHP code in place (for safety), only after that is when you should consider client-side scripting such as JavaScript.

If you wrote a script containing this;


var_dump( $_POST )

… and made that script the target for your forms’ action …

then you would be able to see what was being sent to your script, click and unclick some boxes to see how the values change.

Either make conditional tests for each form element, or programatically loop through them looking for those which have been clicked or had data entered.

If you cannot work it out from there, distill that down to ONE OR TWO form elements, and post small examples of your html and the results of var_dump( $_POST ) and someone will be able to help you - but sending us a load of html and javascript to wade through is not conducive to getting you loads of quick answers.

Edit:

I see somone kind replied, but I’ll leave this advice, you need to learn how to divide and conquor if you want any chance of learning how to debug and develop yourself

I see what you are getting at pmw57, but the problem is when they are emailed that way I wont know which quantity for each item. Cause all that get emailed is the actual quantity numbers


<select id="sofa2" name="sofa2">
<option value=>Pick one ...</option>
<option value=1>1</option>
<option value=2>2</option>
</select>

Try my suggestion, make a simple stripped down form containing the above and look closely at the output of var_dump()

Cups all of the checkboxes and textboxes are working as I want them, when a user clicks a checkbox they can enter a value in a textbox etc.

The problem I am having is I need to get the values from each textbox emailed via PHP.

I think PMW57 is on the right track with his code, but aint got my head around it yet

for ($i = 0; $i < $numberOfCheckboxes; i += 1) {
if (isset($_POST[‘chk’ . $i])) {
// do stuff with [‘txt’ . $i]
}
}

Am struggling guys have tried the code u gave me PMW57. Entered the following

for ($i = 0; $i < $numberOfCheckboxes; i += 1) {
if (isset($_POST[‘chk’ . $i])) {
$items = trim($_POST[‘txt’ . $i]);
}
}

Get the error T_PLUS_EQUAL not needed, so presume thats to do with i += 1

Any ideas please. Am not sure the line I have added is correct as well

$items = trim($_POST[‘txt’ . $i]);

but then I send

$emailTo = ‘myemail.co.uk’;
$subject = 'Home Removal Quote from '.$name;
$sendCopy = trim($_POST[‘sendCopy’]);
$body = “Items required: $items”;

for:
($i = 0; $i < $numberOfCheckboxes; i += 1)

try:
($i = 0; $i < $numberOfCheckboxes; $i++)

Have tried what you told me cups no errors now, but nothing is being emailed out of the selected textboxes

I’m about to head off to work now, butit seems that you require a bit more than simple advise. I would suggest that you get in touch with someone so as to arrange a more one-on-one role of assistance.