Jump to content

Contact Form Page


graham23s

Recommended Posts

Hi Guys,

 

i have made a contact staff form, i query the database find all users whose user class = Site Administrator then submits a pm to each of the site admins, only the way i'm doing it only 1 og the admins gets pm'd

 

code:

 

<?php
     // get the logged in users id...////////////////////////////////////////////////////
     $query = "SELECT * FROM `membership` WHERE `username`='$member'";
     $result = mysql_query($query);
     $rows = mysql_fetch_array($result);     
     $senders_id = $rows['id'];
     
     // contact staff.../////////////////////////////////////////////////////////////////
     if(isset($_POST['submit'])) {
     
     // deal with the posts...///////////////////////////////////////////////////////////
     $subject = addslashes(mysql_real_escape_string($_POST['subject']));
     $pm_message = addslashes(nl2br($_POST['pm_message']));
     $s_id = $_POST['s_id'];
     
     // empty fields check...////////////////////////////////////////////////////////////
     if(empty($subject) || empty($pm_message)) {
     
        echo '<br /><b><font color="red">Error:</font> Sorry, Please Don\'t Leave Any Fields Blank <a href="contact_staff.php">Go Back</a></b><br /><br />';
        include("includes/footer.php");
        exit;
     
     }
     
     // now do the rest...///////////////////////////////////////////////////////////////
     $query1 = "SELECT * FROM `membership` WHERE `user_class`='Site Administrator'";
     $result1 = mysql_query($query1);
     
     while ($row = mysql_fetch_array($result1)) {
     
        $admin_ids = $row['id'];
        
     }  
     
     // insert into the pms...///////////////////////////////////////////////////////////
     $query2 = "INSERT INTO `pms` (`sender_id`,`reciever_id`,`subject`,`pm_message`,`date_added`) VALUES ('$s_id','$admin_ids','$subject','$pm_message',now())";
     $result2 = mysql_query($query2);
     
     // if it worked...//////////////////////////////////////////////////////////////////
     if($result2) {
     
        echo "<br/><b>Thank You, The Staff Have Been Contacted And Will Be In Touch ASAP</b><br/><br />";
     
     } else {
     
        echo "<br /><b><font color=\"red\">Error:</font> Sorry There Has Been An Error Sending Your Message Please Try Again.</b><br /><br />";     
     
     }
      
     
     } else {
     
     // echo out the form.../////////////////////////////////////////////////////////////
          echo '<br />
                <table width="500" border="1" bordercolor="#000000" cellspacing="0" cellpadding="4" />
                <form action="'.$_SERVER['PHP_SELF'].'" method="POST" />
                <tr>
                <td colspan="2" align="center" bgcolor="#004E98" /><font color="#ffffff"><b>Contact Staff</b></font></b></td>
                </tr>
                <tr>
                <td align="right" /><b>Subject:</b></td><td align="center"><input type="text" name="subject" size="40"></td>
                </tr>
                <tr>
                <td align="right" /><b>Message:</b></td><td align="center"><textarea name="pm_message" cols="30" rows="15"></textarea></td>
                </tr>
                <tr>
                <td bgcolor="#004E98" align="right" colspan="2"><input type="submit" name="submit" value="Contact Staff" />
                                                                <input type="hidden" name="s_id" value="'.$senders_id.'">  
                </td>
                </table><br />';
     
     }
?>

 

is there something i'm missing here?

 

cheers guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/53644-contact-form-page/
Share on other sites

<?php

     // now do the rest...///////////////////////////////////////////////////////////////
     $query1 = "SELECT * FROM `membership` WHERE `user_class`='Site Administrator'";
     $result1 = mysql_query($query1);
     
     while ($row = mysql_fetch_array($result1)) {
     
        $admin_ids = $row['id'];
        
     }  
     
?>

 

translated:

1. find all admins

2. set $admin_ids to the current one

3. next admin

4. if not at end of admin list goto 2

5. $admin_ids = last admin id in the list

 

 

move

     // insert into the pms...///////////////////////////////////////////////////////////
     $query2 = "INSERT INTO `pms` (`sender_id`,`reciever_id`,`subject`,`pm_message`,`date_added`) VALUES ('$s_id','$admin_ids','$subject','$pm_message',now())";
     $result2 = mysql_query($query2);

 

into the loop

 

IE

<?php

     // now do the rest...///////////////////////////////////////////////////////////////
     $query1 = "SELECT * FROM `membership` WHERE `user_class`='Site Administrator'";
     $result1 = mysql_query($query1);
     
     while ($row = mysql_fetch_array($result1)) {
     
        $admin_ids = $row['id'];

     // insert into the pms...///////////////////////////////////////////////////////////
     $query2 = "INSERT INTO `pms` (`sender_id`,`reciever_id`,`subject`,`pm_message`,`date_added`) VALUES ('$s_id','$admin_ids','$subject','$pm_message',now())";
     $result2 = mysql_query($query2);

     }  
     
?>

Link to comment
https://forums.phpfreaks.com/topic/53644-contact-form-page/#findComment-265173
Share on other sites

 

(*Snipping everything I just wrote that was nearly identical to MadTechie's response....*  :)  )

 

Furthermore, I think you can do it all with one query using INSERT INTO ... SELECT.

 

INSERT INTO `pms` (`sender_id`,`reciever_id`,`subject`,`pm_message`,`date_added`) SELECT '$s_id',id,'$subject','$pm_message',NOW() FROM membership WHERE user_class ='Site Administrator'

Link to comment
https://forums.phpfreaks.com/topic/53644-contact-form-page/#findComment-265177
Share on other sites

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.