简体   繁体   中英

php contact form not processing

I've been trying to get this php contact form working and I don't understand whats wrong, any help would be greatly appreciated.

I tried change the name variable so its just the first user_name but it didn't change anything.

<?php
if( isset($_POST) ){

//form validation vars
$formok = true;
$errors = array();

//submission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');

//form data
$name = $_POST['user_name'] + ' ' + $_POST['user_name2'];    
$email = $_POST['user_email'];
$message = $_POST['user_message'];

//validate name is not empty
if(empty($name)){
    $formok = false;
    $errors[] = "You have not entered a name";
}

//validate email address is not empty
if(empty($email)){
    $formok = false;
    $errors[] = "You have not entered an email address";
//validate email address is valid
}elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
    $formok = false;
    $errors[] = "You have not entered a valid email address";
}

//validate message is not empty
if(empty($message)){
    $formok = false;
    $errors[] = "You have not entered a message";
}

//send email if all is ok
if($formok){
    $headers = "From: urbnfamily@urbntechnology.com" . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    $emailbody = "<p>You have received a new message from the contact us form on your website.</p>
                  <p><strong>Name: </strong> {$name} </p>
                  <p><strong>Email Address: </strong> {$email} </p>
                  <p><strong>Message: </strong> {$message} </p>
                  <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";

    mail("Urbnfamily@gmail.com","New Enquiry",$emailbody,$headers);

}

//what we need to return back to our form
$returndata = array(
    'posted_form_data' => array(
        'name' => $name,
        'email' => $email,
        'telephone' => $telephone,
        'enquiry' => $enquiry,
        'message' => $message
    ),
    'form_ok' => $formok,
    'errors' => $errors
);

//if this is not an ajax request
if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){

//set session variables
session_start();
$_SESSION['cf_returndata'] = $returndata;

//redirect back to form
header('location: ' . $_SERVER['HTTP_REFERER']);

}
}
?>

html code

<form method="POST" name="contactus" action="process.php" enctype="text/plain">
                        <div>
                            <span><input name="user_name" type="text" class="textbox right" value="first Name" onfocus="if(this.value == 'first Name') this.value='';" onblur="if(this.value == '') this.value='first Name';"></span>
                        </div>
                        <div>
                            <span><input name="user_name2" type="text" class="active textbox" value="last Name" onfocus="if(this.value == 'last Name') this.value='';" onblur="if(this.value == '') this.value='last Name';"></span>
                        </div>
                         <div class="clear"></div>
                        <div>
                            <span><input name="user_email" type="text" class="textbox right" value="Email Address" onfocus="if(this.value == 'Email Address') this.value='';" onblur="if(this.value == '') this.value='Email Address';"></span>
                        </div>

                        <div class="clear"></div>
                        <div>
                            <span><textarea name="user_message" rows="2" cols="70" onfocus="if(this.value == 'Your Message') this.value='';" onblur="if(this.value == '') this.value='Your Message';">Your Message</textarea></span>
                        </div>
                       <div>
                            <span><input type="submit" name="send" class="" value="Submit"></span>
                      </div>
                    </form>

I tried it on my system and it comes back with unknown indexes for each of the POST variables. Comment out the header redirect and enable your error messages to see what I mean. It looks like the enctype="text/plain" is not being handled in php? Check out this other post from stackoverflow which is related. When I removed the enctype attribute, the variables were then being passed fine, although there were a number of missing inputs I assume is from incomplete code posted here? Remember to turn error reporting back on when in production mode. Here is a link to the php error reporting manual .

Update: More info on what I changed: Added print_r($_POST); to confirm nothing in post being transferred

<?php

if( isset($_POST) ){ print_r($_POST);

Also, commented out the return header to see the errors being reported.

//redirect back to form
//header('location: ' . $_SERVER['HTTP_REFERER']);

Here are the errors I receive after doing this:

Array ( ) 
Notice: Undefined index: user_name in /var/www/html/dev/test/test.php on line 15

Notice: Undefined index: user_name2 in /var/www/html/dev/test/test.php on line 15

Notice: Undefined index: user_email in /var/www/html/dev/test/test.php on line 16

You may also wish to give yourself or the users some feedback if certain variables aren't set, such as:

if( isset($_POST) ){

Even something like } else { echo 'No Post Variables';}; although, it is better to put this stuff in an error log somewhere, the user should be informed that you got a message and they should check back later. Just sending them back to the submission form with no info on whether the message was sent successfully leaves a bad taste in one's mouth.

Its working now

I removed the enctype="text/plain" from the form.html and I changed the php code from

$name = $_POST['user_name'] + ' ' + $_POST['user_name2'];    
$email = $_POST['user_email'];

to

$name = $_POST['user_name'];
$name2 = $_POST['user_name2'];    
$email = $_POST['user_email'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM