0
I have a page with Formularies to answer questions and I am trying to use jquery in a separate file and is giving the following error: Uncaught Error: Syntax error, unrecognized Expression: #boot_
$("#botao_resp_<?=$row['id']?>").on('click', function (event) {
event.preventDefault();
alert('Você clicou no botão!');
$.ajax({
method: 'POST',
url : 'banco-responder.php',
data: {
id : $("#id-resposta-<?=$row['id']?>").val() ,
resposta : $("#resposta-pergunta-<?=$row['id']?>").val()
}
}).done(function(data){
console.log($("#card-<?=$row['id']?>").hide());
alert("Respondido com Sucesso!");
}).fail(function(){
alert('Deu pau');
});
return false;
});
In the form ID attribute is completed by a variable, which comes from iterating an array. When I put this code in the same form file, it works. But when I import it from another file it doesn’t work.
That code
JS
stays in a separate javascript file?– Valdeir Psr
Yes, this file is separate.
– milho
Well, it is possible, but it is not recommended. You can "report" to your server
Nginx/Apache
read the JS as if it were a PHP, but this creates serious security problems; you can create a codePHP
to behave like JS, simply add theheader => Content-Type: text/javascript
or you can create a global variable inform.html
, for example and use it in the file.js
.– Valdeir Psr
would have another solution other than this? the problem is in the php variable, right?
– milho
The problem is in using
php
in the archives.js
. Servers by default do not interpret codephp
in those files. If possible post part of yourhtml gerado
so I can post a more complete reply.– Valdeir Psr
<form id="form_ocul_<?= $Row['id']? >" style="display: inline-block;" action="" method="post"> <input type="Hidden" id="id-hide-<?= $Row['id']? >" name="id" value="<?= $Row['id'] ? >"> <button class="btn btn-Warning" type="Submit">HIDE</button> </form> This is one of the forms. The ajax was just below and put in another file.
– milho
I believe that the best solution for this is to join this js code to a file . php
– Julyano Felipe