weixin_39422902
2017-12-25 09:35求高手把本段php译成C#
40<?php
$isPartial = empty($_POST['partial']) ? false : true;
// A preset set of messages
$messages = array(
'required' => 'The field %s is required',
'invalid' => 'The field %s is invalid',
'errors' => 'Please fix the errors in the form to continue',
'generic' => 'An error has occurred and your message has not been delivered. Try later %s',
'short' => 'The value of the field %s is too short. It must have at least %d characters',
'success' => 'Thank you for your message %s. It has been successfully sent'
);
// The result of the request
$result = array(
'status' => '',
'message' => '',
'info' => []
);
// Check Full name
if (!$isPartial && $_POST['name'] === '') {
$result['info'][] = array(
'field' => 'name',
'message' => sprintf($messages['required'], 'Full name')
);
} else if ((!$isPartial || isset($_POST['name'])) && strlen($_POST['name']) <= 3) {
$result['info'][] = array(
'field' => 'name',
'message' => sprintf($messages['short'], 'Full name', 4)
);
}
// Check Email
if (!$isPartial && $_POST['email'] === '') {
$result['info'][] = array(
'field' => 'email',
'message' => sprintf($messages['required'], 'Email')
);
} else if ((!$isPartial || isset($_POST['email'])) && !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$result['info'][] = array(
'field' => 'email',
'message' => sprintf($messages['invalid'], 'Email')
);
}
// Check Subject
if (!$isPartial && $_POST['subject'] === '') {
$result['info'][] = array(
'field' => 'subject',
'message' => sprintf($messages['required'], 'Subject')
);
} else if ((!$isPartial || isset($_POST['subject'])) && strlen($_POST['subject']) <= 3) {
$result['info'][] = array(
'field' => 'subject',
'message' => sprintf($messages['short'], 'Subject', 4)
);
}
// Check Message
if (!$isPartial && $_POST['message'] === '') {
$result['info'][] = array(
'field' => 'message',
'message' => sprintf($messages['required'], 'Message')
);
} else if ((!$isPartial || isset($_POST['message'])) && strlen($_POST['message']) <= 3) {
$result['info'][] = array(
'field' => 'message',
'message' => sprintf($messages['short'], 'Message', 4)
);
}
if (!empty($result['info'])) {
$result['status'] = 'error';
$result['message'] = $messages['errors'];
} else {
/*
* If you have a SMTP server on your computer uncomment
* these lines and remove the `if(true) {` line.
*
*
* if (mail(
* 'youremail@domain.com',
* htmlentities($_POST['subject']),
* htmlentities($_POST['message']),
* 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>'
* )) {
*/
if (true) {
$result['status'] = 'success';
if ($isPartial) {
$result['message'] = '';
} else {
// Sorry if the htmlentities function sounds unfamiliar to you,
// but as a programmer I just can leave this statement subject to XSS attacks
$result['message'] = sprintf($messages['success'], htmlentities($_POST['name']));
}
} else {
$result['status'] = 'error';
$result['message'] = sprintf($messages['generic'], htmlentities($_POST['name']));
}
}
echo json_encode($result);
- 点赞
- 回答
- 收藏
- 复制链接分享
4条回答
为你推荐
- C#使用正则表达式,有没有一个方法能把符合条件的部分替换成序数
- c#
- visual studio
- 2个回答
- c#窗体程序如何备份,即保存多个版本?
- c#
- visual studio
- 2个回答
- C#控制台应用程序,求孪生素数的?
- 应用
- 控制台
- c#
- 1个回答
- c#渣渣菜鸟,跪求问题
- 跪求问题
- c#
- 6个回答
- c#中求1到5阶乘和的问题
- c#
- 2个回答
换一换