Updating Database from Javascript by calling PHP script
html, javascript, mysql, php
Solution
try this
<script type="text/javascript">
function process(LikeId) {
//your validation code
$.ajax( {
type: 'POST',
url: LikeUnlike.php, //file where like unlike status change in database
data:{like:LikeId},
success: function(data) {
//code you want to do after successfull process
}
} );
}
</script>
make changes in html
<a href='Javascript:void(0)' onclick='process(1)'>Like</a>
<a href='Javascript:void(0)' onclick='process(0)'>Unlike</a>
Problem
Ive been trying to get this for age I am building a website which has an activity wall. I have the whole thing working except the like and unlike buttons. I have them currently just showing a text box I like or I don't like ``` <a href='#' onclick='like()'>Like</a> ``` or ``` <a href='#' onclick='unlike()'>Unlike</a> ``` Now these call these scripts ``` <script> function like() { alert("I like"); document.getElementById("p1").innerHTML="<a href='#' onclick='unlike();'>Unlike</a> | 1 Life<hr/>"; } function unlike() { alert("Dont like anymore"); document.getElementById("p1").innerHTML="<a href='#' onclick='like();'>Like</a> | 0 Lifes<hr/>"; } </script> ``` I have a php script that connects to the database and adds or removes the like which I know works. I don't need to return anything to the javascript but how do I call that php script giving the postID and username?? Ok so using this I have modified it a little. But it still doesnt work :S ==LikeUnlink.php== ``` <?php include 'phpScripts/OpenConnection.php'; $mode = $_GET['mode']; $username = $_GET['username']; $postID = $_GET['LikeID']; echo $username."<br/>"; echo $mode."<br/>"; echo $postID."<br/>"; if($mode == 0) { $query = "INSERT INTO tbl1Ups (Username, ActivityID) VALUES ('$username', $postID)"; } else { $query = "DELETE FROM `tbl1Ups` WHERE Username='$username' AND ActivityID=$postID"; } $result = mysql_query($query) or die(mysql_error()); ``` ==MembersHome.php== ``` <script type="text/javascript"> function process(LikeId, Username, currentLikes, likeUnlike) { //your validation code $.ajax( { type: 'GET', url: LikeUnlike.php, //file where like unlike status change in database data:{like:LikeId, username:Username, mode:likeUnlike}, success: function(data) { alert('It Works'); } } ); } </script> ``` ==Like link== ``` <a href='Javascript:void(0)' onclick='process(".$row['ID'].", \"".$username."\", ".$likes.", 0);'>$linkText</a> ```