I am trying to send a form with a textarea by ajax and then receive the data with PHP to be able to register it in the database, but the problem is that I do not receive anything publication.php I have placed a var_dump() to see if I something is coming through POST[] but nothing is coming to me, however if it is coming to publication.js where is the ajax to send the data that php will receive by POST
Form:
<form method="POST" class="col s12" enctype="multipart/form-data">
<div class="row">
<div class="input-field col s12">
<!-- <i class="material-icons prefix">photo_camera</i> -->
<textarea id="post" class="materialize-textarea" required></textarea>
<label for="post">Dile algo al mundo...</label>
<span class="helper-text" data-error="wrong" data-success="right">
<div class="file-field input-field">
<span class="file-path-wrapper">
<i class="material-icons prefix">photo_camera</i>
<input id="img" type="file">
</span>
</div>
</span>
</div>
</div>
<!-- Boton de publicar -->
<button class="btn waves-effect btn-color right" type="button" id="public">Publicar
<i class="material-icons left">send</i>
</button>
<p id="respuesta" class="red-text"></p>
</form>
publication.js file where the ajax is:
$('#public').click(function(){
var post = document.getElementById('post').value;
// Enviar las variables a la ruta
var ruta = "post="+post;
$.ajax({
url: './backend/publication.php',
type: 'POST',
data: ruta,
})
.done(function(res){
$('#repuesta').html(res)
})
.fail(function(){
console.log("error");
})
.always(function(){
console.log(post);
});
});
publication.php file where the PHP logic is:
<?php
// Obtener direccion ip del cliente
$ip = $_SERVER['REMOTE_ADDR'];
// Obtener el navegador del visitante
$browser = $_SERVER['HTTP_USER_AGENT'];
$errors = '';
if(isset($_POST['post'])){
$post = $_POST['post'];
var_dump($post);
// Limpiar
$post = htmlspecialchars($post);
$post = trim($post);
$post = filter_var($post, FILTER_SANITIZE_STRING);
// Si no hay errores, guardamos la pub en la BD
if($errors == ''){
$statement = $conexion->prepare('INSERT INTO publication (id_pub, id_user_pub, messenger_pub, photo_pub, ip_pub, browser_pub, create_at_pub) VALUES(
null, :id_user, :post, :photo, :ip, :browser, NOW())'
);
$statement->execute(array(
':id_user' => $id_user,
':post' => $post,
':photo' => $photo,
':ip' => $ip,
':browser' => $browser
));
}
}
If you notice, the information is collected correctly, but I don't know why it is not sending it to the url, because I am not getting anything to the publication.php page where the PHP logic is to save the information based on data.
Apparently there are some syntax errors here the correct one:
The last comma after the variable was removed
ruta
, and you seem to be missing a period here../backend/publication.php
.Test it.
Error was because it was not calling
You have to create a within the form to be able to send the errors there, also if you don't place it like that it won't work.
This question has already been solved.
If you are using jquery you could try changing this:
For this:
look at the console and comment the error that appears, greetings!