通过ajax调用php mySQL更新

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?

查看全部
dqhdz04240
dqhdz04240
2013/04/20 19:16
  • php
  • html
  • mysql
  • ajax
  • jquery
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复