在PHP中验证联系表单

在PHP中验证联系表单

问题描述:

I have a contact form which I need validating in PHP to check if each field is correctly filled.

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
';
}

if($field_email == ''){

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

if($field_services == ''){

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

if($field_it == ''){

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

if($field_location == ''){

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


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

<script language="javascript" type="text/javascript">
    alert('The following fields have not neen entered correctly
<?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?

我有一个联系表单,我需要在PHP中验证是否正确填写每个字段。 p>

以下是我的内容: p>

  //发布字段
&lt;?php 
 $ field_name = $ _POST ['name']; \  n $ field_email = $ _POST ['email']; 
 $ field_services = $ _POST ['services']; 
 $ field_it = $ _POST ['it']; 
 $ field_location = $ _POST ['location']  ; 
 $ field_message = $ _POST ['message']; 
 
 
 // mail_to省略
 
 
 //验证联系表格
 
 $ errormessage =''; 
if($  field_name ==''){
 
 $ errormessage + ='您还没有输入姓名
'; 
} 
 
if($ field_email ==''){
 
 $ errormessage + =  '你还没有输入你的电子邮件地址
'; 
} 
 
if($ field_services ==''){
 
 $ errormessage + ='你没有选择你需要的服务
'; \  n} 
 
if($ field_it ==''){
 
 $ errormessage + ='你没有选择事件的日期
'; 
} 
 
if($ field_location =='  '){
 
 $ errormessage + ='您还没有输入事件的位置
'; 
} 
 
 
 nif($ err  ormessage!=''){?&gt; 
 
&lt; script language =“javascript”type =“text / javascript”&gt; 
 alert('以下字段未正确输入
&lt;?php echo“  $ errormessage“?&gt;'); 
 window.location ='contact.html'; 
&lt; / script&gt; 
&lt;?php} 
 
 
 
if($ mail_status){?&gt; \  n&lt; script language =“javascript”type =“text / javascript”&gt; 
 alert('感谢您留言。 我们会尽快与您联系。'); 
 window.location ='contact.html'; 
&lt; / script&gt; 
&lt;?php 
} 
 
 
else {?&gt; 
&lt;脚本语言 =“javascript”type =“text / javascript”&gt; 
 alert('消息失败。请发送电子邮件至s_ejaz@live.co.uk'); 
 window.location ='contact.html'; \  n&lt; / script&gt; 
&lt;?php 
} 
?&gt; 
  code>  pre> 
 
 

当我尝试提交一个空的联系表单时,这没有任何作用,它应该 提醒用户未填写但未填写的特定字段。 它只是带我一个空白的白页。 p>

任何人都可以帮我找到我错的地方吗? p> div>

You should use strlen() and isset() to check if any data was received from the form.

Example:

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

Instead of comparing variable to empty string like this $field_services == '' , use empty() or isset()

if(!empty($field_services)) or 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.

To concatenate variables using 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
';
}

Try to use $errormessage.='Some text '; instead of $errormessage+='Some text ';.
Using "+" instead of ".", PHP treats the variable $errormessageas a number, and the assertion failed.

Also, you can use trim function to delete any space.

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