Jump to content

I Need Help Displaying Web Page Title & URL In E-Mail Form!


designer76

Recommended Posts

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();

?>

Link to comment
Share on other sites

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']}";

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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?!?!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

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.