Jump to content

execute sql query by clicking link/button


Ortix

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.