0

So I can't really find the mistake I made, it was working earlier.. I have a HTML contact form that I validate with JS, when it's true, I change the form action to the php file, where it takes all the information of the contact form and sends it to my mail..

I can provide a live-version if necessary..

Example from this site: http://www.freecontactform.com/email_form.php

I only deleted the php validation..

JAVASCRIPT

function validateForm() {
    var voornaam = document.forms["contactform"]["voornaam"].value;
    var achternaam = document.forms["contactform"]["achternaam"].value;
    var straat = document.forms["contactform"]["straat"].value;
    var huisnummer = document.forms["contactform"]["huisnummer"].value;
    var gemeente = document.forms["contactform"]["gemeente"].value;
    var postcode = document.forms["contactform"]["postcode"].value;
    var email = document.forms["contactform"]["email"].value;
    var bericht = document.forms["contactform"]["bericht"].value;


    if (voornaam == null || voornaam == "") {
        alert("Je moet je voornaam invullen");
        return false;
    } else if (achternaam == null || achternaam == "") {
        alert("Je moet je achternaam invullen");
        return false;
    } else if (straat == null || straat == "") {
        alert("Je moet je straat invullen");
        return false;
    } else if (huisnummer == null || huisnummer == "" || isNaN(huisnummer)) {
        alert("Je moet je huisnummer invullen en enkel cijfers gebruiken");
        return false;
    } else if (gemeente == null || gemeente == "") {
        alert("Je moet je gemeente invullen");
        return false;
    } else if (postcode == null || postcode == "" || isNaN(postcode)) {
        alert("Je moet je postcode invullen en enkel cijfers gebruiken");
        return false;
    } else if (email == null || email == "") {
        alert("Je moet je email-adres invullen");
        return false;
    } else if (bericht == null || bericht == "") {
        alert("Je bericht is leeg! ");
        return false;
    } else {
        document.forms["contactform"].action = "send_form_email.php";
    }
}

PHP FILE

<?php

$email_to = "PERSONAL EMAIL";

$email_subject = "Test email form";

$voornaam = $_POST['voornaam'];
$achternaam = $_POST['achternaam'];
$straat = $_POST['straat'];
$huisnummer = $_POST['huisnummer'];
$gemeente = $_POST['gemeente'];
$postcode = $_POST['postcode'];
$email = $_POST['email'];
$bericht = $_POST['bericht'];

$email_message = "Informatie over de bezoeker\n\n";

function clean_string($string)
{

    $bad = array("content-type", "bcc:", "to:", "cc:", "href");

    return str_replace($bad, "", $string);
}

$email_message .= "Voornaam: " . clean_string($voornaam) . "\n";
$email_message .= "Achternaam: " . clean_string($achternaam) . "\n";
$email_message .= "Adres: " . clean_string($straat) . "\n";
$email_message .= "Huisnummer: " . clean_string($huisnummer) . "\n";
$email_message .= "Gemeente: " . clean_string($gemeente) . "\n";
$email_message .= "Postcode: " . clean_string($postcode) . "\n\n";
$email_message .= "Bericht: " . clean_string($bericht) . "\n";

$email_from = $email;

// create email headers

$headers = 'From: ' . $email_from . "\r\n" .
    'Reply-To: ' . $email_from . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($email_to, $email_subject, $email_message, $headers);

?>

    <!-- include your own success html here -->

    Thank you for contacting us. We will be in touch with you very soon.

<?php

}

?>

HTML

<form id="contactform" name="contactform" method="post">
    <div style="text-align: center; margin-left: 10%;">
        <table width="800px" id="table">
            <tr>
                <td>
                    <h1 style="font-family: 'Aller Bold';">Neem contact met ons op:</h1>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="voornaam" class="inputcontact" size="40" placeholder="Voornaam">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="achternaam" class="inputcontact" size="40" placeholder="Achternaam">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="straat" class="inputcontact" size="40" placeholder="Straat">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="huisnummer" class="inputcontact" size="40" placeholder="Huisnummer"
                           maxlength="5">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="gemeente" class="inputcontact" size="40" placeholder="Gemeente">
                </td>
            <tr>
                <td>
                    <input type="text" name="postcode" class="inputcontact" size="40" placeholder="Postcode"
                           maxlength="4">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="email" class="inputcontact" size="40" placeholder="E-mailadres"> <br> <br>
                </td>
            </tr>
            <tr>
                <td>
                    <textarea name="bericht" class="inputcontact" placeholder="Typ je bericht hier.." rows="4"
                              cols="50"></textarea> <br> <br>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" value="Verzenden" id="submitcontact" onclick="validateForm()">
                    <br>
                    <br>
                </td>
            </tr>
        </table>
    </div>
</form>
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • i see the validation is working so what's exactly not working properly? – Shehary Aug 08 '15 at 23:47
  • use `isset` to check if value is posted – Shehary Aug 08 '15 at 23:48
  • @Shehary no need for validation in PHP, I already check whether the value is empty or not with Javascript? Or what do you exactly mean? And for some reason I don't receive an email.. Also, when you view the PHP code, it should show a HTML sentence, but this isn't either.. I'm running this on a webserver with mail-provider, so I should receive the email.. – Marnik Bruyndonckx Aug 08 '15 at 23:51
  • in first comment the validation i referred was with javascript that yes it's working so you have to be more clear the part which is not working, title of your question is confusing and yes i can see the HTML message sentence in php but I'm not seeing any reason why it should show, you haven't define any condition for it to show – Shehary Aug 08 '15 at 23:54
  • @Shehary I see, I'll update my title and description. So I have no idea why it's not working, I guess there's something wrong in the php code, because I'm like you 100% sure the Javascript is working. – Marnik Bruyndonckx Aug 08 '15 at 23:56
  • so lets start with first step to check if your form posting anything at all, use `isset` like this `if (isset($_POST['submit']) { $_Post comes here }` and dump $_Post to check and also in your form submit input `` change it to `` – Shehary Aug 09 '15 at 00:00
  • in our question you said `I change the form action to the php file` but here `
    ` i don't see any action file so if `PHP code` is not on same page where form is you have to define action file like this `
    `
    – Shehary Aug 09 '15 at 00:03
  • @Shehary I change the form action to action="youactionfilename.php" if the validation is true/succeeded. – Marnik Bruyndonckx Aug 09 '15 at 00:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86529/discussion-between-shehary-and-marnik-bruyndonckx). – Shehary Aug 09 '15 at 00:06
  • @MarnikBruyndonckx NEVER (and I really mean it) rely on a frontend (javascript/html/css) validation. Someone could just make a request to your server without even using a browser. Fronted validation should be used just for additional "user friendliness" – Jhuliano Moreno Aug 09 '15 at 01:32
  • @JhulianoMoreno Thank you for the information, I fully understand but this is just for a school project and is all about frontend web-development :) – Marnik Bruyndonckx Aug 09 '15 at 11:07

0 Answers0