简体   繁体   English

PHP的联系表格不处理

[英]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. 我一直在尝试使此php联系人表单正常工作,但我不明白发生了什么问题,我们将不胜感激。

I tried change the name variable so its just the first user_name but it didn't change anything. 我尝试更改名称变量,使其仅是第一个user_name,但它没有进行任何更改。

<?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 HTML代码

<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. 我在系统上进行了尝试,并为每个POST变量返回了未知的索引。 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? 看起来enctype =“ text / plain”没有在php中处理? Check out this other post from stackoverflow which is related. 从stackoverflow查看与此相关的其他帖子 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? 当我删除enctype属性时,这些变量然后可以正常传递,尽管我认为有很多缺少的输入是由于此处发布的不完整代码造成的? Remember to turn error reporting back on when in production mode. 切记在生产模式下重新打开错误报告。 Here is a link to the php error reporting manual . 这是php错误报告手册的链接。

Update: More info on what I changed: Added print_r($_POST); 更新:有关我所做更改的更多信息:添加了print_r($ _ POST); to confirm nothing in post being transferred 确认转帐后没有任何东西

<?php

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

Also, commented out the return header to see the errors being reported. 另外,注释掉return标头以查看所报告的错误。

//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';}; 甚至像} else {之类的东西,都回显'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 我从form.html中删除了enctype =“ text / plain”,并且我将php代码从

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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