Username check from mysql Database during send a message using ajax

diabolic

New Member
I have a simple php code to send mail to registered members which are in mysql database. Before the action form is processed i need to check if the receiver field contains a valid username from the user table or not. If not then it will show a ajax alert on the page that 'not a valid user' the compose message form is - \[code\]<?phpsession_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="http://stackoverflow.com/questions/15864033/js/jquery.js"></script> <script type="text/javascript" src="http://stackoverflow.com/questions/15864033/js/check_user.js"></script> <script type="text/javascript" src="http://stackoverflow.com/questions/15864033/js/jquery.autocomplete.js"></script> <link rel="stylesheet" type="text/css" href="http://stackoverflow.com/questions/15864033/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='http://stackoverflow.com/questions/15864033/compose.php'><span>Compose</span></a></li> <li><a href='http://stackoverflow.com/questions/15864033/mailbox.php'><span>Inbox</span></a></li> <li><a href='http://stackoverflow.com/questions/15864033/unread.php'><span>Unread <strong>(<?php echo $msgcount; ?>)</strong></span></a></li> <li><a href='http://stackoverflow.com/questions/15864033/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");?>\[/code\]and action php file is -\[code\]<?phpsession_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") ?>\[/code\]What will be the ajax code to check it.
 
Back
Top