Jump to content

adding variable information to mail script


webguync

Recommended Posts

Hi, I have a variable $name and I want the information in that variable emailed in the body of the email. I know that the variable session is working with print_r($_SESSION); but when I try to get the info mailed to me it just shows up as "$name has completed the exam".

 

Here is what I am trying.

mail('[email protected]','Application Completed','$name has completed the exam',$body);

 

any ideas on what I need to add/change?

Hi,

 

You can't use $variable under single apostrophe (')  like that. Try one of following options (I have no WAMP at work to test) and you will understand the point:

 

mail('[email protected]','Application Completed', $name .' has completed the exam',$body); //faster way

mail('[email protected]','Application Completed',"$name has completed the exam",$body);

mail('[email protected]','Application Completed','${name} has completed the exam',$body);

 

Good luck :D.

thanks for the reply. I changed to this

mail('[email protected]','Exam Completed',$name.' has completed the online  exam',$body);

 

but the variable doesn't come through. Just shows up in the email as [space]has completed the online  exam. I know the variable info is holding the right value through print_r(_$SESSION);

 

I have the email script in a separate PHP processing file from the form page. Any ideas why the variable info is not coming through?

well not sure why the $variable is empty, but that appears to be the problem. The variable holds from the login to the form page, b/c i use

Welcome! You are now logged in <span class='name'>" . $_SESSION['name'] . "</span>

 

and the value is there. In my form process page I use.

session_start();

 

at the top of my page and

$_SESSION['name'] = $row['name'];

 

the info is originally achieved when the user logs in with SQL

 

$sql = "SELECT username,password,name
           FROM   Candidates
           WHERE
           password = '$password'
           AND
           username='$username'";

$row = mysql_fetch_array($query) or die(mysql_error());

 

 

 

There is a lot to it.

login page

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US" dir="ltr">
<head>
<meta http-equiv="Content-type" content="text/html; charset=ISO-8859-1" />
<title>
Candidate Test
</title>



<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>

<script language="javascript">
//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use

$(document).ready(function()
{
$("#login_form").submit(function()
{
	//remove all the class add the messagebox classes and start fading
	$("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
	//check the username exists or not from ajax
	$.post("login.php", {username:$('#username').val(),password:$('#password').val()} ,function(data)
        {
		//alert(data);
	  if(data==1) //if correct login detail
	  {
	  	$("#msgbox").fadeTo(200,0.1,function(data)  //start fading the messagebox
		{ 
		  //add message and change the class of the box and start fading
		  $(this).html('Success!..Logging in.....').addClass('messageboxok').fadeTo(900,1,
              function()
		  { 
		  	 //redirect to secure page
			 document.location='EditorExam.php';
		  });
		  
		});
	  }
	  else 
	  {
	  	$("#msgbox").fadeTo(200,0.1,function(data) //start fading the messagebox
		{ 
		  //add message and change the class of the box and start fading
		  $(this).html('You have entered an incorrect login<br /> please try again!').addClass('messageboxerror').fadeTo(900,1).delay(5000).fadeTo(900,0);
		});		
          }

        });
		return false; //not to post the  form physically
});
//now call the ajax also focus move from 
$("#password").blur(function(data)
{
	$("#login_form").trigger('submit');
});
});
</script>

<body>
<div id="LoginContainer">

<h1 class="login">Candidate Login</h1>

<div id="form_align">

<form enctype="multipart/form-data" method="post" action="" id="login_form" >
<fieldset>
<legend>Please enter your email address and password  to login to your test.</legend>

<div class="loginwrapper">
   
<label for="username">Username:<span class='red_small'> (email address) </span></label><br />
    
<input type="text" name="username" id="username" size="20"><br /><br />
<label for="password">Password:<span class='red_small'> (you should have been given this)</span> </label><br />
<input type="password" name="password" id="password" size="20"><br /><br />
   <div class="buttondiv">
<input class="button" type="submit" name="submit" value="Login" /><span id="msgbox" style="display:none"></span>
</div>
    </div><!--end login wrapper-->
</form>
</fieldset>
</div>

</div><!--end container div-->
</body>
</html>

 

login processing

 

<?php
session_start();
$db_user = "User";
$db_pass = "Pass";
$db = "DB";

mysql_connect('localhost',$db_user,$db_pass);
mysql_select_db($db);

$username = mysql_real_escape_string($_POST['username']);
$password =(md5($_POST['password']));

$sql = "SELECT username,password,name
           FROM   Candidates
           WHERE
           password = '$password'
           AND
           username='$username'";
$dat = time() + 3600;




   
$sql_update ="UPDATE Candidates
          SET login_timestamp = DATE_ADD(NOW(), INTERVAL 2 HOUR)
          WHERE username = '$username'
          AND password = '$password'"; 


$query = mysql_query($sql) or die("Query Failed: $sql - " . mysql_error());
$num_rows = mysql_num_rows($query);
$row = mysql_fetch_array($query) or die(mysql_error());


if ($num_rows == '1')
{


$_SESSION['name'] = $row['name'];
$_SESSION['username'] = $username;
$_SESSION['sid'] = session_id(); 
// Make it more secure by storing the user's IP address.
$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];



echo '1';
} else
{
echo '0';
}

?>

 

login brings you to a secure form page

 

I am only posting the PHP part at the top of the page. The rest is HTML

 

<?php
session_start();
ini_set("display_errors","1");
ERROR_REPORTING(E_ALL);
// Start a session. If not logged in will be redirected back to login screen.

if(!isset($_SESSION['username'])){
header("Location:Login.php");
exit;
}

print_r($_SESSION);

?>

 

the code to process the form on this page.

 

<?php
session_start();
$con = mysql_connect("localhost","username","pw") or die('Could not connect: ' . mysql_error());
mysql_select_db("ETSI_Internal") or die(mysql_error());
$_SESSION['name'] = $row['name'];


$name=mysql_real_escape_string($_POST['name']); //This value has to be the same as in the HTML form file
$A1=mysql_real_escape_string($_POST['Answer1']); //This value has to be the same as in the HTML form file
$A2=mysql_real_escape_string($_POST['Answer2']); //This value has to be the same as in the HTML form file
$A3=mysql_real_escape_string($_POST['Answer3']); //This value has to be the same as in the HTML form file
$A4=mysql_real_escape_string($_POST['Answer4']); //This value has to be the same as in the HTML form file
$A5=mysql_real_escape_string($_POST['Answer5']); //This value has to be the same as in the HTML form file

$sql="INSERT INTO Responses (`name`,`Answer1`,`Answer2`,`Answer3`,`Answer4`,`Answer5`)"; /*form_data is the name of the MySQL table where the form data will be saved.
name and email are the respective table fields*/
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}

else{
print($name);
mail('[email protected]','Exam Completed',$name.' has completed the exam',$body);
echo "Your information has been submitted successfully.";

}
mysql_close($con);
?>



 

 

 

You probably want to go the other way:

<?php
$name = $_SESSION['name'];
?>

 

Also, in your script, the last line in this snippet doesn't make any sense:

<?php
session_start();
$con = mysql_connect("localhost","username","pw") or die('Could not connect: ' . mysql_error());
mysql_select_db("ETSI_Internal") or die(mysql_error());
$_SESSION['name'] = $row['name'];
?>

Since you haven't done any mysql_fetch statement yet, so remove it.

 

Ken

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.