Style a php value in a PHP mail form

I don’t understand the issue with links, surely you just put the standard html tags around it. I am hoping to continue steering well clear of regular expressions, but I can’t see where that is adding the HTML standard ‘a’ tags to the link to make it clickable, just like you would in a standard web page.

$message_body .= '<a href="' . $url . '">Click here</a>';

It’s a long time since I’ve coded a HTML email, though.

Yes but the values in the email comes from this:

unset($_POST['submit']);
foreach ($_POST as $key => $value){
$message_body .=  "$key: $value<br><br>";
}

hov do i get a link on only the website?

I’m pretty sure it is considered bad practise to loop through the POST array in case something has been injected into it. You should know what elements you are expecting, and each one should be validated or sanitised (or both) before being used.

They are being validated, see code at the top.

If it’s your form, you must know which $_POST field is the link.

unset($_POST['submit']);
foreach ($_POST as $key => $value){
if ($key == "url") { 
  // add the tags
  $message_body = '<a href="' and so on
  }
else {
  // just add the contents
  $message_body .=  "$key: $value<br><br>";
  }
}

So i tested this but it’s not working:

	unset($_POST['submit']);
	foreach ($_POST as $key => $value){
	if ($key == "url") { 
	// add the tags
	$message_body = '<a href="' . $website . '"></a>';
	}
	else {
	// just add the contents
	$message_body .=  "$key: $value<br><br>";
	}
}

Is the form field for the web site URL actually called “url”, as I’ve used in my example code above? I don’t think you showed the html for the form. Or is it called “Website” or something else? Sorry, I didn’t make it clear that was just an example and you’ve have to edit the values.

ETA: Looking further up, it looks as if the name is stored in $post_website, so you need to compare $key to that value.

This is how the form html for this:

<p class="comment-form-url"><input id="url" name="<?echo ($words[$lang]["CONTACTUS_WEBSITE"]);?>" type="url"  placeholder="<?echo ($words[$lang]["CONTACTUS_WEBSITE"]);?>" tabindex="4" value="<?= $website ?>" /><span  class="fa fa-home input_icon_home"></span><label for="url"><?echo ($words[$lang]["CONTACTUS_WEBSITE"]);?> </label><span class="required">*</span></p>
<span class="error"><?= $website_error ?></span>

And the translation file looks like this:

<?php

/* Set cookies for default language */

session_start();

header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{

$lang = $_GET["lang"];

$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'se'; /* Set default language */
}

$words = array(

"en" => array(

'CONTACTUS_WEBSITE' => "Website"),

"se" => array(

'CONTACTUS_WEBSITE' => "Hemsida"),

);

?>

Hello, i have tested this:

	unset($_POST['submit']);
	foreach ($_POST as $key => $value){
	if ($key == $post_website) { 
	// add the tags
	$message_body = '<a href="' . $website . '"></a>';
	}
	else {
	// just add the contents
	$message_body .=  "$key: $value<br><br>";
	
	}
}  

Now the only thing i get is the Message text.

Oh, cross posts.

That’s because in this line

	$message_body = '<a href="' . $website . '"></a>';

you’re not concatenating the message, so it will reset the string when it hits the URL field.

	$message_body .= '<a href="' . $website . '"></a>';

My typo earlier, by the look of it.

OK, now every thing exept the Website field is shown?
What a h*ll am i doing wrong.

unset($_POST['submit']);
foreach ($_POST as $key => $value){
if ($key == $post_website) { 
// add the tags
$message_body .= '<a href="' . $website . '"></a>';
}
else {
// just add the contents
$message_body .=  "$key: $value<br><br>";

}

}

Email fields/values displayed in the email:

Name: A Name

Email: test@mail.se

Company: A Company

Message: Test message.

i dont know if it makes a differnes but i have also changed the $website post code: to this:

  if (empty($_POST[$post_website])) {
  $website = "";
} else {
$website = test_input($_POST[$post_website]);
// check if URL address syntax is valid (this regular expression also allows dashes in the URL)
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$website_error = isset($words[$lang]['CONTACT_WEBSITE_ERROR_TEXT_TWO']) ? $words[$lang]['CONTACT_WEBSITE_ERROR_TEXT_TWO'] : "Invalid url format";
$main_error = isset($words[$lang]['CONTACT_MAIN_ERROR']) ? $words[$lang]['CONTACT_MAIN_ERROR'] : "Error!";
}
}

Hey, i solved it finaly, like this:

unset($_POST['submit']);
foreach ($_POST as $key => $value){
if ($key == $post_website) { 
// add the tags
$message_body .= '<b>Website: </b>';
$message_body .= '<a style="text-decoration: none; color="#4ca6ff" href="' .$website. '">' .$website.'</a><br><br>';
}
else {
// just add the contents
$message_body .=  "<b>$key:</b> $value<br><br>";

}
}

Ah yes, that would do it. I didn’t notice that in your post #26 you didn’t actually have anything between the opening and closing <a> tags. So it was probably there in the source, but with no text to render as a link.

OK, one last thing if you have the time?
The Company and Website fields are not mandatory, so if a user do not fill these fields out i would like there to say in the email:

Company: No Data
Website: No Data

I have tested somthing like:

unset($_POST['submit']);
foreach ($_POST as $key => $value){
	
if ($key == $post_website) { 
// add the tags
$message_body .= '<b>'.$post_website.': </b>';
$message_body .= '<a style="text-decoration: none; color="#4ca6ff" href="' .$website. '">' .$website. '</a><br><br>';
}

else if ($value == false) {
$message_body .= "<b>$key:</b> $no_data<br><br>";
}

else {
// just add the contents
$message_body .=  "<b>$key:</b> $value<br><br>";
}
}

So if i leave only the Company field empty, the code works, the email looks like this:

Name: My Name

Email: my@email.se

Company: No Data

Website: http://a-website.se

Message: A Message

So this is fine. But if i leave only the Website field empty then the email looks like this:

Name: My Name

Email: my@email.se

Company: No Data

Website:

Message: A Message

The Website filed/value is empty. Can you see way please?

Thank you.

Well, it’s not doing it because you only run the check for the value being false if it has already failed the check to see whether it’s the website address. You’d need to add similar code into the first section of the if-then-else clause.

I would say you could check for the data being empty before you open the if-then-else, but that would leave you putting a spurious link. So you just need to add another if/then inside the first one, check if the website is empty, and if it is, don’t put the link code.

Ahhh, i giv up now. But thank you for all your help.
The last thing i tested is this:

unset($_POST['submit']);
foreach ($_POST as $key => $value){
	
if ($key == $post_website) { 
// add the tags
$message_body .= '<b>' .$post_website.': </b><a style="text-decoration: none; color="#4ca6ff" href="' .$website. '">' .$website. '</a>' ."<br><br>";
	
// if Company field is left empty, display no data message in email
if (empty($company) && empty($website)){
$message_body .=  "$no_data<br><br>";
}
}

else {
// just add the contents
$message_body .=  "<b>$key:</b> $value<br><br>";
}
}

You’re still putting the link out for the website before you check whether the website is empty or not.

// pseudo-code:
loop { 
  is it the website?
    yes { 
      is the website empty? 
         yes { output "no data"
         no { output website and link
        }
    no {
       is the field empty? 
          yes { output "no data"
          no { output field
         }
   } end of loop
1 Like

Thank you for your help, but i don’t quit get this :pensive:

What part don’t you understand?

Hello, i still dont understand whare to put this code:

if (empty($company) && empty($website)){
$message_body .=  "$no_data<br><br>";
}