In my view I have a popup with a few buttons where user can select multiple options and I keep track what are selected and put them in an array using Jquery like below:
<div class="modal-body">
<div id="step1">
<h2 class="modal-subtitle">Let's get started</h2>
<br>
<p>Hi, I'm Eleven, Product Manager at MaGIC. I'm here to make sure you'll have a good kickstart. Let's have a quick walkthrough.</p>
<button class="btn-forward btn-sd-green" id="btnEndStep1">Forward</button>
</div>
<div id="step2" class="hideMe">
<!-- <h2 class="modal-subtitle"></h2> -->
<br>
<p style="margin-bottom: 20px;">Now let's customise your Central Dashboard based on your interest</p>
<!-- <?php echo $username ?> -->
<div class="section group-services">
<?php foreach ($listServices as $list): ?>
<a class="col-services span_1_of_3">
<?php echo $list['title']?>
</a>
<?php endforeach; ?>
</div>
<button class="btn-forward btn-sd-green" id="btnEndStep2">Forward</button>
</div>
<div id="step3" class="hideMe">
<h2 class="modal-subtitle">That's awesome!</h2>
<br>
<p>See, isn't that quick? </p><p>Now go ahead and explore your Central dashboard. If you need more help, we've prepared everything in <a href="/cpanel/guidelines">Guideline</a></p>
<button class="btn-forward btn-sd-green" id="btnEndStep3">Got it!</button>
</div>
</div>
$(function () {
var selected = [];
$(".col-services.span_1_of_3").click(function (e) {
$(this).toggleClass("active");
e.preventDefault(); /*ignores actual link*/
});
$(".col-services.span_1_of_3").click(function (e) {
if ($(this).hasClass('active')) {
selected.push($(this).text());
console.log(selected);
} else {
var index = selected.findIndex(function() {return $(this).text()});
selected.splice(index, 1);
console.log(selected);
}
});
});
My controller action for this view is as below:
public function actionIndex()
{
//get user's email
$username = Yii::app()->user->username;
//get a list of services available for user to pick
$tmps = HUB::listServiceBookmarkable();
foreach($tmps as $tmp)
{
$result[] = $tmp->toApi();
}
//set user's selection
$setService = HUB::setServiceBookmarkByUser($username, $result);
foreach($setService as $ss)
{
$ssResult[] = $ss->toApi();
}
$this->render('index',array(
'username' => $username,
'listServices'=>$result,
'setResult' => $ssResult
));
}
My question is that how do I pass the array of user’s selection in comma separated format from my view to its necessary api in my controller?:
$setService = HUB::setServiceBookmarkByUser($username, $result);
What’s the best way to do it?