Page 1 of 1

Php contact problem

Posted: Wed Mar 03, 2010 12:00 am
by ductiletoaster
ok basically i am developing a contact form using JavaScript validation and PHP. the for works kinda! it sends messages but for some reason its not getting any info from the site using $_POST.
here is the code

PHP:

Code: Select all

 
<?php
 
    // Your Email
    $to = "brian@lostpropaganda.net";
    $subject = "Lost Propaganda Inquiry";
    $name_field = $_POST['username'];
    $email_field = $_POST['email'];
    $message = $_POST['message'];
    $footer = "----------------------------------------------------------";
    $statement = "This was a form submission from lostpropaganda.net";
    
    
    $header = "From: $email_field";
    $body = "From: $name_field\nE-Mail: $email_field\nMessage:\n$message\n\n\n\n$footer\n$statement";
    
    // Thank you message
    echo("<p class='redtxt'>Thank you for contacting us! We have received your message.</p>");
    
    // Sends email
    mail($to, $subject, $body, $header);
 
?>
 
Javascript:

Code: Select all

 
function validateForm(dataSource, divID) {
        
        // Instance Variables 
        var username = document.getElementById("username").value; // Gets Username from Html form
        var email = document.getElementById("email").value; // Gets Email address from Html form
        var message = document.getElementById("message").value; // Gets Message from Html form
        var security = document.getElementById("security_q").value; // Gets Security Answer from Html form
        
        // Error Variables
        var count = 0;
        var obj = document.getElementById(divID); // Gets targetDiv
        var tempTXT = "<p class='redtxt'>Errors!</p>"; // Holding String for XML elements
        
        // Error Check
        if (username == "") {
            tempTXT = tempTXT + "<p class='redtxt'>Missing Required Field Name</p>";
            count = 1;
        }
        if (email == "") {
            tempTXT = tempTXT + "<p class='redtxt'>Missing Required Field Email</p>";
            count = 1;
        }
        if (message == "") {
            tempTXT = tempTXT + "<p class='redtxt'>Missing Required Field Message</p>";
            count = 1;
        }
        if (security != 3) {
            tempTXT = tempTXT + "<p class='redtxt>Invalid Security Code</p>";
            count = 1;
        }
        
        // Error Print
        if (count == 1) { // If Error
            obj.innerHTML = tempTXT;
        } else { // Else send message
            
            // Clears any errors that were listed
            tempTXT = "";
            obj.innerHTML = tempTXT;
            
            // Calls Function to make HTTP request
            MakeRequest(dataSource); 
        }
        
        return false;
    }
    
    function MakeRequest(dataSource) {
        
        // Instance Variables
        var xmlHttp = getXMLHttp();
        
        xmlHttp.onreadystatechange = function() {
            if(xmlHttp.readyState == 4) {
                HandleResponse(xmlHttp.responseText);
            }
        }
        
        xmlHttp.open("GET", dataSource, true);
        xmlHttp.send(null);
 
    }
    
    function getXMLHttp() {
        
        var xmlHttp;
        
        try {
            //Firefox, Opera 8.0+, Safari
            xmlHttp = new XMLHttpRequest();
        } 
        catch(e) {
            //Internet Explorer
            try {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch(e) {
                try {
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch(e) {
                    alert("Your browser does not support AJAX!")
                    return false;
                }
            }
         }
         return xmlHttp;
    }
    
    function HandleResponse(response) {
        document.getElementById('targetDiv').innerHTML = response;
    }
 
HTML form

Code: Select all

 
            <form method="post" id="contactform" name="contactform" action="" onSubmit="return validateForm('library/scripts/php/mailer.php', 'targetDiv');">
            
                <div class="form_entry_input">
                    <p><label>Name</label></p>
                    <p><input type="text" id="username" name="username"/></p>
                </div>
                
                <div class="form_entry_input">
                    <p><label>Email</label></p>
                    <p><input type="text" id="email" name="email"/></p>
                </div>
                
                <div class="form_entry_input">
                    <p><label>Message</label></p>
                    <p><textarea rows="9" id="message" name="message"></textarea></p>
                </div>
                
                <div class="form_entry_input">
                    <label class="security">2 + 1 = </label>
                    <input type="text" id="security_q" class="security_q"/>
                </div>
                
                <button type="submit" name="submit">Send Email</button>
                
            </form>
            
            <div id="targetDiv"></div>
 

Re: Php contact problem

Posted: Wed Mar 03, 2010 12:02 am
by jraede
Looks like it should work to me...are you getting any errors, or is it just not sending the correct info?

Re: Php contact problem

Posted: Wed Mar 03, 2010 12:07 am
by ductiletoaster
# $name_field = $_POST['username'];
# $email_field = $_POST['email'];
# $message = $_POST['message'];

these lines i think are the problem
for what ever reason... the info that should be sent from these three deals isnt sending

whats wierd is i have used this contact form for another site i made without the validation

like i said though i am getting emails from the form but the info from the above code is not being sent

Re: Php contact problem

Posted: Wed Mar 03, 2010 1:19 am
by jraede
If it worked on another site without the validation, then the problem is probably with your JavaScript validation script. I don't have time to look it over, but I'm 99% sure that's where your problem is.

Re: Php contact problem

Posted: Wed Mar 03, 2010 1:59 am
by ductiletoaster
I dont see how it could be the JavaScript validation cuz why would everything else work with the php contact form

Re: Php contact problem

Posted: Wed Mar 03, 2010 2:18 am
by ductiletoaster
Ok so i did some testing.... and it totally is the validation script....now to figure whats goin wrong

Re: Php contact problem

Posted: Wed Mar 03, 2010 10:33 am
by Kurby
Ugh... I just realized you posted this twice.

Here is your other one that I already responded to: viewtopic.php?f=1&t=113634

Don't spam though :(