skip to Main Content

i am trying to create a favorite button, that when clicked will favorite the message without reload. everything is coded correctly, except i am having trouble figuring out how i will send each message’s individual ID to the ajax response.

my ajax:

   $(document).on('submit', '.favourite-form', function(e) { 
   e.preventDefault();
   var data = $(this).serialize();  
   $.ajax({ 
  data: data, 
  type: "post", 
   url: "favorite.php?message=529", // here i put 529 as an example, 
  i need it to be a variable that changes based on which message has been clicked.
  success: function(data) { 
  alert("Data Save: " + data); 
    },
    error: function(jqXHR, textStatus, errorThrown) //gracefully handle any errors in the UI
    {
   alert("An ajax error occurred: " + textStatus + " : " + errorThrown);
  }
}); 
 }); 

my HTML.

   <form class="favourite-form" method="post">
        
        <a class="msg-icon " href="<?php echo "reply?message=" . $row['msgid'] . ""; ?>"></a>
       <button type="submit" name="fav" value="<?php echo $row['msgid'] ?>" ></button>
        </form>

My php relies on the id of messages to be sent via $_GET METHOD.

my php :

        $user_id = $_SESSION['active_user_id'];
        
    
            extract($_GET);
            $id=$_GET['message'];
            
            $q=$db->prepare("SELECT msgid,date,text
            
            FROM messages 
            WHERE to_id=? and msgid=?");
            $q->bindValue(1,$user_id);
            $q->bindValue(2,$id);
            $q->execute();
            $row2=$q->fetch();
            $d=$row2['date'];
            
            
            $fav_questionq=$db->prepare("SELECT *
            FROM messages
            LEFT JOIN users
            ON messages.to_id=users.id
            WHERE users.id=? AND messages.msgid=?
            
            ");
            $fav_questionq->bindValue(1,$user_id);
            $fav_questionq->bindValue(2,$id);
            $fav_questionq->execute();
            $frow=$fav_questionq->fetch();
            
            $fquestion= $frow['text'];
        
            
            $result = $db->prepare("SELECT * FROM fav_messages
                                WHERE username=? AND message=?");
            $result->bindValue(1,$user_id); 
            $result->bindValue(2,$id);              
            $result->execute();
                                
                        
        if($result->rowCount()== 1 )
        {
            $deletequery=$db->prepare("DELETE FROM fav_messages WHERE message=?");
            $deletequery->bindValue(1,$id);
            $deletequery->execute();
        
        }
        else
        {
        $insertquery = $db->prepare("INSERT INTO fav_messages (username,message,fav_question,fav_date) values(?,?,?,?)");
        $insertquery->bindValue(1,$user_id);
        $insertquery->bindValue(2,$id);
        $insertquery->bindValue(3,$fquestion);
        $insertquery->bindValue(4,$d);
        $insertquery->execute();
        }
        
        
        
        ?>

how can i send each message id via ajax this way.

2

Answers


  1. You can do:

    $(document).on('submit', '.favourite-form', function(e) {
        e.preventDefault();
        var data = $(this).serialize();
        // Here, you will get the individual id before submiting the form
        var mssg_id = $(this).find('button[name="fav"]').val();
        $.ajax({
            data: data,
            type: "post",
            url: `favorite.php?message=${mssg_id}`, // It will be added to the url ES6 method
            success: function(data) {
                alert("Data Save: " + data);
            },
            error: function(jqXHR, textStatus, errorThrown) 
            {
                alert("An ajax error occurred: " + textStatus + " : " + errorThrown);
            }
        });
    });
    

    Edit

    url: "favourite.php?message="+mssg_id,
    
    Login or Signup to reply.
  2. What i would do is just to add a hidden input in the form of the view, try this:

    <input type="hidden" name="msgid" value="<?php echo $row['msgid' ?>" />
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search