嗨,这个代码可以完美地使用价值:
<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>
和 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.''));
}
?>
问题是当我尝试将消息发送给多个用户时。sql 查询尝试在同一行中插入所有用户 ID。我知道我应该做一些循环,但我想不出如何为我想向其发送消息的每个用户运行查询。
$(this).siblings("input[type=text]").val()
像这样返回多个用户 ID:113,143,234