0

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?

1
  • Just a note you can use $query = "UPDATE table SET var='$var' "; because you dont need to escape the quotes since " string means it can expect variables inside. A string started with ' does not look for variables. Commented Apr 20, 2013 at 19:39

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

You have a typo in your HTML code. Your attribute should be onclick not onclik.

<img src="./smile.png" class="pic" onclick="update()" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.