Jump to content

Improving invite script


nadz

Recommended Posts

I have an invite script that allows my users to invite their contacts. it puts all their contacts in an array and emails each one. id like the script to split the contacts in to 2 groups - registered and unregistered so it can send an invite email to one group and a friend request to the other. any snippets of code that would help will be appreciated.

Link to comment
Share on other sites

<?php

$emails = array("sitemember@gmail.com", "justafriend@hotmail.com");

foreach($emails as $email) {
$q = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
$rows = mysql_num_rows($q) or die(mysql_error());
if($rows > 0) {
  // User is already a member, just send a friend request
} else {
  // User is not a member, ask them to join the site
}
}

?>

 

Not tested, but should work :)

Link to comment
Share on other sites

ok, this is how it usually looks:

 

	<?php

$db_host = "mysql.*****.co.uk";
$db_user = "*****";
$db_pwd = "****";
$db_name = "****";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

include_once("settings.php");
if (!$_POST)
{
  include("form.php");
}
else if(!$_POST['formname'] || $_POST['formname'] != "invite")
{
  include_once($scripts[$iscript]['filename']);
  $login = $_POST['username'];
  $name = $_POST['name'];

  $resultarray = get_contacts($login, $password);


#if contacts were retreived successfully:
  if(is_array($resultarray))
{
    #the first array_shift of the result will give you the names in an array
	$names = array_shift($resultarray);
	#the second array_shift of the result will give you the emails
	$emails = array_shift($resultarray);

			   if (!eregi("@", $login))
			   {
			   		$login = $login . "@" . strtolower($iscript) . ".com";
			   }

                   echo '<div align="center" style="padding:5; width:350;">';
                                      echo '<form method="POST" action="'.$formaction .'" name="inviteform" id="inviteform"><table style="background-color:white; border:black solid thin;">';
                   echo '<SCRIPT LANGUAGE="JavaScript">' ."\n"
                           .'    function togglechecked(){ ' . "\n"
                           .'      for (var i = 0; i < document.inviteform.elements.length; i++) {' . "\n"
                           .'        var e = document.inviteform.elements[i];' . "\n"
                           ."        if ((e.disabled == false) && (e.name != 'allbox') && (e.type == 'checkbox')) {" ."\n"
                           .'    e.checked = document.inviteform.allbox.checked;' . "\n"
                           .'        }' . "\n"
                           .'      }' . "\n"
                           .'    }' . "\n"
                           .'    function toggleselect(){ ' . "\n"
                           .'      document.inviteform.allbox.checked = !document.inviteform.allbox.checked;' . "\n"
                           .'        togglechecked();}' . "\n"

                           .'    </SCRIPT>'  . "\n";
                   echo "<tr bgcolor=\"#FFFFFF\"><td colspan=\"3\" align=\"center\"><h1 align=\"center\">Invite Contacts</h1>$logo</td></tr>";
                   echo "<tr bgcolor=\"#CCCCCC\"><td>" . "<input type=\"checkbox\" name=\"allbox\" id=\"allbox\" value=\"nothing\" onClick=\"togglechecked()\" checked>" . '</td><td><b>Name</b></td><td><b>Email</b></td></tr>';
                   echo '<input type="hidden" name="formname" value="invite">';
                   echo "<input type=\"hidden\" name=\"sender\" value=\"$login\">";
                   echo "<input type=\"hidden\" name=\"name\" value=\"$name\">";
                   
                   $maxin = count($names);


                   for ($i=0; $i<$maxin; ++$i)
                   {
                     $emails[$i] = trim($emails[$i]);
                     if ($emails[$i]!="" && eregi("@", $emails[$i]))
                     {
          					   $emails[$i] = strtolower($emails[$i]);
                       echo "<tr><td>" . "<input type=\"checkbox\" name=\"addresses[]\" value=\"$emails[$i]\" checked>" . "</td><td>$names[$i]</td><td>$emails[$i]</td></tr>";
                     }
                   }
                   echo <<< _end_this
               <tr>
               <tr><td><input type="checkbox" name="allbox2" value="nothing" onClick="toggleselect()" checked></td><td><a href="javascript:toggleselect()">Select/Deselect All</a></td><td></td></tr>

             <td colspan="3" style="padding:4"><input name="submit" type="submit" value="Invite Selected" style="width:100%"></td>
           </tr>
          </table></form>
</div>
_end_this;
}
  else #else print out the form with the error message
  {
    switch ($resultarray)
    {
      case 1: #invalid login
        $formdisclaimer = "<br><b style=\"color:red\">Invalid Login</b><br>";
        break;
      case 2: #empty username or password
        $formdisclaimer = "<br><b style=\"color:red\">Enter Your Username and Password</b><br>";
        break;
    }
  	include("form.php");
  }
}
else if ($_POST['formname'] == "invite")
{
  $message = file_get_contents($basedir . $slash . "email.html");
  $subject = file_get_contents($basedir . $slash . "emailsubject.txt");


  $addressesStr = implode(",", $_POST['addresses']);

  $headers = "";
  
  if ($fromfield && $fromfield!="")
  {
    $from = $fromfield;
  }
  else
  {
  $from = trim($_POST['name']) . " <" . trim($_POST['sender']) . ">";
  	$headers .= "From: $from\are\n";
  }
  
  $headers .= "Bcc: $addressesStr" . "\are\n";

  $message = str_replace("[[[sender]]]", $_POST['name'], $message);
  $subject = str_replace("[[[sender]]]", $_POST['name'], $subject);

  $headers .= "MIME-Version: 1.0\are\n" .
         "Content-Type: text/html;\are\n";
  
	if(mail ($tofield, $subject, "\are\n". $message, $headers))
	  $msg = "Done!";
	else
	  $msg = "Error occured";

echo <<< _end_sent
<style type="text/css">
<!--
.formheading
{
color:black;
font-size:24px;
font-family:Arial, sans-serif;
font-weight:bolder;
}
.scriptlinks a
{
color:blue;
  text-decoration:none;
}

-->
</style> 
  <div style="padding:5; background-color: #EEEEEE; width:350;">
  <div style="width:340; border: black thin solid; background-color:white;" align="center"><h1>$msg</h1>
<p>Your friends have been invited. <a href="/">Click here</a> to login </p><br><br><br></div>
</div> 	
_end_sent;

}

