0

EDIT: My apologies, I comepletely forgot to add the HTML. I have included it below.

I'm new to HTML and I am currently attempting to incorporate a "Contact" form into a Weebly generated web page. The HTML seems to register the click alright, but there seems to be something wrong with the JavaScript and PHP execution. When the form is submitted, one is simply directed to a blank PHP page. This is also the case when JavaScript is supposed to kick in if not all the entries have been completed. My JavaScript is below:

Note: My JS is in the header.

<script>
    function has_id(id) {
        try {
            var tmp = document.getElementById(id).value;
        } catch (e) {
            return false;
        }
        return true;
    }

    function has_name(nm) {
        try {
            var tmp = cfrm.nm.type;
        } catch (e) {
            return false;
        }
        return true;
    }

    function $$(id) {
        if (!has_id(id) && !has_name(id)) {
            alert("Field " + id + " does not exist!\n Form validation configuration error.");
            return false;
        }
        if (has_id(id)) {
            return document.getElementById(id).value;
        } else {
            return;
        }
    }

    function $val(id) {
        return document.getElementById(id);
    }

    function trim(id) {
        $val(id).value = $val(id).value.replace(/^\s+/, '').replace(/\s+$/, '');
    }

    var required = {
        field: [],
        add: function(name, type, mess) {
            this.field[this.field.length] = [name, type, mess];
        },
        out: function() {
            return this.field;
        },
        clear: function() {
            this.field = [];
        }
    };
    var validate = {
        check: function(cform) {
            var error_message = 'Please fix the following errors:\n\n';
            var mess_part = '';
            var to_focus = '';
            var tmp = true;
            for (var i = 0; i < required.field.length; i++) {
                if (this.checkit(required.field[i][0], required.field[i][1], cform)) {} else {
                    error_message = error_message + required.field[i][2] + ' must be supplied\n';
                    if (has_id(required.field[i][0]) && to_focus.length === 0) {
                        to_focus = required.field[i][0];
                    }
                    tmp = false;
                }
            }

            if (!tmp) {
                alert(error_message);
            }
            if (to_focus.length > 0) {
                document.getElementById(to_focus).focus();
            }
            return tmp;
        },
        checkit: function(cvalue, ctype, cform) {
            if (ctype == "NOT_EMPTY") {
                if (this.trim($$(cvalue)).length < 1) {
                    return false;
                } else {
                    return true;
                }
            } else if (ctype == "EMAIL") {
                exp = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
                if ($$(cvalue).match(exp) == null) {
                    return false;
                } else {
                    return true;
                }
            }
        },
        trim: function(s) {
            if (s.length > 0) {
                return s.replace(/^\s+/, '').replace(/\s+$/, '');
            } else {
                return s;
            }
        }
    };

    required.add('Email_Address', 'EMAIL', 'Email Address');
    required.add('Your_Message', 'NOT_EMPTY', 'Your Message');
</script>

My HTML (sorry for it not being there earlier :/)

<form enctype="multipart/form-data" name="contactform" method="post" action="contactformprocess.php" onsubmit="return validate.check(this)">
    <div id="466589977852666939-form-parent" class="wsite-form-container" style="margin-top:10px;">
        <ul class="formlist" id="466589977852666939-form-list">
            <h2 class="wsite-content-title" style="text-align:left;">SEND US AN EMAIL</h2>

            <div>
                <div class="wsite-form-field" style="margin:5px 0px 5px 0px;">
                    <label class="wsite-form-label" for="Full_Name">Name </label>
                    <div class="wsite-form-input-container">
                        <input id="Full_Name" class="wsite-form-input wsite-input wsite-input-width-370px" type="text" name="Full_Name" maxlength="80" />
                    </div>
                    <div id="instructions-270584481949385592" class="wsite-form-instructions" style="display:none;"></div>
                </div>
            </div>

            <div>
                <div class="wsite-form-field" style="margin:5px 0px 5px 0px;">
                    <label class="wsite-form-label" for="Email_Address">Email <span class="form-required">*</span></label>
                    <div class="wsite-form-input-container">
                        <input id="Email_Address" class="wsite-form-input wsite-input wsite-input-width-370px" type="text" name="Email_Address" maxlength="100" />
                    </div>
                    <div id="instructions-270584481949385592" class="wsite-form-instructions" style="display:none;"></div>
                </div>
            </div>

            <div>
                <div class="wsite-form-field" style="margin:5px 0px 5px 0px;">
                    <label class="wsite-form-label" for="Your_Message">Your Message <span class="form-required">*</span></label>
                    <div class="wsite-form-input-container">
                        <textarea id="Your_Message" class="wsite-form-input wsite-input wsite-input-width-370px" name="Your_Message" style="height: 200px"></textarea>
                    </div>
                    <div id="instructions-137987539423347553" class="wsite-form-instructions" style="display:none;"></div>
                </div>
            </div>
        </ul>
    </div>
    <div style="display:none; visibility:hidden;">
        <input type="text" name="wsite_subject" />
    </div>
    <div style="text-align:left; margin-top:10px; margin-bottom:10px;">
        <input type='submit' style='position:absolute;top:0;left:-9999px;width:1px;height:1px' /><a class='wsite-button' onclick="document.contactform.submit(); return false"><span class='wsite-button-inner'>Submit</span></a>
    </div>
</form>

And my PHP which is in the same directory named contactformprocess.php.

<?php

if(isset($_POST['Email_Address'])) {

    $email_to = "foo@bar.com"; // your email address
    $email_subject = "Contact Form Message"; // email subject line
    $thankyou = "thankyou.htm"; // thank you page

    function died($error) {
        echo "Sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    if(!isset($_POST['Full_Name']) ||
        !isset($_POST['Email_Address']) ||
        !isset($_POST['Your_Message']) ||   
        ) {
        died('Sorry, there appears to be a problem with your form submission.');        
    }

    $full_name = $_POST['Full_Name']; // required
    $email_from = $_POST['Email_Address']; // required
    $comments = $_POST['Your_Message']; // required

    $error_message = "";

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(preg_match($email_exp,$email_from)==0) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\r\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Full Name: ".clean_string($full_name)."\r\n";
    $email_message .= "Email: ".clean_string($email_from)."\r\n";
    $email_message .= "Message: ".clean_string($comments)."\r\n";

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
header("Location: $thankyou");
?>
<script>location.replace('<?php echo $thankyou;?>')</script>
<?php
}
die();
?>

Thanks for your time, any input is appreciated.

Sushil
  • 2,837
  • 4
  • 21
  • 29
neelcor
  • 23
  • 3

0 Answers0