designer76 Posted April 22, 2011 Share Posted April 22, 2011 Hi, I have this simple e-mail script that I am modifying to my liking. I know how to do everything I want to do, except make the script e-mail the recipient the page title and url link of the website page this is form is sent from. Since the link to this file will be included in pretty much every page on my site, I can't put static url links and page titles in the code below. I need a way for the page title and url to be pulled from the active page and be displayed in the e-mail once the person receives it. Just to be clear how this will work; there will be a link on every web page that people can click that will pop-up a window that will have this form & script in it where they can enter in their e-mail, a friends e-mail and a message and then send it off. I have tried a bunch of methods myself, but I am not getting good results. Can anyone help me with this? I am at my wits end. Thanks! <?php //minimum characters allowed in the message box $msg_min_chars = "10"; //maximum characters allowed in the message box $msg_max_chars = "250"; $errors = array(); function validate_form_items() { global $msg_min_chars, $msg_max_chars; $msg_chars = "{".$msg_min_chars.",".$msg_max_chars."}"; $form_items = array( "name" => array( "regex" => "/^([a-zA-Z '-]+)$/", "error" => "Your name appears to be in improper format", ), "email" => array( "regex" => "/^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/", "error" => "email address is invalid", ), "message" => array( "regex" => "/.*/", "error" => "Your message is either too short or exceeds $msg_max_chars characters", ), ); global $errors; if(!preg_match($form_items["name"]["regex"], $_POST["your_name"])) $errors[] = $form_items["name"]["error"]; if(!preg_match($form_items["email"]["regex"], $_POST["your_email"])) $errors[] = "your ".$form_items["email"]["error"]; if(!preg_match($form_items["email"]["regex"], $_POST["friend_email1"])) $errors[] = "Friend 1 ".$form_items["email"]["error"]; if(strlen(trim($_POST["message"])) < $msg_min_chars || strlen(trim($_POST["message"])) > $msg_max_chars ) $errors[] = $form_items["message"]["error"]; if(trim($_POST["friend_email2"]) != "") { if(!preg_match($form_items["email"]["regex"], $_POST["friend_email2"])) $errors[] = "Friend 2 ".$form_items["email"]["error"]; } if(trim($_POST["friend_email3"]) != "") { if(!preg_match($form_items["email"]["regex"], $_POST["friend_email3"])) $errors[] = "Friend 3 ".$form_items["email"]["error"]; } return count($errors); } function email($from, $from_name, $to, $message) { //header("Location: thankyou.html");return; $headers .= "From: ".$from."\r\n"; $headers .= "Content-type: text/plain; charset=ISO-8859-1"; $your_domian_name = "www.yourdomain.com"; //edit what you want your vistors to see in their email here $subject = $from_name." sent you an invitation to $your_domian_name"; $your_message = "Hi!\r\n"; $your_message.= ucfirst($from_name); $your_message.= " wants you to check out $your_domian_name\r\n"; $your_message.= "Sender's Message:\n\r"; $message=$your_message.stripslashes($message); if (mail($to,$subject,$message,$headers) ) { return true; } else { return false; } } function print_error($errors) { foreach($errors as $error) { $err.=$error."<br/>"; } echo "<div style=\"border:1px red solid; font-size:14px; font-weight:normal; color:red; margin:10px; padding:10px;\"> $err <div>"; } function form_process() { $from_name = $_POST["your_name"]; $from_email = $_POST["your_email"]; $to = $_POST["your_email"].",".$_POST["friend_email1"].",".$_POST["friend_email2"].",".$_POST["friend_email3"]; $message = $_POST["message"]; $error_count = validate_form_items(); if($error_count == 0) { if(email($from_email, $from_name, $to, $message)) header("Location: thankyou.html"); else { global $errors; $errors[] = "Email coudn't be send at this time. <br>Please report the webmaster of this error."; } } } if(isset($_POST["submit"])) form_process(); ?> Quote Link to comment Share on other sites More sharing options...
designer76 Posted April 23, 2011 Author Share Posted April 23, 2011 Can anyone help me with this? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 This should get your url. For the title, you can store it in a variable in the script, and use the variable to populate the <title></title> tag then send it along with the mail form. $domain = 'http://www.your_domain.com'; $url = "{$domain}{$_SERVER['SCRIPT_NAME']}"; Quote Link to comment Share on other sites More sharing options...
designer76 Posted April 23, 2011 Author Share Posted April 23, 2011 This should get your url. For the title, you can store it in a variable in the script, and use the variable to populate the <title></title> tag then send it along with the mail form. $domain = 'http://www.your_domain.com'; $url = "{$domain}{$_SERVER['SCRIPT_NAME']}"; Thanks for the help, but I am still a little confused as to how this would work. I would need a way to pass the url and title from one page to another, because the code I posted above is in a window that pop-ups when a link is clicked. Essentially, I am trying to do the exact same thing as the e-mail button from: http://www.addthis.com/ does (it e-mails the url title and url hyperlink of whatever page you are on), but I want to customize the look and features of the pop-up window, that is why I am building my own. Thanks again for your help! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 Put $url in a hidden form field, and process it just like all the other form fields. Quote Link to comment Share on other sites More sharing options...
designer76 Posted April 23, 2011 Author Share Posted April 23, 2011 Put $url in a hidden form field, and process it just like all the other form fields. OK, thanks for your continued help. Can you take a look at what I did and maybe tell me where I screwed up? I still can't seem to get this working correctly. I have a feeling that I am completely off-base, but maybe I am not. File that has the URL I want displayed: <?php session_start(); $_SESSION['site_url'] $domain = 'http://www.your_domain.com'; $url = "{$domain}{$_SERVER['SCRIPT_NAME']}"; ?> <form target="form" action="http://www.mydomain.com/sendscript.php" method="post"> <input type="hidden" name="site_url" value="$url"> <input type="hidden" name="site_title" value=""> <input type="image" src="http://www.mydomain.com/images/button.jpg" border="0" name="submit" alt="Send This"> </form> File that has the E-Mail form and the sending script: $_SESSION['site_url'] = $_POST['site_url']; $puturl = $_REQUEST['site_url']; $message = "Hello, please look at $puturl" Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 You're pretty close, but you have to echo a php variable from within <?php ?> tags. <input type="hidden" name="site_url" value="<?php echo $url; ?>"> Quote Link to comment Share on other sites More sharing options...
designer76 Posted April 23, 2011 Author Share Posted April 23, 2011 You're pretty close, but you have to echo a php variable from within <?php ?> tags. <input type="hidden" name="site_url" value="<?php echo $url; ?>"> Thanks again. I am not really sure where to go from here, as it is still not working. I checked my page source and the url IS echoing properly now, so that is good. The problem is that when I click the link to open my e-mail form, I fill out the fields and hit the submit button, but the session is not carrying over/the web link is not showing up in the e-mail. I just get a blank space where the web url address is supposed to be. Do you have any ideas what could be the issue? Every other part of my e-mail form using variables is working but this one, and I have tried just about all that I could, and I am stumped as to why this isn't working?!?! Quote Link to comment Share on other sites More sharing options...
designer76 Posted April 23, 2011 Author Share Posted April 23, 2011 Actually, I just checked and the session IS carrying over from my page to my e-mail form, but for whatever reason the actual link isn't outputting in the e-mail body. I just don't get it. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 23, 2011 Share Posted April 23, 2011 You're going to need to pass the values to the whichever function is sending the email, and concatenate them into the message body's variable. Quote Link to comment Share on other sites More sharing options...
designer76 Posted April 24, 2011 Author Share Posted April 24, 2011 You're going to need to pass the values to the whichever function is sending the email, and concatenate them into the message body's variable. I figured out what it was, I forgot to echo it out in the e-mail form first. That's why it wasn't showing up properly in the e-mail. One last question. How would I go about finding the page title again? I am going to put that in the e-mail as well. Thanks again! Quote Link to comment Share on other sites More sharing options...
designer76 Posted April 24, 2011 Author Share Posted April 24, 2011 Actually, I think I know how to get the page title. I could use some help figuring out how to keep the .php extension from showing up in the url in the e-mail. I am using .htaccess for my site to hide the extension, but that doesn't help me in this instance. Thanks! Quote Link to comment 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.