0

I have a page with the post and a "like" button, when I click the button "like" I would to change it in "dislike", my code in here:

    public function show_all(){

            $query = @mysql_query("SELECT * FROM post INNER JOIN users ON post.id_user = users.id_user ORDER BY post.id DESC") or die('Errore: ' . mysql_error());
                        while($risultato = mysql_fetch_array($query)) {
        echo '<div style=" border: 1px solid #ccc; box-shadow: 1px 1px 1px #ccc; margin-bottom: 20px; padding: 20px; word-wrap:break-word;">
            <ul class="navbar-header pull-right panel_toolbox">

  <li class="dropdown"><a href="#" class="dropdown-toggle"
                                            data-toggle="dropdown" role="button" aria-expanded="false"><i
                                                class="glyphicon glyphicon-chevron-down"></i></a>
                                            <ul class="dropdown-menu pull-right" role="menu">
                                                <li><a href="#">Elimina</a></li>
                                                <li><a href="#">Segnala</a></li>
                                            </ul></li>

                                    </ul>
                <h4>' . $risultato['username'] . '</h4>
                <a href="post.php?id='. $risultato['id'] .'" > ' . $risultato['post'] . '</a>


                <small>
                    <hr>
                    <input id="like" type="button" value="'.$like.'" onclick="like(' . $risultato["id"] . ' );" /> ' . $risultato['like1'] . '
                </small>
            </div> 
';

                        }
    }

And this is my main page:

<script>
    function pullPost() {
                $('#error-msg').html('loading....');
                $.ajax({
                    url: '../class/newsfeed.php',
                    method: "post",
                    success: function(data) {
                        setTimeout(function() {
                            $("#error-msg").fadeOut();
                        }, 3000);
                        $('.news-feed').html(data);
                    },
                    error: function() {
                        $("#error-msg").html('There is some error occured').fadeIn();
                        setTimeout(function() {
                            $("#error-msg").fadeOut();
                        }, 3000);
                    }
                });
            }
            window.onload = function() {
                pullPost();
            };
        </script>

<script>
    function like(id_post, id_user) {

      $.ajax({
           type: "POST",
           url: '../class/like.php',
           data:{id_post:id_post},
           complete: function() {
                            pullPost();
                        }

      });

 }
 </script>

How change "like" button in "dislike"?

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Gae
  • 71
  • 1
  • 7
  • you can have a look at [jQuery replace element with the provided new content](http://api.jquery.com/replacewith/) – OldPadawan Apr 13 '17 at 14:24
  • Thanks, but when I extract data from the database I will always "like" and not "dislike" – Gae Apr 13 '17 at 14:30
  • yes, and when people validate a like/dislike, you can change the button again – OldPadawan Apr 13 '17 at 14:31
  • 2
    I *dislike* `mysql_*` I also *dislike* `@` characters to suppress errors. I also dislike badly formatted code. – apokryfos Apr 13 '17 at 14:35
  • sorry, can you help me with a code example? I saw your link but do not understand much of javascript / jquery – Gae Apr 13 '17 at 14:39
  • ***Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).*** [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 13 '17 at 14:41
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 13 '17 at 14:42
  • Thanks for the advice, But back to my problem, how do I print a video like or dislike in the second database of the results? – Gae Apr 13 '17 at 14:51
  • Can you give me a simple example? – Gae Apr 13 '17 at 14:52
  • kind of `success: function() { $('#your_button').replaceWith(' – OldPadawan Apr 13 '17 at 14:54
  • But if I did Something Kind? `$like="Like"; if($risultato['like1']==1) { $like="Dislike"; } ` – Gae Apr 13 '17 at 15:14
  • I do a inner join three tables but does not work – Gae Apr 13 '17 at 15:17

0 Answers0