我正在使用PHP / jQuery和AJAX创建用于发送和接收消息的测试应用程序。当我注释掉这部分代码时,我的代码没有任何问题:
$("#sendUser").submit(function(){
return false;
});
如果我将此部分取消注释,则jQuery不会从<select>
标记中获得值。有人能告诉我如何解决这个问题。这是我的完整代码:
<form action="send_toUser.php" method="post" id="sendUser">
<label>Title: </label>
<input type="text" name="title" id="title"><br>
<label>To: </label>
<select name="user" id="user">
<?php $getData = $mysqli->query("select * from login");
while($row = $getData->fetch_assoc()):
?>
<option><?php echo $row['username']; ?></option>
<?php endwhile; ?>
</select><br>
<label>Message:</label><br>
<textarea name="content" id="content"></textarea><br>
<input type="submit" value="Send" id="sendToUserButton">
<label id="outputLabel" hidden></label>
</form>
send_toUser.php
<?php
session_start();
include 'functions.php';
protect();
$title = $_POST['title'];
$content = $_POST['content'];
$user = $_POST['user'];
$con = new mysqli("localhost", "root", "alen", "loginregister") or die($con->error);
$queryReceiver = $con->query("select * from login where username = '$user'");
$idReceiver = $queryReceiver->fetch_assoc();
$idReceiver = $idReceiver['user_id'];
$queryUser = $con->query("select * from admin where username='admin'");
$idUser = $queryUser->fetch_assoc();
$idUser = $idUser['id_admin'];
date_default_timezone_set('UTC');
$date = date('Y-m-d');
$con->query("insert into message values('', '$idReceiver', '$idUser', '$title', '$content','', '$date')") or die("Error while sending!");
echo "Message sent";
?>
的script.js
$("#sendToUserButton").click(function(){
$.post("send_toUser.php"),
{"title" : ("#title").val(), "user" : ("#user").val(), "content" : ("#content").val()},
function(data){
$("#outputLabel").html(data);
}
});
$("#sendUser").submit(function(){
return false;
});