webguync Posted May 20, 2010 Share Posted May 20, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/ Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 hi, wanted to bump this back to the forefront. Need to get this resolved, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061614 Share on other sites More sharing options...
waterssaz Posted May 21, 2010 Share Posted May 21, 2010 Thats because you are sending $name as a string literal and not a variable. Take it out of the quotes like this: mail('[email protected]','Application Completed',$name.' has completed the exam',$body); Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061616 Share on other sites More sharing options...
salih0vicX Posted May 21, 2010 Share Posted May 21, 2010 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 . Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061621 Share on other sites More sharing options...
salih0vicX Posted May 21, 2010 Share Posted May 21, 2010 LOL waterssaz was faster Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061622 Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061644 Share on other sites More sharing options...
salih0vicX Posted May 21, 2010 Share Posted May 21, 2010 Well you solved one thing but the variable has no value. Do print($variable) before mail(...) line - it will be empty. So you need to look for a problem in the script where you assigning value to $variable. Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061647 Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 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()); Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061667 Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 Do you assign a value to the variable $name anywhere in your script? Ken Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061670 Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 I think that is being set after name is extracted via SQL and set as a session var. $_SESSION['name'] = $row['name']; Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061675 Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 Please post the whole script. Seeing one line at a time is not going to help solve your problem. Ken Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061679 Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061683 Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 At the start of the script that is sending the email message, put <?php echo '<pre>' . print_r($_POST,true) . '</pre>'; ?> This will tell you what's coming in from the form. Ken Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061697 Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 when you say at the start of the script, do you mean the line above mail()? Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061700 Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 I mean right after the session_start(); line. Ken Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061702 Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 I just get [name] => name [code=php:0] doesn't evaluate to anything, but like I say it is there on the form page, just doesn't post. Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061713 Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 Since the variable is coming from a Session, I tried $_SESSION['name'] = $_POST['name']; but this didn't work either. Any other ideas? Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061751 Share on other sites More sharing options...
kenrbnsn Posted May 21, 2010 Share Posted May 21, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061759 Share on other sites More sharing options...
webguync Posted May 21, 2010 Author Share Posted May 21, 2010 ok, that got it, thx! Quote Link to comment https://forums.phpfreaks.com/topic/202432-adding-variable-information-to-mail-script/#findComment-1061778 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.