I'm currently utilizing Fullcalendar for Yii2 and I am looking to save events via Ajax when clicking on a date. The data comes from a dropdown list.
It seems like my code is unable to locate the function in my controller, possibly due to the URL?
Here is my view with JavaScript along with the link to my Ajax function in the controller:
<?php
$form = ActiveForm::begin();
?>
<div class="row">
<div class="col-md-4">
<?= $form->field($feuille_de_jour_responsable, 'ID_Categorie')->dropDownList(CategorieFdj::find()->select(['Nom', 'ID_Categorie'])->indexBy('ID_Categorie')->column(), ['id'=>'catId']); ?>
</div>
<div class="col-md-4">
<?= $form->field($feuille_de_jour_responsable, 'ID_Poste_FDJ')->dropDownList(PosteFdj::find()->select(['Nom_Poste_FDJ', 'ID_Poste_FDJ'])->indexBy('ID_Poste_FDJ')->column(), ['id'=>'postId']); ?>
</div>
<div class="col-md-4">
<?= $form->field($feuille_de_jour_responsable, 'Code_Personnel')->dropDownList(Personnel::find()->select(['Nom_Personnel', 'Code_Personnel'])->indexBy('Code_Personnel')->column(), ['id'=>'codePers']); ?>
</div>
</div>
<?php ActiveForm::end();?>
<?php
$JSCode = <<<EOF
// JavaScript code goes here...
EOF;
$JSEventClick = <<<EOF
function(calEvent, jsEvent, view) {
alert('Event: ' + calEvent.title);
}
EOF;
?>
<!-- Render FullCalendar widget -->
<?= yii2fullcalendar\yii2fullcalendar::widget([
'id' => 'calendar',
'clientOptions' => [
'height' => 650,
'selectable' => true,
'selectHelper' => true,
'droppable' => true,
'editable' => true,
'fixedWeekCount' => false,
'defaultDate' => date('Y-m-d'),
'eventClick' => new JsExpression($JSEventClick),
'select'=>new JsExpression($JSCode)
],
]);
?>
<?= Html::encode($JSCode); ?>
<?= Html::encode($JSEventClick); ?>
And here's the function in my controller (FeuilleDeJourResponsableController)
public function actionCreate()
{
// PHP code for creating an event...
}
When using firebug, I noticed that upon clicking, nothing gets saved without any errors appearing. I assumed that by linking "POST" (similar to submitting a form), my "create" function would save the data, but it remains unsuccessful. The "if" statement doesn't execute, and I'm unsure why.
An exception was found stating: "SyntaxError: unexpected token < in JSON at position 0"
Any help or guidance would be greatly appreciated! Sarah