I have a problem with symfony, twig and jquery,
I want to get the contact list from the database, and after that, by clicking on the button, the contact list of the phone should be added to the field under the name phone_list.
In my controller, I:
$connection = $em->getConnection();
$statement = $connection->prepare("select * from contact where id_user=$id");
$statement->execute();
$contacts = $statement->fetchAll();
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$jsonContent = $serializer->serialize($contacts, 'json');
In the javascript area, I have this code:
if($(this).is(":checked")) {
var contacts_json = $.parseJSON("{{ contacts|json_encode() }}");
var points = [];
var ii = 0;
for (var i in contacts_json) {
points[ii] = contacts_json[i]["id"];
i++;
}
$('#destinationList').val(points.join(';'));
}
else {
$('#destinationList').val("");
}
});
In Twig, I put {{ dump(contacts) }}and the result appeared:

but when I try to execute a function, I get an error in the console,
L'usage de "getPreventDefault ()" est obsolète. Utility "defaultPrevented" à la place. SyntaxError: JSON.parse: expected property name or '}' in row 1 of column 3 of JSON data
source
share