Reputation: 177
I have the problem that I print the DIV but the content of the foreach is empty and I print NaN, I started to move the accents and quotation marks and now I throw error of a token <. I have been fighting with this for 2 hours and I can't figure out how to make it work.
function activateButtonSend(action){
var addButton = "";
var php = "";
if (action === true) {
php += @php
App\Models\Activity::getOptionsForMail()->map(function($name,$id){
echo "<a class='dropdown-item btn-act-".$id."' href='javascript:sendComments(".$id.");'>".$name."</a>";
});
@endphp
addButton = "<div class='f-flex float-right' id='dropdown-activities'>\
<button id='content-message' style='display: none' type='button' class='btn btn-lg iq-bg-primary-abs dropdown-toggle'\
data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>\
Guardar\
</button>\
<div class='dropdown-menu' aria-labelledby='btnGroupDrop1'>'"+php+"'</div>\
</div>";
}else{
addButton = '<button type="submit" id="content-message" style="display: none" class="btn iq-bg-primary-abs btn-lg float-right action-button-submit">Guardar</button>';
}
$('#type_buttons').html(addButton);
}
Message error console
It prints the results but throws an error because it does not take the tilde to join it inside the php variable of jquery.
@php is the abbreviation of <?php ?>
Upvotes: 0
Views: 29
Reputation: 781340
You're generating JavaScript code, but you're not putting quotes around the JS string. The best way to turn a PHP value into a JavaScript literal is to call json_encode()
when echoing.
function activateButtonSend(action){
var addButton = "";
var php = "";
if (action === true) {
php += @php
App\Models\Activity::getOptionsForMail()->map(function($name,$id){
echo json_encode("<a class='dropdown-item btn-act-".$id."' href='javascript:sendComments(".$id.");'>".$name."</a>");
});
@endphp
addButton = "<div class='f-flex float-right' id='dropdown-activities'>\
<button id='content-message' style='display: none' type='button' class='btn btn-lg iq-bg-primary-abs dropdown-toggle'\
data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>\
Guardar\
</button>\
<div class='dropdown-menu' aria-labelledby='btnGroupDrop1'>'"+php+"'</div>\
</div>";
}else{
addButton = '<button type="submit" id="content-message" style="display: none" class="btn iq-bg-primary-abs btn-lg float-right action-button-submit">Guardar</button>';
}
$('#type_buttons').html(addButton);
}
Upvotes: 1