I have a jquery code to reload a session of my website in real time every 1 second to fetch user posts in real time from the database.
But it turns out that every time I refresh the page, the user's publication is showing me everything fine, but since the second that refreshes the session passes, it is throwing me an error with the address of the connection folder . The address of the folder is well written because it works great before I try to refresh the page in real time and the address to the connection worked great. Be careful, the address of the connection is well written.
Code to reload the page in real time:
function tiempoReal()
{
var tabla = $.ajax({
url:'./publication/publication.php',
dataType:'text',
async:false
}).responseText;
document.getElementById("publication").innerHTML = tabla;
}
setInterval(tiempoReal, 1000);
I'll explain a little what this code does. What I did is that I created a function to reload the file in real time where I have the PHP logic to extract the users' publications, which is: url:'./publication/publication.php',
EYE: the address is written correctly even though it only has a dot and a backslash to indicate that it should leave the current folder and try to place the colon and there it does not work at all.
This select-publication.js file , which is where the code is that I just placed, which is to refresh the page in real time, I am calling it on the page './publication/publication.php'
, which is where the HTML is, which in turn is required by it home.php file which is where all the posts are required.
publication/publication.php file: which is the file where all the html of the publications is
<?php require_once('./backend/select-publication.php') ?>
<?php foreach($result as $post) : ?>
<!-- Card de publicacion -->
<div id="publication" class="row">
<div class="col s12 m7 xl6">
<div class="card">
<div>
<br>
<img src="images/yo.jpg" alt="carla" class="col s2 m2 xl2 img-adaptable circle">
<span class="flow-text pink-text"><?php echo $post['name'].' '.$post['surname'] ?></span>
<span class="right">
<i class="material-icons left">more_vert</i>
</span><br>
<small class="grey-text">
Publicado el <?=form_fecha($post['create_at_pub'])?>
</small><br><br>
</div>
<div class="card-image">
<img src="./photo/<?php echo $post['photo_pub'] ?>" alt="Publicación de <?php echo $post['name'].' '.$post['surname'] ?>" class="materialboxed">
<span class="card-title"><?php echo $post['name'].' '.$post['surname'] ?></span>
<!-- <a class="btn-floating halfway-fab waves-effect waves-light white">
Imagen del usuario que publico
<img src="images/yo.jpg" alt="carla" height="60" class="img-adaptable">
</a> -->
</div>
<div class="card-content">
<!-- <p class="grey-text">
<i class="material-icons left">more_vert</i>
Publicado el 07 de Febrero de 2021
</p><br> -->
<p><?php echo $post['messeger_pub'] ?></p><br>
<div class="divider"></div><br>
<!-- Reacciones -->
<div class="row">
<!-- Like -->
<div class="col s6">
<a href="" class="black-text">
<i class="material-icons left small">favorite_border</i>
<span class="flow-text black-text">10</span>
</a>
</div>
<!-- Comments -->
<div class="col s6">
<span id="click-comment" class="black-text right">
<i class="material-icons left small">comment</i>
<span class="flow-text black-text">2</span>
</span>
</div>
</div>
<!-- Area de texto para escribir un comentario -->
<div id="view-comment" class="hide row">
<div class="divider"></div>
<form class="col s12">
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">comment</i>
<textarea id="comment" class="materialize-textarea"></textarea>
<label for="comment">Escribe un comentario...</label>
</div>
<button class="btn waves-effect btn-color right" type="submit" name="action">Comentar
<i class="material-icons left">send</i>
</button>
</div>
</form>
</div>
</div>
</div>
</div>
<?php endforeach ?>
As you can see I am doing a foreach to go through all the posts. At the top is the require where the PHP logic to pull all the posts is found. EYE, all the code worked perfectly before I included the Jquery code to refresh the page in real time. For that reason I think that the problem is in the code above where the jquery is.
If you need more information just let me know.
I managed to solve the problem after studying it for a long time. It turns out that the file that is in the "publication/publication.php" folder, when doing the refresh, did not enter the includes to read the other files that were of the utmost importance and for this reason it threw me those redirection errors. What I did to solve the problem was to place the connection and the necessary functions inside that file so that when the refresh occurs, it reads everything without having to throw the errors for not being able to read the other files necessary to work.
publication/publication.php: