1
I’m having a hard time sending the contents of a variable JS
for a <textarea>
(in a bootstrap modal) and with XML formatting defined in the variable. Both the JS
as to textarea
are in the same html. The code is based on this (which only generates html results): http://www.yogihosting.com/implement-tmdb-api-with-jquery-ajax
This is the variable:
var xmlString = "<channel>" + " <title><![CDATA[" + title + "|"+ to + "(" + ano + ")]]></title>" + " <logo_30x30><![CDATA[" + image + "]]></logo_30x30>" + " <description><![CDATA[<center><img height=\"254\" width=\"200\" src=\"" + poster + "\"</img><br>"+ sin +"</center>]]></description>" + " <stream_url><![CDATA[" + escape(country) + "]]></stream_url>" + " <subtitle><![CDATA[" + escape(zip) + "]]></subtitle>" + "</channel>";
And this is the textarea:
<textarea class="texto" id="ctext" name="ctext" cols="86" readOnly></textarea>
Can anyone help? If there is a suggestion to improve this part of the code, you are welcome.
I edited to include the full code:
<!DOCTYPE html>
Search on Tmdb
<div class="container">
<div id="apiDiv">
<h4>Digite o Título do Filme em Qualquer Língua e Clique em Buscar</h4>
<input type="text" id="searchInput" placeholder="Busca Filme no TMDb" />
<button id="submit">Buscar</button><button id="reset">Limpar »</button>
<div class="imageDiv">
<img src="Image/loading.gif" />
</div>
<div id="message">
</div>
</div>
<ul id="pagination"></ul>
</div>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title" id="modalTitleH4"></h4>
</div>
<div class="modal-body" id="modalBodyDiv">
</div>
<div class="modal-footer">
<p><textarea class="texto" id="ctext" name="ctext" cols="86"></textarea></p>
<button class="button" id="clipboard" onclick="clipboard()">CopiarXML</button>
<button id="bd" type="button" class="btn btn-default">Add MySQL</button>
<button id="close" type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="js/jquery.twbsPagination.js"></script>
<script>
$(document).ready(function () {
$("#reset").click(function (e) {
location.reload();
});
$("#submit").click(function (e) {
var validate = Validate();
$("#message").html(validate);
if (validate.length == 0) {
CallAPI(1);
}
});
$("#message").on("click", ".result", function () {
var resourceId = $(this).attr("resourceId");
$.ajax({
url: "https://api.themoviedb.org/3/movie/" + resourceId + "?append_to_response=credits&language=ptbr&include_image_language=pt-BR,null",
data: {
api_key: ""
},
dataType: 'json',
success: function (result, status, xhr) {
$("#modalTitleH4").html(result["name"]);
var title = result["title"] == null ? "No information available" : "" + result["title"];
var image = result["poster_path"] == null ? "Image/no-image.png" : "https://image.tmdb.org/t/p/w185/" + result["poster_path"];
var poster = result["backdrop_path"] == null ? "Image/no-image.png" : ""+ "https://image.tmdb.org/t/p/w500/" + result["backdrop_path"];
var to = result["original_title"] == null ? "No information available" : ""+ result["original_title"];
var sinopse = result["overview"] == null ? "No information available" : "" + result["overview"];
var ano = result["release_date"] == null ? "No information available" : ""+ result["release_date"];
var resultHtml = "<p class=\"text-center\"><img src=\"" + image + "\"/></p><p>" + title + "</p>" + to + "</p>" + poster + "</p>" + ano + "</p>" + sinopse + "</p>";
//resultxml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"+"<items>"+"\n<playlist_name><![CDATA["+"]]></playlist_name>"+"\n\t<channel>"+"\n\t\t<title><![CDATA[<font color=\"#ffff00\">"+title+ "|" + to + "(" + ano + ")" + "</font>]]></title>"+"\n\t\t<logo_30x30><![CDATA["+image+"]]></logo_30x30>"+"\n\t\t\<description><![CDATA[<center><img height=\"254\" width=\"200\" src=\""+poster+"\"/><center><br>" +sinopse+ "</center>]]></description>"+"\n\t\t<playlist_url><![CDATA["+"]]></playlist_url>"+"\n\t\t<stream_url><![CDATA["+"]]></stream_url>"+"\n\t\t<subtitle><![CDATA["+"]]></subtitle>"+"\n</channel>\n</items>";
$("#modalBodyDiv").html(resultHtml) +
$("#myModal").modal("show");
},
error: function (xhr, status, error) {
$("#message").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
}
});
});
$(document).ajaxStart(function () {
$(".imageDiv img").show();
});
$(document).ajaxStop(function () {
$(".imageDiv img").hide();
});
function CallAPI(page) {
$.ajax({
url: "https://api.themoviedb.org/3/search/movie?language=pt-BR&query=" + $("#searchInput").val() + "&page=" + page + "&include_adult=false",
data: { "api_key": "" },
dataType: "json",
success: function (result, status, xhr) {
var resultHtml = $("<div class=\"resultDiv\"><p>Names</p>");
for (i = 0; i < result["results"].length; i++) {
var image = result["results"][i]["poster_path"] == null ? "Image/no-image.png" : "https://image.tmdb.org/t/p/w500/" + result["results"][i]["poster_path"];
resultHtml.append("<div class=\"result\" resourceId=\"" + result["results"][i]["id"] + "\">" + "<img src=\"" + image + "\" />" + "<p><a>" + result["results"][i]["title"] + " (" + result["results"][i]["release_date"] + ")" +"</a></p></div>");
var xmlString = "<channel>" + " <title><![CDATA[" + title + "|"+ to + "(" + ano + ")]]></title>" + " <logo_30x30><![CDATA[" + image + "]]></logo_30x30>" + " <description><![CDATA[<center><img height=\"254\" width=\"200\" src=\"" + poster + "\"</img><br>"+ sinopse +"</center>]]></description>" + " <stream_url><![CDATA[" + escape(country) + "]]></stream_url>" + " <subtitle><![CDATA[" + escape(zip) + "]]></subtitle>" + "</channel>";
$("textarea#ctext").val(xmlString);
}
resultHtml.append("</div>");
$("#message").html(resultHtml);
Paging(result["total_pages"]);
},
error: function (xhr, status, error) {
$("#message").html("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
}
});
}
function Validate() {
var errorMessage = "";
if ($("#searchInput").val() == "") {
errorMessage += "► Enter Search Text";
}
return errorMessage;
}
function Paging(totalPage) {
var obj = $("#pagination").twbsPagination({
totalPages: totalPage,
visiblePages: 5,
onPageClick: function (event, page) {
CallAPI(page);
}
});
}
function clipboard() {
var x = document.getElementById("ctext").value;
document.getElementById("modal-footer").innerHTML = x;
}
});
</script>
What mistake it makes?
– Sam
Nothing happens. I tried using
$("#ctext").val(xmlString);
– Antonio Oliveira
Leo Caracciolo. That didn’t work for me.
– Antonio Oliveira
With text sends without problem (had already tested). Does not work with variable
– Antonio Oliveira
$("textarea#ctext"). val(xmlString);
– user60252
I tried them all. I tested them one by one. The curious thing is that there is already another variable that sends to the modal in html and works perfectly. The problem is to send text to the textarea. Do you need to convert the variables?
– Antonio Oliveira
Put the variables in the question
– user60252
$("#ctext"). val(xmlString); here with me it doesn’t work. q works is
$("textarea#ctext").val(xmlString);
– user60252
I put a function in the reply to exchange accented words for not accented to avoid error
Result: error Internal Server Error 500 Internal Server Error
– user60252