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)");
?>