Ortix Posted January 27, 2011 Share Posted January 27, 2011 So I am trying to execute an sql query by clicking a button or a link. Ultimately I want users to be able to click the "Fav" button and it then fades into "Added to favorites!" (I am building a favorite system which first collects data on page load like userid and itemid and then it needs to send it to the DB after a button is clicked) The fade and stuff is not the most important part now (although a tip on doing that would be awesome) but the most important part is how to get this functional: PHP Code: <?php // Start Favorite System! $user =& JFactory::getUser(); $userid = $user->id; if ($userid == 62) { echo "userID: ".$userid."<p />"; if ($user->guest) {echo "You are a guest<p />";} $itemid = $this->item->id; echo "itemID: ".$itemid."<p />"; $catid= $this->item->category->id; echo "catID: ".$catid."<p />"; $query = "INSERT INTO jos_k2_fav_xref (userID, itemID, catID) VALUES ('$userid', '$itemid', '$catid')"; // Need a way to make this into a button $run = mysql_query($query) or die(mysql_error()); } else {} ?> Currently all the values get stored in the DB as the page is loaded. I however want to be able to click on something to execute that query how do i do that? I read a bunch of stuff on javascript and ajax but it got me nowhere... Help is greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/225898-execute-sql-query-by-clicking-linkbutton/ Share on other sites More sharing options...
rondog Posted January 27, 2011 Share Posted January 27, 2011 you have two options, when they click, it refreshes the page, thus sending the favorited item to the server or what sounds like will be your best bet is to use ajax. jQuery has a very easy to use ajax library. Link to comment https://forums.phpfreaks.com/topic/225898-execute-sql-query-by-clicking-linkbutton/#findComment-1166265 Share on other sites More sharing options...
Ortix Posted January 27, 2011 Author Share Posted January 27, 2011 i opt in for the latter. But like I said.. i read those things but I'm in the learning stages of PHP.. i haven't used __GET or __POST at all... code examples would really be helpful! Link to comment https://forums.phpfreaks.com/topic/225898-execute-sql-query-by-clicking-linkbutton/#findComment-1166267 Share on other sites More sharing options...
rondog Posted January 27, 2011 Share Posted January 27, 2011 this is from here: http://api.jquery.com/jQuery.ajax/ $.ajax({ type: "POST", url: "some.php", data: "name=John&location=Boston", success: function(msg){ alert( "Data Saved: " + msg ); } }); Link to comment https://forums.phpfreaks.com/topic/225898-execute-sql-query-by-clicking-linkbutton/#findComment-1166271 Share on other sites More sharing options...
Ortix Posted February 1, 2011 Author Share Posted February 1, 2011 ok i tried putting together a script with the ajax you told me and i read that page where you found that but it doesn't seem to work. I've got a couple of questions regarding this. As you can see I need to execute an sql query that uses variables. These variable are defined in the file which also has the link in it (so not in the php file that will be run with AJAX) Now my question is how that data (the variables) will be "transfered" to the query? Will these variable be loaded in memory and be available by the file executed by AJAX? either way.. it doesn't work and i can't seem to get it to work. This is what I have: <script type="text/javascript"> function addFavorite() { $.ajax({ type: "POST", url: "favorite.php", success: function(){ alert( "Data Saved:"); } }); return false; } </script> <a href="#" onclick="addFavorite();">Click Me!</a> the php code in the favorite.php file: <?php $query = "INSERT INTO jos_k2_fav_xref (userID, itemID, catID) VALUES ('$userid', '$itemid', '$catid')"; $run = mysql_query($query) or die(mysql_error()); ?> and the php that get's the data that needs to be inserted in the Database (this is inside the same file as the ajax): $user =& JFactory::getUser(); $userid = $user->id; if ($userid == 62) { echo "userID: ".$userid."<p />"; if ($user->guest) {echo "You are a guest<p />";} $itemid = $this->item->id; echo "itemID: ".$itemid."<p />"; $catid= $this->item->category->id; echo "catID: ".$catid."<p />"; BTW. I'm using Joomla! Link to comment https://forums.phpfreaks.com/topic/225898-execute-sql-query-by-clicking-linkbutton/#findComment-1168286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.