0

I'm very new for web developing. May be this is a silly problem. But the thing is I need a answer for this. I have experience in adding div tags inside the php tags. But my problem is can we add onclick function or any other function to this div tags.

I have a code like that

     <?php 
 // Connects to your Database 
 mysql_connect("localhost", "root", "") or die(mysql_error()) ; 
 mysql_select_db("selfie") or die(mysql_error()) ; 

 //Retrieves data from MySQL 
 $data1 = mysql_query("SELECT * FROM image_upload INNER JOIN user_table
ON image_upload.user_id=user_table.user_id  ORDER BY timestamp DESC; ") or     die(mysql_error());


//Puts it into an array

 while($info = mysql_fetch_array($data1)){
 //Outputs the image and other data 
 Echo


'<div class="test">' .
'<div class="username">'.$info['user_name'].'</div>'.
  '<div class="imagedisplay">' .'<img src="uploads/'.$info['image'].'" width="230px"     height=auto border="1px solid #"
 >'. '</div>'.
  '<div class="horizontal">'. '</div>'.
   '<div class="desc">'.$info['description'].'</div>'.
  '<div class="horizontal">'. '</div>'.
   '<a class="like" id="press_me">'."Press Me".'</a>'.
   '</div>'.
   '</div>';

   }
 ?>  

Using this code I can display the things i want. In above code i have a tag with id="press_me"

I write javascript to increase value of like_count column of my database when someone click on that field. But it's noy work. I can't understand the error.. can anyone help me.

This is my script

    <script>
    $(function (){
            $('#press_me').click(function(){
            var request = $.ajax({
                                    type: "POST",
                                    url:     "increment.php"                               
                                  });
                                  request.done(function() {

                                        alert('Success');
                                        return;

                                   });
                                  request.fail(function(jqXHR, textStatus) {
                                        alert( "Request failed: " + textStatus );
                                    });
            });
    });
     </script>

This is my increment.php

    <?php
 $host = "localhost:3306"; // Host name
$username = "root"; // Mysql username
$password = ""; // Mysql password
   $db_name = "selfie"; // Database name

// Connect to server and select databse.
mysql_connect($host, $username, $password) or die("cannot connect");
mysql_select_db($db_name) or die("cannot select DB"); 


// Increasing the current value with 1
  mysql_query("UPDATE image_upload SET like_count = (like_count + 1)");
 ?>
Lanka
  • 29
  • 2
  • 11
  • 2
    You [shouldn't use mysql_* functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use PDO or MySQLi. – Daniel Gelling Oct 30 '14 at 11:19
  • 1
    You need to give `unique` ids for the each button.Now it is producing multiple since it is in loop – GautamD31 Oct 30 '14 at 11:21
  • It is bad practice to "echo" html code in PHP context. It is more easy, more readable and from IDE's better supported to always close PHP context, write plain HTML code and open PHP context again. And ofcourse you dont have to mess with all the ' and " all the time. So instead of "; you should rather go like that ?>
    php
    – Steini Oct 30 '14 at 11:29

3 Answers3

0

The <?php and the <div> tags are separate concerns. The processor for the php tag (usally) runs on the server and doesn't care about the html div tag. The div tag is (usually) interpreter by the client-side html processor (within your browser) and (again usually) doesn't even get the <?php tag and its related stuff - because it has already been processed on the server and only the output of that script is sent to the client. That is also true for the stuff outside of <?php ...?> tags because the php processor treats anything outside those tags as "as-is" output of the script.

Therefore the answer to your question "can we add onclick function or any other function to this div tags[?]" is: yes according to the html/dom specification regardless of whether on the server php has been used or not. Just use the "show source code" button in your browser and take a look at this code. That is what your browser, the piece of code that handles the html, javascript, dom, ... stuff, interprets.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
0

If you just want to increment one counter you can remove your id="press-me" and change your JS to

$(function (){
  $('.like').click(function(){
    var request = $.ajax({
      type: "POST",
      url: "increment.php"                               
    });
 ...                     
});

The click event will be detected for all the div using the like class I f you want to increment a different counter for each image, you can also use the class for the event and add a parameter in your request (var id = $(this).attr("idImg"); for example).

Marc Delerue
  • 65
  • 10
0

This is how I often use function calls on success or error in JQuery.

$(document).ready(function(){


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

                          $.ajax({
                                type:"POST",
                                url:"increment.php",                               
                                success:function(textStatus) {

                                    alert('Success');


                               },
                               error:function(jqXHR,textStatus){
                                    alert("Request failed: " + textStatus);
                               }
        });
        });
});
Larry Lane
  • 2,141
  • 1
  • 12
  • 18