Search code examples
phphtmlwampservermail-server

Contact forms values goes into URL


So i´m currently working on a website where i want the customers to be able to mail directly to my email via a Contact Form. I am right now running all the files trough a wamp server 3 and right now I´m testing if the mail function works with Test Mail Server that listen to Port 25.

This is the PHP code for the form:

<?php

$myemail  = "test.xample@xample.com";

if (isset($_POST['email'])) 
{
    echo "Thank you for contacting us!";

    /* Set e-mail recipient */

    /* Check all form inputs using check_input function */
    $name = ($_POST['name']);
    $subject  = ($_POST['subject']);
    $email    = ($_POST['email']);
    $message = ($_POST['message']);

    /* If e-mail is not valid show error message */
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
        show_error("E-mail address not valid");
    }

    if ($name = "") 
    {
        show_error("You need to write your name...");
    }

    if ($email = "") 
    {
        show_error("You need to enter a email...");
    }

    if ($message = "") 
    {
        show_error("A message is required if you wish to have out help...");
    }
    /* Let's prepare the message for the e-mail */
    $mail = "Hello!

    Your contact form has been submitted by:

    Name: $yourname
    E-mail: $email

    Message:
    $Message

    End of message
    ";

    /* Send the message using mail() function */
    mail($myemail, $subject, $message);

    /* Functions we used */
    function check_input($data, $problem='error')
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        if ($problem && strlen($data) == 0)
        {
            show_error($problem);
        }
        return $data;
    }

    function show_error($myError)
    {
    ?>
        <html>
        <body>

        <b>Please correct the following error:</b><br />
        <?php echo $myError; ?>

        </body>
        </html>
    <?php
    exit();
    }
}
?>

And this is the HTML part of the form:

<form class="mt-5 ml-5 mr-5" method="_POST" action="handler.php" id="reused_form">
        <p id="contactForm" class="h4 text-center mt-5"><strong>Kontakta oss</strong></p>

        <!-- input text(Name) -->
        <div class="md-form">
            <i class="fa fa-user prefix">*</i>
            <input type="text" name="name" id="name" class="form-control">
            <label for="name">Ditt namn</label>
        </div>

        <!-- input email -->
        <div class="md-form mt-5">
            <i class="fa fa-envelope prefix">*</i>
            <input type="email" name="email" id="email" class="form-control validate">
            <label for="email" data-error="Fel" data-success="Rätt">Din email</label>
        </div>

        <div class="md-form mt-5">
            <i class="fa fa-user prefix"></i>
            <input type="text" id="subject" class="form-control">
            <label for="subject">Ämne</label>
        </div>

        <!-- input message -->
        <div class="md-form mt-5">
            <i class="fa fa-pencil prefix">*</i>
            <textarea type="text" name="message" id="message" maxlength="5000" class="form-control md-textarea" rows="3"></textarea>
            <label for="textareaPrefix">Meddelande</label>
        </div>

        <div class="text-center mt-4 mb-4">
            <button class="btn danger-color" type="submit">Skicka</button>
        </div>
    </form>

When i press the Submit Button the only thing that happens is that this is added to the URL:

My inputs for this test:

  • Name: asd
  • email: asd@ad
  • message: asd

/handler.php?name=asd&email=asd%40ad&message=asd

So it is just adding the values of the inputs in the URL...

What i need help with is how to solve this problem and be able to mail. I would be very pleased if someone could help with that.


Solution

  • Yo are using method=_POST which is not correct method so in default, the form uses GET method to send the data.

    Please try using method=POST instead of method=_POST and it will work perfectly.

    <form method="POST">
       .........
    </form>