I need to extend an online application by a translation functionality.
The application consists of a frontend (HTML+JS) and a backand (PHP), which is connected to a database.
The frontend and backend communicate via Ajax (jQuery).
From my colleague I got another PHP file with a function func
which takes a word as a parameter. It then looks up its definition in the database, according to the set language, and returns the translated word.
Suppose I have a simple HTML form, that would look like this:
<?php echo func("Name"); ?>: <input type="text" id="name" />
<?php echo func("occupation"); ?>: <input type="text" id="address" />
...
If the language was set to French, the output would look like:
Prénom: <input type="text" id="name" />
Profession: <input type="text" id="address" />
But in my case all the output comes dynamically from the database via Ajax (jquery). So I assume, I would need to put PHP into my JS script.
When I try:
function getData() {
$.ajax({
type : "GET",
url : "backend.php",
data : {
q : "query",
attribut : "attr",
},
success : function(data) {
$.each(data, function(i, item) {
$('.myDiv').append("<button>" + <?php echo htmlspecialchars(tr(" + item.value + "))?> + "</button>");
},
error : function(jqXHR, status, error) {
console.log(status, error);
}
});
}
In this case I get the following error:
Uncaught SyntaxError: Unexpected token <
PS: The actual translation function in the PHP script comes from me colleagues, so I don't have any influence here, I just need to get it work.