0

I have a php contact form. It is not working, I don't know why.

HTML CODE:

    <!-- Contact section -->
    <section class="contact" id="contact">
        <h2 class="heading">Contact <span>Me !</span></h2>

        <form action="contact.php">
            <div class="input-box">
                <input type="text" name="name" placeholder="Full Name">
                <input type="email" name="email" placeholder="Email Address">
            </div>
            <div class="input-box">
                <input type="number" name="number" placeholder="Mobile Number">
                <input type="text" name="subject" placeholder="Email Subject">
            </div>
            <textarea name="message" id="" cols="30" rows="10" placeholder="Your Message"></textarea>
            <input type="submit" value="Send Message" class="btn">
        </form>
    </section>
    <!-- Contact section end -->`

PHP CODE:

<?php

if(isset($_POST['submit'])){
    
    // Collect form data //
    $name = $_POST['name'];
    $email = $_POST['email'];
    $mobile = $_POST['number'];
    $subject = $_POST['subject'];
    $message = $_POST['message'];
    
    // My email //
    $to = "#mail@mail.com#";
    
    // Set email headers //
    $headers = "From: " . $name . " <" . $email . ">\r\n";
    $headers .= "Reply-To: " . $email . "\r\n";
    
    // Build email content //
    $email_content = "Name: " . $name . "\n";
    $email_content .= "Email: " . $email . "\n";
    $email_content .= "Mobile Number: " . $number . "\n";
    $email_content .= "Subject: " . $subject . "\n";
    $email_content .= "Message: " . $message . "\n";
    
    // Send email //
    mail($to, $subject, $email_content, $headers);

    header("Location: index.html");
}

?>

I try to make own contact form php fail on my webpage.

halfer
  • 19,824
  • 17
  • 99
  • 186
Peyo Videv
  • 16
  • 2
  • 1
    A guess at a glance... The submit button has no `name` attribute, so that very first `if` condition in the PHP code is probably `false`. – David May 16 '23 at 17:21
  • 1
    Change `` to `` – Masivuye Cokile May 16 '23 at 17:23
  • Not working link massage: contact.php?name=Test+Test&email=test%40mail.com&number=4205484118415&subject=Test&message=testetststs&submit=Send+Message – Peyo Videv May 16 '23 at 17:25
  • yes @MasivuyeCokile is right. give name="submit" to submit input field – Developer May 16 '23 at 17:29
  • @MasivuyeCokile Not working - url massage: contact.php?name=Test+Test&email=test%40mail.com&number=4205484118415&subject=Test&message=testetststs&submit=Send+Message – Peyo Videv May 16 '23 at 17:32
  • 2
    @PeyoVidev you also need to add `method="POST"` on the `
    ` tag
    – Masivuye Cokile May 16 '23 at 17:43
  • Your form has no `method`. The default action is `get`, so you'll need to add `method="post"` to your form – aynber May 16 '23 at 17:46
  • Always double-check your work closely, and pay attention to details in tutorials. Take a tutorial about HTML forms if you don't understand the basic principles of how they work (which it seems, based on the simple mistakes here, that you don't). – ADyson May 16 '23 at 17:53
  • @ADyson that is really my fail i don't see method tag i dont write... and don't see that fail i find fail in php but my php is tier 0 – Peyo Videv May 16 '23 at 17:59
  • Ok. Have a look at things like https://developer.mozilla.org/en-US/docs/Learn/Forms/Your_first_form and https://www.php.net/manual/en/tutorial.forms.php to improve your core knowledge. – ADyson May 16 '23 at 21:03
  • 1
    `$headers = "From: " . $name . " <" . $email . ">\r\n";`- very bad idea, see https://stackoverflow.com/q/24644436/1427878, section titled "Don't use a faux From: sender" – CBroe May 17 '23 at 06:25

0 Answers0