Search code examples
phpformsemailbuttonsend

PHP: Getting the email from a form and sending it when a button is clicked


So I want the site to send 2 emails - to the staff and to the client. The staff has a preset email, while the client has to write it inside a text box (form). I tried to get the client's mail.

<form id='contact-info' method='post' novalidate class="form contact-info">
    <div class="contact-info__field contact-info__field-mail">
        <input type='email' name='user-mail' placeholder='Your email' class="form__mail">
    </div>
    <div class="contact-info__field contact-info__field-tel">
        <input type='tel' name='user-tel' placeholder='Phone number' class="form__mail">
    </div>
</form>


</div>

<div class="order">
    <a href="Rezervare-final.html" name="button1" class="btn btn-md btn--warning btn--wide">Cumpara</a>
</div>

And here's the php right after the html.

<?php
if (isset($_POST['button1'])) {
    $toclient = "Test client";
    $tostaff = "Test staff";
    $maile = $_POST['user-mail'];
    $toclient = wordwrap($toclient, 70);
    $tostaff = wordwrap($tostaff, 70);
    mail($maile, 'My Subject', $toclient);
    mail('example@yahoo.com', 'My Subject', $tostaff);
}
?>

I am able to receive the staff email, but not the client email. Question: why can't I receive the client email?


Solution

  • You are not using submit button to submit data so you can't get the $_POST['user-mail'] value.

    if you use code something like,it should work

    <form id='contact-info' method='post' class="form contact-info" action="" >
                    <div class="contact-info__field contact-info__field-mail">
                        <input type='email' name='user-mail' placeholder='Your email' class="form__mail">
                    </div>
                    <div class="contact-info__field contact-info__field-tel">
                        <input type='tel' name='user-tel' placeholder='Phone number' class="form__mail">
                    </div>
                              <input type='submit' name='button1' value="submit">
                </form>
    
    
            </div>