Receive data from Ajax to PHP

Hi everyone,

I will try to explain my problem as simple as I can. I am creating a forum, and I want each post to have the button to like the post. I am using a little form with a label and an input (which is not displayed), there is not submit button. What I want is that if somebody clicks on the label, send information to PHP using AJAX and with that information update the database, just to know the people which have liked the post.

I need to receive some data, and only one is send through the form. To resume, I want to update the information of the page (and the database) withouth refreshing the page. If I am not wrong using PHP the page refreshes, but not with AJAX.

HTML Code:

echo "<div class='busqueda_hilo'>";
   echo "<input type='text' name='search_hilo' placeholder='Buscar en este foro...'>"; 
   echo "<label class='fa fa-search' for='search_hilo'></label>";
echo "</div>";

JS:

<script>
        function myFunction(respuesta_id) {
          let heart;
          var id_respuesta = respuesta_id;

          if( $('#corazon').prop('checked') ) {
            console.log('Seleccionado');
            heart = 1;
          } else {
            console.log('No seleccionado');
            heart = 0;
          }
          console.log(id_respuesta);
          console.log(heart);

          $.ajax ({
            type: 'POST',
            url: 'proces_like.php',
            data: { "corazon": heart, "id_respuesta":id_respuesta },
            success:function(datos){
              $("#resultado").html(datos);
            }
          });
        };
</script>

In process.php file where the data is processed:

<?php

session_start();

require 'admin/config.php';

try {
    $conexion = new PDO($bd_config['dbname'], $bd_config['usuario'], $bd_config['password'] );
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}


echo $hola = isset($_POST['corazon'])? $_POST['corazon'] : 0;
echo $respuesta_id;

?>

In this case, the only variable I am receiving from AJAX is heart, but not the second one. I don’t know how to resolve this problem…in the console of the browser I can see that both variables are OK, but one is not sent to PHP.

I don’t see anywhere in your PHP where you extract the value of $repuesta_id from the $_POST array, in the way that you do for corazon. You could var_dump($_POST) before your echo to make sure it’s being received, but I suspect it’s just that you need to do the same on your second echo that you do on the first one.

1 Like

A note on your DB code: Never ever send database errors back to your users! you log them for yourself (since the message may contain confidential data such as your connection password). otherwise attackers get a free hack at your DB.

Second, if the connection fails, do not proceed with the script, as if nothing had happened (which is what happens after a catch() statement). best is to exit with a HTTP 500, so your JavaScript code can react appropriately.

I have fixed this problem removing one extra code line I had in process.php.

Ops, my fault, I didn’t know that it was also sending through $_POST as I was just passing the variable manually, not through a submit button. The problem I am facing now is that when I click the like button, the following happens. Before clicking on the like button the page is like this:

https://s12.postimg.org/k2vl8884d/Captura_de_pantalla_2017-03-27_a_las_13.45.14.png

And after I click on the like button:

https://s21.postimg.org/43ynxq4tj/Captura_de_pantalla_2017-03-27_a_las_13.48.07.png

In the browser console I can see this error each time I press the ā€œlikeā€ button, but the problem is not expanding, at least visually.

TagError: adsbygoogle.push() error: Only one ā€˜enable_page_level_ads’ allowed per page.

Thanks for your response, you mean this line?

echo "Error: " . $e->getMessage();

This and all the other code following it.

The following it’s just to know what I am receiving.

It makes no sense to do anything else on a database failure. even for testing.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.