0
I am creating a code, where I send a form with ajax and return the result obtained in a PHP(comment.php) file, but this file ignores the linked scripts on the page where the form is contained (question.php):
Ajax code (question.php):
jQuery(document).ready(function(){
jQuery('.formcomment').submit(function(){
var dados = jQuery( this ).serialize();
jQuery.ajax({
type: "POST",
url: "comment.php",
data: dados,
success: function(response){
var editorContent = tinyMCE.get('camporesposta').getContent();
if (editorContent == "" || editorContent == null){
$("#aviso").html("<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span></button><strong><i class='fa fa-meh-o'></i> Preencha o campo de resposta!</strong></div> ");
} else{
$("#aviso").html("<div class='alert alert-warning alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-label='Close'><span aria-hidden='true'>×</span></button><strong><i class='fa fa-meh-o'></i> Resposta inserida com sucesso!</strong></div> ");
document.getElementById("ajaxrespostas").innerHTML=response;
}
}
});
return false;
});
});
In this file Linkei the scripts below:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="node_modules/bootstrap/js/tinymce/tinymce.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML' async></script>
<script type="text/javascript" src="node_modules/bootstrap/js/tinymce/plugins/tiny_mce_wiris/tests/js/google_analytics.js"></script>
However, when returning the page (comment.php) in the "Answer" variable defined in ajax code, it simply fails to recognize the above scripts:
comment.php:
<?php
require_once 'db.php';
include 'functions.php';
?>
<?php
// Variável enviada do formulário
$resposta= $_POST['resposta'];
$idpergunta= $_POST['idPergunta'];
if ($resposta == null){
echo "falha";
die();
} else{
$sql = "INSERT INTO respostas (texto, idUsuario, datapostagem, idPergunta) VALUES ('$resposta', '$iduser', current_date, '$idpergunta')";
$insert = $mysqli->query($sql);
include("comentarios.php");
}
result (question.php page, initially with scripts being recognized):
result after the ajax request is made (question.php page with the comment.php page returned in the Response variable, unrecognized scripts)
as you can see, the scripts responsible for the text editor and mathematical formulas are not loaded.
I’m not sure I understand, but aren’t the Avascripts loaded? If so, try adding the property
async
to all tags<script>
– Costamilam
Yes, scripts are not recognized. I tried but failed :(
– Gabriel Oliveira
So, simulating this code here is difficult, but have you ever tried to remove the callback from the Success to check if it is AJAX itself, or its function that is breaking the page? One thing I’m missing is Ubmit’s preventDefault. You shouldn’t receive an event as a parameter in ('.formcomment'). Submit, and then call the preventDefault method to prevent your page from reloading when making a Submit?
– user129564
can you tell me how this would look? I’m a beginner in the subject.
– Gabriel Oliveira