I think this is simple question. I'm new to jQuery. I'm trying to make script, so that when you click on image, ajax will call php file which will update mySQL database.
My script:
<script>
function update(){
var request = $.ajax({
url: "insert.php",
type: "GET",
dataType: "html"
});
request.done(function(msg) {
$("div.recen").html(msg);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
};
</script>
HTML:
<div class="recen">
<img src="./smile.png" class="pic" onclik="update()" />
</div>
PHP file:
<?php
$var='123';
$url='localhost';
$user='root';
$password='';
$sql=mysql_connect($url,$user,$password);
mysql_select_db('database');
$query="UPDATE table SET var='".$var."';";
if(mysql_query($query)) echo '<b>Done.</b>';
else echo mysql_error();
?>
For some reason, there is absolutely no response. Database remains the same and html page doesn't change a bit. Is there something wrong with the code, or I just can't update database through ajax?
i think somthing wrong in your query
try this
$query="UPDATE table SET var='".$var."' ";
^^----remove this `;`
change this
if(mysql_query($query)) echo '<b>Done.</b>';
else echo mysql_error();
to
if(mysql_query($query)){ echo '<b>Done.</b>';}
else {echo mysql_error();}
there is no onclik
in javascript instead use onClick
You have a typo in your HTML code. Your attribute should be onclick
not onclik
.
<img src="./smile.png" class="pic" onclick="update()" />