How to send data in a string using ajax?

0

I need your help, gentlemen. You see I want to send the value of 3 variables which come from several fields of a form, except one which I pass to javascript from PHP. If you are wondering if those variables load the values, I say yes. I already tried that, using "alert ();"

The problem is that only the first of them registers in the BD, the others do not do anything, they do not appear.

JQUERY

 <script type="text/javascript">
  $(document).ready(function(){

  $('#btnGL').click(function(){

  var id_post = $('#bookId').val();//Obtener valor del campo con el ID del post
  var id_usuario = <?php echo $id_user ?>;//Obtener ID del usuario que hizo el post
  var obtenerBallon = $('#ageOutputId').val();//Obtener valor del campo cantidad de ballons

 alert(id_usuario);
 alert(id_post);

 var datos = 'idPost='+ id_post + 'idUsuario='+ id_usuario + 'puntos='+ 
 obtenerBallon;

 $.ajax({

 type:'POST',
 url:'VotosGL.php',
 data:datos,
 success:function(result){
 alert(result);
 }

});

});

});

</script>

PHP VotosGL.php

  <?php 

include('conexion.php');

$id_post = $_POST['idPost'];
$id_usuario = $_POST['idUsuario'];
$puntos = $_POST['puntos'];

$insert =  mysqli_query($conn, "INSERT INTO posts_votos   
(id_post,id_usuario,puntos) VALUES('$id_post','$id_usuario','$puntos')");

 ?>
    
asked by luis 30.10.2016 в 22:18
source

2 answers

2

Try to send the data like this, it may be because they lack the &

var datos = 'idPost='+ id_post + '&idUsuario='+ id_usuario + '&puntos='+ 
 obtenerBallon;

If it does not work either send it as an object

var datos = {
  idPost: id_post,
  idUsuario: id_usuario,
  puntos: obtenerBallon
}

$.ajax({

  type:'POST',
  url:'VotosGL.php',
  data:datos,
  success:function(result){
  alert(result);
 }
});
    
answered by 30.10.2016 / 22:40
source
1

You can also use it like this, directly without putting together an arrangement

var id_post = $('#bookId').val();
var id_usuario = <?php echo $id_user ?>;
var obtenerBallon = $('#ageOutputId').val();

$.ajax({
  type:'POST',
  url:'VotosGL.php',
  data:{id_post:id_post,id_usuario:id_usuario,obtenerBallon:obtenerBallon},
  success:function(result)
  {
    alert(result);
  }
});

And in PHP you would recover the data in the following way:

if(isset($_REQUEST["id_post"])){$id_post = $_REQUEST["id_post"];}else{$id_post = "";}
if(isset($_REQUEST["id_usuario"])){$id_usuario = $_REQUEST["id_usuario"];}else{$id_usuario= "";}
if(isset($_REQUEST["obtenerBallon"])){$obtenerBallon= $_REQUEST["obtenerBallon"];}else{$obtenerBallon= "";}
    
answered by 02.11.2016 в 01:30