-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 protected], 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 protected]">
    

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;
    }
}
?> 
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. Commented Feb 10, 2022 at 23:46

1 Answer 1

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 = "[email protected]"; so you don't have dangling input type="hidden in your form

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.