Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8606719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:09:50+00:00 2026-06-12T03:09:50+00:00

hi have this code which works perfectly with just value: <script type=text/javascript> $(document).ready(function() {

  • 0

hi have this code which works perfectly with just value:

    <script type="text/javascript">
    $(document).ready(function() {
    $("input[type=button]").click(function () {
    var textarea_content = $('textarea#wall').val(); // get the content of what user typed    (in textarea) 
    if (textarea_content != '') { // if textarea is not empty 
    var sender =  "<?php echo $_SESSION['user_id'];?>";

     $.ajax({
            type: "POST",
            url: "send_message_function.php",
            data : "action=send&from_id="+sender+"&to_user="+$(this).siblings("input[type=text]").val()+"&message="+textarea_content,
            dataType: "json",
            success: function (data) {
               var  LastID = data["cid"];
               alert("toUser: " + $(this).siblings("input[type=text]").val()+"and text area " + textarea_content + " message id: " + LastID);
                                     }
        });//end success function

        //do something else

        } else {
        alert('Enter some text ! '); // just in case somebody click on share witout writing anything :)
    }
    });//end click function
    });//end ready function
    </script>

and send_message_function.php :

<?php
 require 'core/functions/init.php';

 $action  = $_POST['action'];
 if($action=="send"){
 $from_id = mysql_real_escape_string($_POST['from_id']);
 $to_id = mysql_real_escape_string($_POST['to_user']);
 $message = strip_tags($_POST['message']);

 $sql = mysql_query("INSERT INTO chat (from_id, to_id, message,   dt)VALUES('$from_id','$to_id','$message',NOW())") or die("0");

 echo json_encode(array("cid"=>(mysql_insert_id()),
                               "from_id"=>''.$message_id.''));
}
    ?>

the problem is when I try to send the message to multiple users. The sql query try to insert all the users IDs in the same row. I know I should do some loop but I can’t think how run the query for every users I want to send the message to.

$(this).siblings("input[type=text]").val()      

returns multiple users id like this: 113,143,234

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T03:09:51+00:00Added an answer on June 12, 2026 at 3:09 am

    you do not need a loop, you can do multiple inserts with just one query, like this:

     $to_id = explode(',', mysql_real_escape_string($_POST['to_user'])); //split the string to an array containing each id
    
     $sql = mysql_query("INSERT INTO chat (from_id, to_id, message, dt) VALUES('$from_id','$to_id[0]','$message',NOW()), ('$from_id','$to_id[1]','$message',NOW()), ('$from_id','$to_id[2]','$message',NOW()) ") or die("0");
    

    Note: $from_id shouldn’t be a primary key, else you would get a duplicate key error.

    Edit: If you do not know the no. of users,

       $to_id = explode(',', mysql_real_escape_string($_POST['to_user']));
    
       foreach ($to_id as $v)
         $sql = mysql_query("INSERT INTO chat (from_id, to_id, message, dt) VALUES('$from_id','$v','$message',NOW()) ") or die("0");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which compiles and works as expected: class Right {}; class
I have this code in my cfm, which works <cfif not StructIsEmpty(form)> <cfset larray
I have this code which is called at an onChange event of an function
I have the following code which works perfectly fine in my php file and
Here is my code, which works perfectly except for one case: when I have
I have this code which adds a x number of views to a Multiview
I have this code which does the trick: #include <stdio.h> int main() { int
I have this code which gets WP posts, but it gets all the posts
I have this code (which is way simplified from the real code): public interface
I have this code which will include template.php file from inside each of these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.