0

I have 3 tables say:-

invoice_details, customer_details and bill_address.

The invoice form has a dropdown and a checkbox. Customer names are listed in the dropdown from the customer table. And when I select a customer name the checkbox values must have the corresponding billId.

so when I mark the checkbox, the value of checkbox must be the billId of that customer name that has been selected. I hope my question is clear.

view page:-

<div class="row space">
 <label class="control-label">Customer <span class="manda">*</span></label>
 <select class="form-control" name="customerId" id="customerId">
   <option value="0">Choose....</option>
     <?php 
      if ($customer) {

       foreach ($customer as $row) {

        echo "<option value='".$row->customerId."'>".$row->customer_name."</option>";
      }

     } ?>

     </select>
</div> 
<div class="row space">
 <div class="col-sm-6">
  <input type="checkbox" name="checkbox" value="bill">
  <label>Bill To</label>
 </div>
</div>

How could I do this? please help me...

customerId is common in all 3 tables.

Table details:-

bill_address(billId, customerId, street, city, state, country).
customer_details(customerId, customer_name, phone, email,..).
invoice_details(invoiceId, customerId, billId, date, status,..).
5
  • you know ajax in codeigniter??? Commented Aug 9, 2020 at 3:39
  • have your problem is solved or not , please let me know? Commented Aug 9, 2020 at 6:32
  • yes sir, I know ajax. Commented Aug 9, 2020 at 9:17
  • yes sir, I got solution. thank you @KUMAR. But I made a few changes in code. foreach($BillTableDta as $bill) { $billData.='<input type="checkbox" name="billcheckbox" value="'.$bill.'">'; } as using for each 9 check box appears i need only one to appear. its working fine. Commented Aug 9, 2020 at 9:31
  • ok , its good to hear its working for you. Commented Aug 9, 2020 at 10:23

2 Answers 2

1

view page of Invoice Form:-

<div class="row space">
 <label class="control-label">Customer Name<span class="manda">*</span></label>
 <select class="form-control" name="customerId" id="customerId">
   <option value="0">Choose Customer Name</option>
     <?php 
      if ($customer) {

       foreach ($customer as $row) {

        echo "<option value='".$row->customerId."'>".$row->customer_name."</option>";
         }
       }
      ?>

     </select>
</div> 
<div class="row space">
 <div class="col-sm-6">
  <div id='billData'></div>
 </div>
</div>

Jquery Ajax Code:- Add this code in Invoice Form view Page before closing </body> Tag.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

<script>
$(document).ready(function() {
    $("#customerId").on("change",function(){
    var customerId = $(this).val();
 
    $.ajax({
         url : "<?php echo base_url('controler_Name/get_data_in_bill_checkbox') ?>",
         type: "post",
         data: {"customerId":customerId},
         success : function(data){
            //alert(data);
    $("#billData").html(data);
         }
    });
});
});
</script>

Contoller Code:-

public function get_data_in_bill_checkbox(){
    
    $customerId = $this->input->post("customerId");

    $BillTableDta = $this->db->get_where('bill_address',array('customerId'=>$customerId))->row_array();
           
           $billData = "<label>Bill To</label>";
              foreach($BillTableDta as $bill)
              {
                $billData.='<input type="checkbox" name="billcheckbox" value="'.$bill.'">';
              }
               echo $billData;
           }

Note:- For more Reference see this https://api.jquery.com/change/

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the following let me know if it worked using the triangle.:-)

<div class="row space">
    <form action="" method="POST">
        <label class="control-label">Customer <span class="manda">*</span></label>
        <select class="form-control" name="customerId" onchange="this.form.submit()" id="customerId">
        <option value="0">Choose....</option>
        <?php 
            if ($customer) {

            foreach ($customer as $row) {

            echo "<option value='".$row->customerId.'/'.$row->customer_name."'>".$row->customer_name."</option>";
            }

            } 
        ?>
        </select>
    <?php
        if(isset($_POST['customerID'])){
        $true=checked;
        $data=explode("/",$_POST['customerID']);
        $id=$data[0];
        $name=$data[1];
        }
        else {
        $true="";
        }
    ?>
</div> 
<div class="row space">
    <div class="col-sm-6">
        <input type="checkbox" <?php if(isset($true){ echo $true; } ?> name="checkbox" value="<?php if(isset($id){ echo $id; } ?>"> <!-- you can change this $id to $name based on your need.-->
        <label><?php if(isset($name) { echo $name; } ?></label>
    </div>
</div>
</form>

6 Comments

I am having an error. undefined id. sir @waanofii.tech both dropdown and checkbox are in a form with a submit button.
edited the answer typos error change implode to explode.and move the closing form tag to the bottom
A PHP Error was encountered Severity: Notice Message: Undefined variable: id Filename: manger_views/invoice.php Line Number: 330 iam getting this error. what should i do? @waanofii.tech
check now i have made some changes in the last section of the code on check box and values around it :-)
I got solution above code is working. Thanks @waanofii.tech
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.