?>

 

and this is how im trying it:

 

	<?php

$db_host = "mysql.*****.co.uk";
$db_user = "*****";
$db_pwd = "****";
$db_name = "****";
mysql_connect($db_host, $db_user, $db_pwd);
mysql_select_db($db_name);

include_once("settings.php");
if (!$_POST)
{
  include("form.php");
}
else if(!$_POST['formname'] || $_POST['formname'] != "invite")
{
  include_once($scripts[$iscript]['filename']);
  $login = $_POST['username'];
  $name = $_POST['name'];

  $resultarray = get_contacts($login, $password);


#if contacts were retreived successfully:
  if(is_array($resultarray))
{
    #the first array_shift of the result will give you the names in an array
	$names = array_shift($resultarray);
	#the second array_shift of the result will give you the emails
	$emails = array_shift($resultarray);

foreach($emails as $email) {
$q = mysql_query("SELECT * FROM user WHERE email = '$email'") or die(mysql_error());
$rows = mysql_num_rows($q) or die(mysql_error());
if($rows > 0) {
  // User is already a member, just send a friend request
} else {

			   if (!eregi("@", $login))
			   {
			   		$login = $login . "@" . strtolower($iscript) . ".com";
			   }

                   echo '<div align="center" style="padding:5; width:350;">';
                                      echo '<form method="POST" action="'.$formaction .'" name="inviteform" id="inviteform"><table style="background-color:white; border:black solid thin;">';
                   echo '<SCRIPT LANGUAGE="JavaScript">' ."\n"
                           .'    function togglechecked(){ ' . "\n"
                           .'      for (var i = 0; i < document.inviteform.elements.length; i++) {' . "\n"
                           .'        var e = document.inviteform.elements[i];' . "\n"
                           ."        if ((e.disabled == false) && (e.name != 'allbox') && (e.type == 'checkbox')) {" ."\n"
                           .'    e.checked = document.inviteform.allbox.checked;' . "\n"
                           .'        }' . "\n"
                           .'      }' . "\n"
                           .'    }' . "\n"
                           .'    function toggleselect(){ ' . "\n"
                           .'      document.inviteform.allbox.checked = !document.inviteform.allbox.checked;' . "\n"
                           .'        togglechecked();}' . "\n"

                           .'    </SCRIPT>'  . "\n";
                   echo "<tr bgcolor=\"#FFFFFF\"><td colspan=\"3\" align=\"center\"><h1 align=\"center\">Invite Contacts</h1>$logo</td></tr>";
                   echo "<tr bgcolor=\"#CCCCCC\"><td>" . "<input type=\"checkbox\" name=\"allbox\" id=\"allbox\" value=\"nothing\" onClick=\"togglechecked()\" checked>" . '</td><td><b>Name</b></td><td><b>Email</b></td></tr>';
                   echo '<input type="hidden" name="formname" value="invite">';
                   echo "<input type=\"hidden\" name=\"sender\" value=\"$login\">";
                   echo "<input type=\"hidden\" name=\"name\" value=\"$name\">";
                   
                   $maxin = count($names);


                   for ($i=0; $i<$maxin; ++$i)
                   {
                     $emails[$i] = trim($emails[$i]);
                     if ($emails[$i]!="" && eregi("@", $emails[$i]))
                     {
          					   $emails[$i] = strtolower($emails[$i]);
                       echo "<tr><td>" . "<input type=\"checkbox\" name=\"addresses[]\" value=\"$emails[$i]\" checked>" . "</td><td>$names[$i]</td><td>$emails[$i]</td></tr>";
                     }
                   }
}
}
                   echo <<< _end_this
               <tr>
               <tr><td><input type="checkbox" name="allbox2" value="nothing" onClick="toggleselect()" checked></td><td><a href="javascript:toggleselect()">Select/Deselect All</a></td><td></td></tr>

             <td colspan="3" style="padding:4"><input name="submit" type="submit" value="Invite Selected" style="width:100%"></td>
           </tr>
          </table></form>
</div>
_end_this;
}
  else #else print out the form with the error message
  {
    switch ($resultarray)
    {
      case 1: #invalid login
        $formdisclaimer = "<br><b style=\"color:red\">Invalid Login</b><br>";
        break;
      case 2: #empty username or password
        $formdisclaimer = "<br><b style=\"color:red\">Enter Your Username and Password</b><br>";
        break;
    }
  	include("form.php");
  }
}
else if ($_POST['formname'] == "invite")
{
  $message = file_get_contents($basedir . $slash . "email.html");
  $subject = file_get_contents($basedir . $slash . "emailsubject.txt");


  $addressesStr = implode(",", $_POST['addresses']);

  $headers = "";
  
  if ($fromfield && $fromfield!="")
  {
    $from = $fromfield;
  }
  else
  {
  $from = trim($_POST['name']) . " <" . trim($_POST['sender']) . ">";
  	$headers .= "From: $from\are\n";
  }
  
  $headers .= "Bcc: $addressesStr" . "\are\n";

  $message = str_replace("[[[sender]]]", $_POST['name'], $message);
  $subject = str_replace("[[[sender]]]", $_POST['name'], $subject);

  $headers .= "MIME-Version: 1.0\are\n" .
         "Content-Type: text/html;\are\n";
  
	if(mail ($tofield, $subject, "\are\n". $message, $headers))
	  $msg = "Done!";
	else
	  $msg = "Error occured";

echo <<< _end_sent
<style type="text/css">
<!--
.formheading
{
color:black;
font-size:24px;
font-family:Arial, sans-serif;
font-weight:bolder;
}
.scriptlinks a
{
color:blue;
  text-decoration:none;
}

-->
</style> 
  <div style="padding:5; background-color: #EEEEEE; width:350;">
  <div style="width:340; border: black thin solid; background-color:white;" align="center"><h1>$msg</h1>
<p>Your friends have been invited. <a href="/">Click here</a> to login </p><br><br><br></div>
</div> 	
_end_sent;

}

?>

 

the problem is now i just get an "invalid login" everytime i test the script.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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