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