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.
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.
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>";
}
}
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.
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:
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