Email values only from form fields that are selected

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dthomas31uk
Forum Newbie
Posts: 19
Joined: Mon Oct 13, 2008 8:14 am

Email values only from form fields that are selected

Post by dthomas31uk »

This might seem long winded guys. But I am struggling with this would be great if you could help me out.

Right have got a form working fine with various textboxes etc on my form all working fine and when submitted they are emailed to me. All fine.

Anyway I have now got a couple of select boxes, they are for a removal company. The user selects if they need moving i.e. one selection box is 2 seater sofa and the other selection box is 3 seater sofa. If the user needs these moving they select a checkbox and they can enter the quantity in the selection. This all works see the code below

Code: Select all

<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>


This is handled with javascript with the following code

Code: Select all

<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>
What I am struggling with is that I need these totals emailed to me.

I think it needs to be in a loop like the following but I am stuck. Any ideas??

Code: Select all

for ($i = 0; $i < $numberOfCheckboxes; $i++) {
        if (isset($_POST['chk' . $i])) {
        $items = trim($_POST['txt' . $i]);
        }
        }
Hope someone can help. Cheers
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Email values only from form fields that are selected

Post by AbraCadaver »

If I understand you correctly, then yes, something like that will work if you know the $numberOfCheckboxes. Are you wanting to total the values of the checkboxes? Normally I would use an array of checkboxes name="chk[]" or name="chk[1]", etc. but you would need to change your javascript in that case. This uses your code and totals:

Code: Select all

$items = 0;
for ($i = 0; $i < $numberOfCheckboxes; $i++) {
        if (isset($_POST['chk' . $i])) {
            $items += trim($_POST['txt' . $i]);
        }
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dthomas31uk
Forum Newbie
Posts: 19
Joined: Mon Oct 13, 2008 8:14 am

Re: Email values only from form fields that are selected

Post by dthomas31uk »

Hi AbraCadaver.
Yeah iam struggling with this to be honest. The form works all ok, when the user hits the 2 seater sofa checkbox they can select a quantity. Likewise with a 3 seater sofa.

But I cannot get the quantity values emailed to me based on user selection.

I think I might need to change each field to an array, but to be honest I am not sure.

Have tried the code you gave me AbraCadaver without using an array, had no errors but just got a value of 0 in my email.

Should I change each option field name to name="chk[]" , but I will then need to change the javascript. Any ideas so you can put me in the right direction

Thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Email values only from form fields that are selected

Post by AbraCadaver »

Do a test post to the receiving page and do this, and post it here:

Code: Select all

echo "<pre>".print_r($_POST, true)."</pre>";
Also include what you selected on the form to get this output.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply