1

I have a checkbox down below...
It is in the loop :

<script>
                    function checkCheckBoxes_abel() { //check if the checkbox is checked before submitting.
                        if (document.payform.pay_checkbox.checked == false)
                            {
                                alert ('You didn\'t choose any of the checkboxes for payment !');
                                return false;
                            }
                        else
                            {
                                alert ('One or more checkboxes from payment form are checked!');
                                document.forms["payform"].submit(); 
                                return true;
                            }
                        }
                    </script>    

<form name="payform" onsubmit="return checkCheckBoxes_abel();" method="POST" action="payment.php">    
    for($record_count=0;$record_count<$record;$record_count++)
                    {
        <td><input type="checkbox" name="pay[]" id="pay_checkbox" value="<?php echo $amount_dueArr[$record_count];?>" onClick="checkTotal()"/></td>
        }
    </form>

How can I pass the value of the checkbox that is being selected ?

Thanks

can I do :

if (isset($_POST['pay']))
  { 
     foreach($_POST["eg_payamt_"] as $key => $payamt){
            echo "eg_payamt_$key => $payamt\n <br>"; 
        }
  }

on payment.php ?

Thanks

An illustration : I have three checkboxes...

If I check one of the checkbox,

Checkbox ticked on : Array

and if I'm not checking any of them

Checkbox ticked on :

Which is correct, but the content of the Array is not only one but three of them, How can I make it only one ? or only two ? depends on how many checkboxes are being checked.

can I do it on another field ? it seems that it works only for one field

if (isset($_POST['pay']))
    { 
      if(is_array($_POST['pay']))  
      {
     //foreach($_POST["pay"] as $key => $desc)
     foreach($_POST["eg_description_"] as $key => $desc)
        {
            echo "eg_description_$key => $desc\n <br>"; 
        }
      }
      else
      {
         //echo 'description :'.$_POST['pay'];
         echo 'description :'.$_POST["eg_description_"];
      }
    }
Robin Michael Poothurai
  • 5,444
  • 7
  • 23
  • 36
Rick Ant
  • 230
  • 4
  • 15
  • Here's a similar question/answer: http://stackoverflow.com/questions/476426/submit-an-html-form-with-empty-checkboxes – EJK Dec 22 '11 at 04:36
  • you can do in that way, perform the foreach @ $_POST['pay'] – Punit Dec 22 '11 at 05:14

2 Answers2

0

there are 2 types of values will receive in POST, if someone selects only one checkbox that will throw warning in foreach loop so you can try this way

if (isset($_POST['pay']))
{ 
    if(is_array($_POST['pay']))  {
      //foreach($_POST["eg_payamt_"] as $key => $payamt){ 
      foreach($_POST["pay"] as $key => $payamt){
        echo "eg_payamt_$key => $payamt\n <br>"; 
      }
    }
    else  {
       echo 'pay : '. $_POST['pay'];
    }
}
Robin Michael Poothurai
  • 5,444
  • 7
  • 23
  • 36
  • I don't check any checkboxes, but when I click submit, it still displays all the pay_amt in the payment.php – Rick Ant Dec 22 '11 at 06:34
  • @Rick Ant I have edited My answer, Can you tell , from where you are posting 'eg_payamt_' value and possible to show all codes related to showing checkbox in payment.php – Robin Michael Poothurai Dec 22 '11 at 06:58
  • echo 'AU$ '.$amount_dueArr[$record_count].' – Rick Ant Dec 22 '11 at 07:08
  • payment.php $pay = $_POST['pay']; for displaying payment being checked if (isset($_POST['pay'])) { if(is_array($_POST['pay'])) { foreach($_POST["eg_payamt_"] as $key => $payamt){ echo "eg_payamt_$key => $payamt\n
    "; } } else { echo 'pay : '. $_POST['pay']; } }
    – Rick Ant Dec 22 '11 at 07:09
  • yes , you can do that. You can use that still it has POST values – Robin Michael Poothurai Dec 22 '11 at 08:00
  • if I put "pay" it will display ammount not a description...? I want to display description – Rick Ant Dec 22 '11 at 08:06
  • if I put "description" it will display all of the descriptions, ignoring which one I check or not. – Rick Ant Dec 22 '11 at 08:07
  • if (isset($_POST['pay'])) { if(is_array($_POST['pay'])) { //foreach($_POST["pay"] as $key => $desc) foreach($_POST["eg_description_"] as $key => $desc) { echo "eg_description_$key => $desc\n
    "; } } else { echo 'description :'.$_POST['pay']; } } ?
    – Rick Ant Dec 22 '11 at 08:12
  • how to use POST values ? I've tried and no luck yet for description and another field – Rick Ant Dec 22 '11 at 08:20
  • if (isset($_POST['pay'])) { if(is_array($_POST['pay'])) { //foreach($_POST["pay"] as $key => $desc) foreach($_POST["eg_description_"] as $key => $desc) { echo "eg_description_$key => $desc\n
    "; } } else { //echo 'description :'.$_POST['pay']; echo 'description :'.$_POST["eg_description_"]; } }
    – Rick Ant Dec 22 '11 at 08:43
  • When I ticked two checkboxes, it displayed 3 description data eg_description_0 => CINV159012 eg_description_1 => CINV158729 eg_description_2 => CINV158627 , it should be only two field of datas – Rick Ant Dec 22 '11 at 08:52
  • echo $invoiceArr[$record_count] .'
    '; //display the invoice number (DocumentID)
    – Rick Ant Dec 22 '11 at 09:00