我有一个简单的 php 代码来向 mysql 数据库中的注册成员发送邮件。在处理操作表单之前,我需要检查接收者字段是否包含来自用户表的有效用户名。如果不是,那么它将在页面上显示“不是有效用户”的 ajax 警报,撰写消息表单是 -
<?php
session_start();
include("header.php");
include("includes/usermenu.php");
include("includes/connection.php");
if(isset($_SESSION['id']))
{
$username=$_SESSION['username'];
$ret_user = mysql_query("SELECT * FROM users WHERE id = '$id'") or die(mysql_error());
while($fetch_user = mysql_fetch_array($ret_user)){
$username = $fetch_user['username'];
}
$ret_count = mysql_query ("SELECT * FROM inbox WHERE receiver = '$username' and readstatus = '0' ORDER BY timesend") or die(mysql_error());
$msgcount = 0;
while($countmsg = mysql_fetch_array($ret_count))
{ $msgcount++;
}
?>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/check_user.js"></script>
<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="styles/jquery.autocomplete.css" />
<script type="text/javascript">
$().ready(function() {
$("#username").autocomplete("includes/source.php", {
width: 260,
matchContains: true,
selectFirst: false
});
});
</script>
<div id='slidemenu'>
<ul>
<li><a href='compose.php'><span>Compose</span></a></li>
<li><a href='mailbox.php'><span>Inbox</span></a></li>
<li><a href='unread.php'><span>Unread <strong>(<?php echo $msgcount; ?>)</strong></span></a></li>
<li><a href='outbox.php'><span>Outbox</span></a></li>
</ul>
</div>
<div id="msgbox" align="right">
<div style="height:25px; text-align:center; margin-top: 20px;" > <font style="color:yellow;font-size:18px;font-family:'Orienta', sans-serif; font-size: 12px;" id="W"></font>
<div id="HIDDEN" style="display: none;"> </div>
</div>
<table>
<form action="includes/savemsg.php" method="post">
<tr><td><label for="rec">Recievers ID</label></td>
<td><input type="text" name="username" id="username" tabindex="1"/><br /><br /></td></tr>
<tr><td><label for="subject">Subject</label></td>
<td><input type="text" name="subject" tabindex="2"/><br /><br /></td></tr>
<tr><td><label for="msg">Message</label></td>
<td><textarea name="message" cols="50" rows="3" tabindex="3"></textarea></td></tr>
<tr><td></td><td><input type="submit" onClick="check_user();"/></td></tr>
</form>
</table>
</div>
<?php
}
else header("location: login.php");
include("includes/footer.php");
?>
和行动 php 文件是 -
<?php
session_start();
include("connection.php");
$id = $_SESSION['id'];
$rec_id = $_POST['username'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$ret_user = mysql_query("SELECT * FROM users WHERE id = '$id'") or die(mysql_error());
while($fetch_user = mysql_fetch_array($ret_user)){
$username = $fetch_user['username'];
}
$send_msg = mysql_query ("INSERT INTO inbox VALUES(DEFAULT,'$username','$rec_id','$subject','$message',now(),now(),'0')") or die(mysql_error());
?>
<script>
alert("Message send");
</script>
<?php
header ("location: ../mailbox.php")
?>
检查它的ajax代码将是什么。