-1

I have a list of users and if they have an email they can click on a button to send an email. The thing is that I need to hide the EMAIL button if the user doesn't have a email.

Here is what i have:

<form action='/' method='get'>
<a href='mailto:<?=$user['email']?>'><button type="button" class="btn btn-dark btn-sm"><span>Email</span></button></a>
</form>

This is what i tryed but didn't work:

<a href='mailto:<?=$user['email']?>'><button id="sendmail" type="button" class="btn btn-dark btn-sm" onload="hideButton()"><span>Email</span></button></a>

<script type="text/javascript">
$(function(){
    function hideButton(){
        if(email === "mailto:"){
            $("#sendmail").show();
        }
        else {
            $("#sendmail").hide();
        }
    }
});
</script> 

I don't know how to solve it. I google this but most of the answers gives me an Error 500 when I upload to the website

Mr. Name
  • 3
  • 3
  • *=$user['email']?>* what syntax is this? Are you using a PHP template engine for HTML rendering? – Toxnyc Jul 16 '20 at 16:56
  • @Toxnyc - `= ... ?>` is short syntax for `` – waterloomatt Jul 16 '20 at 16:59
  • @waterloomatt that looks very clean, do you have the link to the doc? – Toxnyc Jul 16 '20 at 17:11
  • Your question is unclear and lacks detail/code and what the source of the values are. You've also been given an answer. If it solves what you asked, consider accepting it. That will mark your question as solved. – Funk Forty Niner Jul 16 '20 at 17:18
  • @Toxnyc - see https://www.php.net/manual/en/language.basic-syntax.phptags.php#language.basic-syntax.phptags. Notice the difference between _short echo tag_ (this) and _short open tag_, which should be avoided and may be removed entirely in a future version of PHP. – waterloomatt Jul 16 '20 at 18:59

1 Answers1

1

You can do it directly in PHP just by wrapping it an if statement.

<?php

if (isset($user['email']) && trim($user['email']) !== '') {
    echo '<a href="mailto:' . $user['email'] . '">Send email</a>';
}
waterloomatt
  • 3,134
  • 1
  • 16
  • 21
  • 1
    And I should also point out that if your email link is just a link, it doesn't need to be wrapped in a form. Forms are only used to submit form inputs such as text boxes, radio buttons etc. I see that you're wrapping a button within the link but this is kind of redundant. Just use the link by itself. – waterloomatt Jul 16 '20 at 17:07
  • Turn on error, reporting and then edit your _question_ with the appropriate details. https://stackoverflow.com/a/21429652/296555 – waterloomatt Jul 20 '20 at 11:44