Search code examples
javascriptphpconcrete5concrete

how to pass php value from one file to another through java script


I am working with Concrete-5 CMS, I have an issue in passing value form view to controller.In my application I am using following code for displaying employee role.

foreach($rd as $data){
    echo "<tr><td>".$data[role_name]."</td><td>".$data[role_description]."</td><td><a href=".$this->action('edit', $data['role_id']).">Edit</a></td><td>".$ih->button_js(t('Delete'), "deleteRole('".$data['role_id']."')", 'left', 'error')."</td></tr>";
}
<input type="hidden" name="rno" id="rno" />

script:

$delConfirmJS = t('Are you sure you want to remove this Role?'); ?>

<script type="text/javascript">
    function deleteRole(myvar) {
    var role = document.getElementById('rno');
    role.value = myvar;
    if (confirm('<?php echo $delConfirmJS ?>')) {
        $('#rolelist').submit();
        //location.href = "<?php echo $this->url('/role/add_role/', 'delete', 'myvar')?>";
    }
}
</script>

html code I did edit operation by passing role_id through edit action. But, In case of delete i should ask for a conformation, so I use java script to conform it and call the href location and all.

But i don't know how to pass the role_id to script and pass to my controller. how to achieve this task?

thanks Kumar


Solution

  • You can pass value to server using ajax calls. See the following code. Here We use a confirm box to get user confirmation.

    function deleteEmployee(empId){
       var confirm=confirm("Do you want to delete?");
       if (confirm)
       {
         var url = "path/to/delete.php";
         var data = "emp_id="+empId;
         $.ajax({
           type: "POST",
           url: "otherfile.php",
           data: data ,
           success: function(){         
              alert("Employee deleted successfully.");
           }
         });
      }
    }
    

    In delete.php you can take the employee id by using $_POST['emp_id']