How to pass single page value to controller through script

Permalink 1 user found helpful
In my application list out the employee role by using for each
single page:
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()", 'left', 'error')."</td></tr>";

<script type="text/javascript">
   deleteRole = function() {
      if (confirm('<?php echo $delConfirmJS ?>')) { 
         location.href = "<?php echo $this->url('/role/add_role/', 'delete')?>";

to edit the role i use the href link to pass the value to controller but in the case delete, i want conformation from user so i use button_js call the script and validate it but i dnt know how to pass the corresponding value(role_id) to controller ?

help please:-)


View Replies:
bbeng89 replied on at Permalink Reply
It's a really bad idea to be able to delete objects with a GET request in your site. What I always do for my delete buttons is just make them a form that just has a hidden input for the ID of the object I want to delete. Or you can also pass the ID in the parameter to the action. Something like this:
<form action="<?php echo $this->action('delete', $idToDelete); ?>" method="POST">
    <button type="submit"><?php echo t('Delete'); ?></button>

Then the controller method looks like this:
public function delete($id){
   //do stuff
JohntheFish replied on at Permalink Reply
Also in your form, include a token to verify that the post is genuine:
shankumars replied on at Permalink Reply
thanks for reply, i have small query i am using ajax to pass the value but my query how to full url in concrete.
in this code write in single page how to call the corresponding controller page? Its right or wrong?
   type: 'post',
   data: {   myvar: myvar },
   url: "<?php echo $this->url('/role/add_role/', 'delete', 'data')?>",