mercredi 27 mai 2015

How to get value of a hidden field based on a checkbox state using jquery

i'am working on a small phalcon project where on a particular page it has a search box to search customers, the search results are then displayed under foreach loop having checkboxes for each results along with a hidden field that has a value. The search results are displayed as an ajax response on button click. On checking a particular checkbox, is there anyway to pass the value of that particular hidden field associated with the checked checkbox . Below the search box, there is a form that takes input values from the user along with the customer name value and id passed from the search results. I tried including jQuery script to pass the hidden field value to an input field, iam getting desired output only when the search result is one, but when the search results are more than one, then only the first hidden field value is passed, even if i check second or third checkbox. I'am a biginner in programming. please help. My code is as follows:

public function editengagementAction($id)
{
    $engmt = Engagement::findFirstById($id);
    $eid=$engmt->customerid;
    $cusdetails= Customerdetails::findFirstById($eid);
    $this->view->objs = $cusdetails;
    if(isset($_POST['searchrslt'])){
        $rst = $_POST['searchrslt'];
        $outputs = $this->db->query("SELECT * FROM customerdetails WHERE name LIKE '%".$rst."%' ")->fetchAll();

        echo"<table width='98%' class='table table-condensed' align='right' id='srchtable' ><thead>";
        echo"<tr><th class='mi_table_head'  style='padding-left:10px;'>User Name</th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'>Company Details</th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'>Email</th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'>Phone NUmber</th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'></th>";
        echo"<th class='mi_table_head'  style='padding-left:10px;'></th></tr>";
        $count=1;
        foreach ($outputs as $value) {
            echo"<tr><td height='40'  class='mi_table_text'  style='padding-left:10px;'>$value[name]</td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'>$value[companyname]<br>$value[companyurl]</td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'>$value[email]</td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'>$value[phonenumber]</td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'><input type='checkbox' name='checkbox'  id='count' value='$value[name]'></td>";
            echo"<td valign='middle' class='mi_table_text'  style='padding-left:10px;'><input type='hidden' id='$count' name='cid' value='$value[id]'></td></tr>";
            echo "<script>
            $(function(){ $('#addsubmit').on('click',function(){
             $('#checkbox').removeAttr('checked');
             var boxChecked = false;
             var customid;
             $('input[type=\"checkbox\"]').each(function() {
              if ($(this).is(':checked')) {
             boxChecked = $(this).val();
             customid=$('input[type=\"hidden\"]').val();
               alert('id is'+ customid);
               }
                });
               $('#custname').val(boxChecked);
               $('#custmid').val(cusid);
             if (boxChecked == false) {
             alert('Please select one customer');
             return false; }
             alert('Customer Changed !');
             return false;
             }); });
             </script>";
            $count++;
        }
        echo"<tr><td colspan='10' align='right'><input type='button' value='Add Customer' id='addsubmit' class='button blue' name='button' onclick='addcustmr()'></td></tr>";
        echo"</tbody><tbody><tbody><tbody></thead></table>";
        exit();
        $this->view->disable();
    }  
  }

here is my screenshot of the above said page with search results displayed with checkboxes.




Aucun commentaire:

Enregistrer un commentaire