Notify controller of pressed button
How do I notify the controller when the delete button is pressed?
MyController.php
public function actionUpdate($id)
{
isset($_POST['del'])
{
// delete user.
}
}
MyView.php
<?php
echo '<button type="button" class="btn btn-danger" name="deleteButton">Delete User</button>';
?>
This is what I tried:
$('.deleteButton').click(function()
{
var clickBtnValue = $(this).val();
var ajaxurl = Yii::app()->basePath . '/controllers/MyController.php';
del = {'action': clickBtnValue};
$.post(ajaxurl, del, function (response)
{
alert("action performed successfully");
});
});
But I can't get a notification on the controller that the button is clicked.
+3
source to share
1 answer
- You seem to be using jQuery.
$('.deleteButton')
selects an element by class, not by name.$('[name="deleteButton"]')
should be used instead. - You cannot inject PHP code into JS this way. If your JS is written in view, you must wrap PHP code
<?=Yii::app()->basePath ?>
. - Yii uses a url manager. This means the url is not equal to the path to the controller file. Create url from view
<?=$this->createUrl('my/update', array('id' => $userId) ?>
. Lear is more about URL management .
+2
source to share