-1

So I've been working on creating a contact form. No matter what I try, I can't get it to work. I've read different blogs, how to's, etc. I would love some help. Note: when it says email@email.com, I've input my own email.

HTML:

<div class="col-lg-6 offset-lg-1">
   <form action="form-data/formdata.php" class="form-widget form-control-op-02">
        <div class="field-wrp">
                <input type="hidden" name="to"value="email@email.com">
    

I won't bore you with all the details of the form fields i.e. name, phone number, email, etc.

PHP:

if (isset($_POST) && sizeof($_POST) > 0) {
    
    $to = $_POST['to']['val']; // <=== Set static email here.
    
    if (isset($_POST['formtype'])) {
        unset($_POST['formtype']);
    }
    if (isset($_POST['to'])) {
        unset($_POST['to']);
    }
    
    $email_address = $_POST['email']['val'];
    $email_subject = "Form submitted by: ".$_POST['name']['val'];
    $email_body    = "You have received a new message. <br/>".
                     "Here are the details: <br/><br/>";
                     foreach ($_POST as $key => $value) {
                        $email_body .= "<strong>" . $value['label'] . ": </strong> " . $value['val'] . "<br/><br/>";
                     }

    $headers = "From:<$email_address>\n";
    $headers.= "Content-Type:text/html; charset=UTF-8";
    if($email_address != "") {
        mail($to,$email_subject,$email_body,$headers);
        return true;
    }
}
?> 
parker
  • 1
  • You've already gotten an answer to your question, but why do you have `$_POST['to']['val']` if the you named the input `name="to"`? It should be just be `$_POST['to']` (and probably the same for the other inputs as well). It's also not good to have the "to" address in the form. People can change that and use your form to spam other people. Hard code the "to" address in your PHP instead. – M. Eriksson Feb 10 '22 at 23:46

1 Answers1

1

Change

<form action="form-data/formdata.php" class="form-widget form-control-op-02"> 

into this

<form action="form-data/formdata.php" method="post" class="form-widget form-control-op-02">

by default HTML form method is get also if i understand correctly you are storing your own Email into input type="hidden you can just store that into a PHP variable for instance $myEmail = "free2rhyme@gmail.com"; so you don't have dangling input type="hidden in your form

Emma Marshall
  • 354
  • 1
  • 12