Jump to content

send messages to members


searls03

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/234385-send-messages-to-members/
Share on other sites

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.

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');


}
}}



?>

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.....

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');

    }
}
?>

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');

    }
}
?>

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

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 ...

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.";
}
?>

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)">
<?
}
?>

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.

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');



}
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.