searls03 Posted April 21, 2011 Share Posted April 21, 2011 Let me make this clear really quick, I am not talking about this forum. Ok, so how do I set up a way that I can make a simple messaging system on my website and make it so that I can have check boxes of who I can send a message to on my website. I have a recipient field and I need to know how I can make it so that only the specific members that are checked will get the message.........does this make sense? Quote Link to comment Share on other sites More sharing options...
drisate Posted April 21, 2011 Share Posted April 21, 2011 Umm this is kind of hard to explain ... but starting from scratch you need a member systeme id, username, password, email Once you got the users to register you create a form with a WYSIWYG for the message body. (Preferably on that works with BBCode) You can select the users by seperating the names by a coma. On the sending process you can explode them in order to add your message to the message table. The message table would look like this: id, from, to, subject, message, date, status The loop to send to each would look like this $members = explode(',', str_replace (' ', '', $_POST[to])); // Delete the white spaces and explode the , character foreach ($members as $member){ // [...] Code to insert the message in the message table } I don't think a check box is the best way of doing this but if thats really what you want then you would need to loop the members like this $select = mysql_query("SELECT * FROM member") or die(mysql_error()); while ($member = mysql_fetch_array($select)) { echo '<input type="checkbox" name="user[]" value="'.$member[username].'">'.$member[username].'<br>'; } Then once the data is submited you loop the POST array if ($_POST[user]){ foreach ($_POST[user] as $key => $value){ // [..] code that adds the message to the database } } Then you need a page that shows the messages you got $select = mysql_query("SELECT * FROM message WHERE user_id='$_SESSION[id]' order by id desc") or die(mysql_error()); while ($messages = mysql_fetch_array($select)) { // Print out the message subject } Once opened i would segest you to use the status field as an indicator if the message is new or not and if the user already replyed 0 is new 1 is viewed 2 is replyed Anyway, i hope thats gona help your in seting up your message systeme. It's really not as complicated as it seems. Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 21, 2011 Author Share Posted April 21, 2011 what is wrong with this, the recipients are not posting........: <?php $members = explode(',', str_replace (' ', '', $_POST[to])); foreach ($members as $member){ if ($_POST[user]){ foreach ($_POST[user] as $key => $value){ // [..] code that adds the message to the database $id = $_POST['id']; $content = $_POST['content']; $user = $_POST['recipient']; $from = $_POST['from1']; $subject = $_POST['title']; $query ="insert into messages(title, content, recipient, from1) VALUES ('$subject', '$content', '$user','$from')"; mysql_query($query) or die('Error, query failed'); } }} ?> Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 21, 2011 Author Share Posted April 21, 2011 actually nothing is posting. Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 21, 2011 Author Share Posted April 21, 2011 I don't believe I put things where they are supposed to be.......... Quote Link to comment Share on other sites More sharing options...
drisate Posted April 22, 2011 Share Posted April 22, 2011 I think you forgot to change the $_POST vars in order to get it to work i used $_POST[to] but i think your using $_POST['recipient'] and i am using $_POST[user] but i think your using $_POST['from1'] Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 I tried changing those and i don't believe it worked......let me go ahead and post all of it: <form id="form1" name="form1" method="post" action="newmess.php"> <p>Subject: <label for="subject"></label> <input type="text" name="subject" id="subject" /> </p> <p> <?php $select = mysql_query("SELECT * FROM members") or die(mysql_error()); while ($member = mysql_fetch_array($select)) { echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br>'; } ?> </p> <p> <label for="content">Content:</label> <textarea name="content" id="content" cols="45" rows="5"></textarea> <input type="hidden" name="from" id="from" value='<?php echo $name; ?>' /> </p> <p> <input type="submit" name="submit" id="submit" value="Post" /> </p> </form> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> and here is the post again: <?php $members = explode(',', str_replace (' ', '', $_POST[recipient])); foreach ($members as $member){ if ($_POST[recipient]){ foreach ($_POST[recipient] as $key => $value){ // [..] code that adds the message to the database $content = $_POST['content']; $user = $_POST['recipient']; $from = $_POST['from1']; $subject = $_POST['title']; $query ="insert into messages(title, content, recipient, from1) VALUES ('$subject', '$content', '$user','$from')"; mysql_query($query) or die('Error, query failed'); } } } ?> I may have changed some stuff in that..... Quote Link to comment Share on other sites More sharing options...
drisate Posted April 22, 2011 Share Posted April 22, 2011 Looks like you used both loop options in your code. If you using the checkbox systeme use only that one <?php if ($_POST[user]){ foreach ($_POST[user] as $key => $value){ // [..] code that adds the message to the database $content = $_POST['content']; $from = $_POST['from1']; $subject = $_POST['title']; $query ="INSERT INTO messages (title, content, recipient, from1) VALUES ('$subject', '$content', '$value','$from')"; mysql_query($query) or die('Error, query failed'); } } ?> Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 what is the code that adds to database? or is it there already? Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 so now tyhe content and recipient is posting...........not from1 or title? Quote Link to comment Share on other sites More sharing options...
drisate Posted April 22, 2011 Share Posted April 22, 2011 Your using subject as the name of your imput not title and from not from1 <?php if ($_POST[user]){ foreach ($_POST[user] as $key => $value){ // [..] code that adds the message to the database $content = $_POST['content']; $from = $_POST['from']; $subject = $_POST['subject']; $query ="INSERT INTO messages (title, content, recipient, from1) VALUES ('$subject', '$content', '$value','$from')"; mysql_query($query) or die('Error, query failed'); } } ?> Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 yep, I realized that!!!!!!!I got it working now!!!!!! :D :D :D :D :D :D Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 how can I make it so people can reply to the message............without a limit..........I can't use table associations in database........ Quote Link to comment Share on other sites More sharing options...
drisate Posted April 22, 2011 Share Posted April 22, 2011 You would do the form like this and use the same code as above to send your message <?php $message = mysql_fetch_array(mysql_query("SELECT * FROM messages WHERE id='$_GET[message_id]'")); ?> <form id="form1" name="form1" method="post" action="newmess.php"> <p>Subject: <label for="subject"></label> <input type="text" name="subject" id="subject" value="RE: <?=$message[subject]?>" /> </p> <p> <label for="content">Content:</label> <textarea name="content" id="content" cols="45" rows="5"></textarea> <input type="hidden" name="from" id="from" value='<?=$message[recipient]?>' /> <input type="hidden" name="user[]" id="from" value='<?=$message[from]?>' /> </p> <p> <input type="submit" name="submit" id="submit" value="Post" /> </p> </form> Just make sure you get the id of the message right Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 but I need the replies in a database...........................how do I do that? or is it I need like messageid userid and the comment and just link them together? Quote Link to comment Share on other sites More sharing options...
drisate Posted April 22, 2011 Share Posted April 22, 2011 The hole thing would look like this (I did not test the code) <?php session_start(); $DB_HOST = "localhost"; $DB_USERNAME = ""; $DB_PASSWORD = ""; $DB_NAME = ""; $DB = mysql_connect("$DB_HOST", "$DB_USERNAME", "$DB_PASSWORD") or die('DB Error: ' . mysql_error()); mysql_select_db($DB_NAME); if ($_SESSION[username]){ if ($_GET[message_id]){ $message = mysql_fetch_array(mysql_query("SELECT * FROM messages WHERE id='$_GET[message_id]' and recipient='$_SESSION[username]'")); } if ($_GET[mod]=="post"){ if ($_POST[user]){ foreach ($_POST[user] as $key => $value){ $content = $_POST['content']; $subject = $_POST['subject']; $query ="INSERT INTO messages (title, content, recipient, from1) VALUES ('$subject', '$content', '$value','$_SESSION[username]')"; mysql_query($query) or die('Error, query failed'); } } ?> <form id="form1" name="form1" method="post"> <p>Subject: <label for="subject"></label> <input type="text" name="subject" id="subject" <?php if ($message[subject]){echo 'value="RE: '.$message[subject].'"';} ?> /> </p> <?php if (!$_GET[message_id]){ $select = mysql_query("SELECT * FROM members") or die(mysql_error()); while ($member = mysql_fetch_array($select)) { echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br>'; } } ?> <p> <label for="content">Content:</label> <textarea name="content" id="content" cols="45" rows="5"></textarea> <input type="hidden" name="user[]" id="from" value='<?php echo $message[from]; ?>' /> </p> <p> <input type="submit" name="submit" id="submit" value="Post" /> </p> </form> <?php }elseif ($_GET[mod]=="see"){ echo "FROM: $message[from]<br>TO: $message[recipient]<br>SUBJECT: $message[subject]<hr>$message[content]<hr><a href='?mod=post&message_id=$message[id]'>REPLY</a>"; }else{ echo "<a href='?mod=post'>COMPOSE</a><hr>"; $select = mysql_query("SELECT * FROM messages WHERE recipient='$_SESSION[username]'") or die(mysql_error()); while ($messages = mysql_fetch_array($select)) { echo "<a href='?mod=see&message_id=$messages[id]'>[$messages[from]] $messages[subject]</a><br>"; } } }else{ echo "You are not connected. Please login first."; } ?> From that code you can add the missing pieces you need like the status of the messages and the member login systeme I still think the checkbox is not the best way of doing this ... I would of used coma seperated usernames ... Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 ok, so how do I make it so that on the new message page, how do I make it so that I can have one of those check boxes that select all the check boxes? Quote Link to comment Share on other sites More sharing options...
drisate Posted April 22, 2011 Share Posted April 22, 2011 it would look like <?php session_start(); $DB_HOST = "localhost"; $DB_USERNAME = ""; $DB_PASSWORD = ""; $DB_NAME = ""; $DB = mysql_connect("$DB_HOST", "$DB_USERNAME", "$DB_PASSWORD") or die('DB Error: ' . mysql_error()); mysql_select_db($DB_NAME); if ($_SESSION[username]){ if ($_GET[message_id]){ $message = mysql_fetch_array(mysql_query("SELECT * FROM messages WHERE id='$_GET[message_id]' and recipient='$_SESSION[username]'")); } if ($_GET[mod]=="post"){ if ($_POST[user]){ foreach ($_POST[user] as $key => $value){ $content = $_POST['content']; $subject = $_POST['subject']; $query ="INSERT INTO messages (title, content, recipient, from1) VALUES ('$subject', '$content', '$value','$_SESSION[username]')"; mysql_query($query) or die('Error, query failed'); } } ?> <form id="form1" name="form1" method="post"> <p>Subject: <label for="subject"></label> <input type="text" name="subject" id="subject" <?php if ($message[subject]){echo 'value="RE: '.$message[subject].'"';} ?> /> </p> <?php if (!$_GET[message_id]){ ?> <SCRIPT LANGUAGE="JavaScript"> <!-- function checkAll(field) { for (i = 0; i < field.length; i++) field[i].checked = true ; } function uncheckAll(field) { for (i = 0; i < field.length; i++) field[i].checked = false ; } --> </script> <?php $select = mysql_query("SELECT * FROM members") or die(mysql_error()); while ($member = mysql_fetch_array($select)) { echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br>'; } ?> <br> <input type="button" name="CheckAll" value="Check All" onClick="checkAll(document.form1.user)"> <input type="button" name="UnCheckAll" value="Uncheck All" onClick="uncheckAll(document.form1.user)"> <? } ?> <p> <label for="content">Content:</label> <textarea name="content" id="content" cols="45" rows="5"></textarea> <input type="hidden" name="user[]" id="from" value='<?php echo $message[from]; ?>' /> </p> <p> <input type="submit" name="submit" id="submit" value="Post" /> </p> </form> <?php }elseif ($_GET[mod]=="see"){ echo "FROM: $message[from]<br>TO: $message[recipient]<br>SUBJECT: $message[subject]<hr>$message[content]<hr><a href='?mod=post&message_id=$message[id]'>REPLY</a>"; }else{ echo "<a href='?mod=post'>COMPOSE</a><hr>"; $select = mysql_query("SELECT * FROM messages WHERE recipient='$_SESSION[username]'") or die(mysql_error()); while ($messages = mysql_fetch_array($select)) { echo "<a href='?mod=see&message_id=$messages[id]'>[$messages[from]] $messages[subject]</a><br>"; } } }else{ echo "You are not connected. Please login first."; } ?> Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 sorry, I am not talking about the comment page, but the original posting page that we originally started talking about........ Quote Link to comment Share on other sites More sharing options...
drisate Posted April 22, 2011 Share Posted April 22, 2011 The code above has everything in it See messages Post message Reply message I added to it the check all and uncheck button I think you got all you need ... have fun. I am out ;-) Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 ok, here is a snippet of code, how do I make it if the message is unread, the $from will be bold, if not, it is normal? Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 22, 2011 Author Share Posted April 22, 2011 ok, so this doesn't work: <?php if (!$_GET[message_id]){ ?> <SCRIPT LANGUAGE="JavaScript"> <!-- function checkAll(field) { for (i = 0; i < field.length; i++) field[i].checked = true ; } function uncheckAll(field) { for (i = 0; i < field.length; i++) field[i].checked = false ; } --> </script> <?php $select = mysql_query("SELECT * FROM members") or die(mysql_error()); while ($member = mysql_fetch_array($select)) { echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br>'; } ?> <br> <input type="button" name="CheckAll" value="Check All" onClick="checkAll(document.form1.user)"> <input type="button" name="UnCheckAll" value="Uncheck All" onClick="uncheckAll(document.form1.user)"> <? } ?> Quote Link to comment Share on other sites More sharing options...
drisate Posted April 22, 2011 Share Posted April 22, 2011 Indeed ... i forgot the check box are set as an array ... hmm then i guess you need to do it like this <?php session_start(); $DB_HOST = "localhost"; $DB_USERNAME = ""; $DB_PASSWORD = ""; $DB_NAME = ""; $DB = mysql_connect("$DB_HOST", "$DB_USERNAME", "$DB_PASSWORD") or die('DB Error: ' . mysql_error()); mysql_select_db($DB_NAME); if ($_SESSION[username]){ if ($_GET[message_id]){ $message = mysql_fetch_array(mysql_query("SELECT * FROM messages WHERE id='$_GET[message_id]' and recipient='$_SESSION[username]'")); } if ($_GET[mod]=="post"){ if ($_POST[user]){ foreach ($_POST[user] as $key => $value){ $content = $_POST['content']; $subject = $_POST['subject']; $query ="INSERT INTO messages (title, content, recipient, from1) VALUES ('$subject', '$content', '$value','$_SESSION[username]')"; mysql_query($query) or die('Error, query failed'); } } ?> <form id="form_id" name="myform" method="post"> <p>Subject: <label for="subject"></label> <input type="text" name="subject" id="subject" <?php if ($message[subject]){echo 'value="RE: '.$message[subject].'"';} ?> /> </p> <?php if (!$_GET[message_id]){ ?> <script type="text/javascript"> <!-- var formblock; var forminputs; function prepare() { formblock= document.getElementById('form_id'); forminputs = formblock.getElementsByTagName('input'); } function select_all(name, value) { for (i = 0; i < forminputs.length; i++) { // regex here to check name attribute var regex = new RegExp(name, "i"); if (regex.test(forminputs[i].getAttribute('name'))) { if (value == '1') { forminputs[i].checked = true; } else { forminputs[i].checked = false; } } } } if (window.addEventListener) { window.addEventListener("load", prepare, false); } else if (window.attachEvent) { window.attachEvent("onload", prepare) } else if (document.getElementById) { window.onload = prepare; } //--> </script> <?php $select = mysql_query("SELECT * FROM members") or die(mysql_error()); while ($member = mysql_fetch_array($select)) { echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br>'; } ?> <br><br><a href="#" onClick="select_all('user', '1');">Check All Locations</a> | <a href="#" onClick="select_all('user', '0');">Uncheck All Locations</a><br><br> <? } ?> <p> <label for="content">Content:</label> <textarea name="content" id="content" cols="45" rows="5"></textarea> <input type="hidden" name="user[]" id="from" value='<?php echo $message[from]; ?>' /> </p> <p> <input type="submit" name="submit" id="submit" value="Post" /> </p> </form> <?php }elseif ($_GET[mod]=="see"){ echo "FROM: $message[from]<br>TO: $message[recipient]<br>SUBJECT: $message[subject]<hr>$message[content]<hr><a href='?mod=post&message_id=$message[id]'>REPLY</a>"; }else{ echo "<a href='?mod=post'>COMPOSE</a><hr>"; $select = mysql_query("SELECT * FROM messages WHERE recipient='$_SESSION[username]'") or die(mysql_error()); while ($messages = mysql_fetch_array($select)) { echo "<a href='?mod=see&message_id=$messages[id]'>[$messages[from]] $messages[subject]</a><br>"; } } }else{ echo "You are not connected. Please login first."; } ?> The script, form and check buttons changed. works fine now i tested it. Quote Link to comment Share on other sites More sharing options...
searls03 Posted April 23, 2011 Author Share Posted April 23, 2011 how can I make it work with this form code? all the other stuff like connecting and stuff is set up...........: form: <form id="form1" name="form1" method="post" action="newmess.php"> <center> <table width="323" border="1" cellspacing="2" cellpadding="2"> <tr> <th width="58" scope="col">Subject: <label for="subject"></label></th> <th width="245" scope="col"><input type="text" name="title" id="subject" /></th> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> <br /></td> <td><?php $select = mysql_query("SELECT * FROM members") or die(mysql_error()); while ($member = mysql_fetch_array($select)) { echo '<input type="checkbox" name="user[]" value="'.$member[name].'">'.$member[name].'<br>'; } ?></td> </tr> <tr> <td><label for="content2">Content:</label></td> <td><textarea name="content" id="content" cols="45" rows="5"></textarea></td> </tr> <tr> <td><input type="hidden" name="from" id="from" value='<?php echo $name; ?>' /></td> <td><input type="submit" name="submit" id="submit" value="Post" /></td> </tr> </table></center> <p> </p> <p> </p> <p> </p> <p> </p> </form> posting: <?php if ($_POST[user]){ foreach ($_POST[user] as $key => $value){ // [..] code that adds the message to the database $content = $_POST['content']; $from = $_POST['from']; $subject = $_POST['title']; $query ="insert into messages(title, content, recipient, from1) VALUES ('$subject', '$content', '$value','$from')"; mysql_query($query) or die('Error, query failed'); } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.