简体   繁体   English

验证PHP中的联系表单

[英]Validating contact form in PHP

I have a contact form which I need validating in PHP to check if each field is correctly filled. 我有一个联系表格,需要在PHP中进行验证,以检查每个字段是否正确填写。

Here is what I have: 这是我所拥有的:

//Post fields
<?php
$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_services = $_POST['services'];
$field_it = $_POST['it'];
$field_location = $_POST['location'];
$field_message = $_POST['message'];


//mail_to omitted 


//Validation of contact form

$errormessage = '';
if($field_name == ''){

$errormessage += 'You have not entered your Name\n';
}

if($field_email == ''){

$errormessage += 'You have not entered your Email Address\n';
}

if($field_services == ''){

$errormessage += 'You have not chosen the service you require\n';
}

if($field_it == ''){

$errormessage += 'You have not chosen the Date of your event\n';
}

if($field_location == ''){

$errormessage += 'You have not entered the location of your event\n';
}


if($errormessage != ''){ ?>

<script language="javascript" type="text/javascript">
    alert('The following fields have not neen entered correctly\n<?php echo "$errormessage" ?>');
    window.location = 'contact.html';
</script>
<?php } 



if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
    alert('Thank you for the message. We will contact you shortly.');
    window.location = 'contact.html';
</script>
<?php
}


else { ?>
<script language="javascript" type="text/javascript">
    alert('Message failed. Please, send an email to s_ejaz@live.co.uk');
    window.location = 'contact.html';
</script>
<?php
}
?>

This does nothing when I try to submit an empty contact form, it should alert the user of the particular fields which were not filled but it doesn't. 当我尝试提交一个空的联系表单时,这没有任何作用,它应该警告用户未填写但没有填写的特定字段。 It just takes me to a blank white page. 它只带我到空白页。

Could anyone help me find where I am going wrong? 谁能帮我找到我要去的地方吗?

另外,您可以使用修剪功能删除任何空格。

trim($_POST['name'])...

You should use strlen() and isset() to check if any data was received from the form. 您应该使用strlen()isset()来检查是否从表单接收到任何数据。

Example: 例:

if(!isset($_POST['name']) || strlen($_POST['name']) < 1){
    $errormessage .= 'You have not entered your Name\n';
}

Instead of comparing variable to empty string like this $field_services == '' , use empty() or isset() 而不是像$field_services == ''这样将变量与空字符串进行比较,请使用empty()isset()

if(!empty($field_services)) or if(isset($field_services)) if(!empty($field_services))if(isset($field_services))

An other problem is that you concatenate strings using + , this is true if you are using javascript , java or C# etc.. not PHP . 另一个问题是您使用+连接字符串,如果使用的是javascriptjavaC#等,则为true。不是PHP

To concatenate variables using PHP: 要使用PHP连接变量:

 $var='Hello';
 $var.=' World !'

 echo $var;// Hello World !

So your code should be : 所以你的代码应该是:

 if(empty($_POST['name'])){
   $errormessage .= 'You have not entered your Name\n';
}

Try to use $errormessage.='Some text\\n'; 尝试使用$errormessage.='Some text\\n'; instead of $errormessage+='Some text\\n'; 而不是$errormessage+='Some text\\n'; .
Using " + " instead of " . ", PHP treats the variable $errormessage as a number, and the assertion failed. 使用“ + ”代替“ . ”,PHP将变量$errormessage视为数字,并且断言失败。

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

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