我需要在实际发送电子邮件之前检查是否有互联网连接。这里有什么问题? (新手)
if(doesConnectionExist())
{
$errors .= "\n No internet connection!";
}
加上这个功能:
function doesConnectionExist
{
var xhr = new XMLHttpRequest();
var file = "http://www.?????.com/somefile.png";
var randomNum = Math.round(Math.random() * 10000);
xhr.open('HEAD', file + "?rand=" + randomNum, false);
try {
xhr.send();
if (xhr.status >= 200 && xhr.status < 304) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
总代码如下所示:直到我加入这样的代码和功能一切正常
<?php
header ('Content-type: text/html; charset=iso8859-15');
$your_email ='[email protected]????.com';
session_start();
$errors = '';
$firstname = ' ';
$lastname = '';
$visitor_email = '';
if(isset($_POST['submit']))
{
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$visitor_email = $_POST['email'];
if(empty($firstname)||empty($lastname)
{
$errors .= "\n firstname and lastname are required fields. ";
}
if(doesConnectionExist())
{
$errors .= "\n No internet connection!";
}
if(empty($errors))
{
$to = $your_email;
$subject="test";
$from = $your_email;
$body = "test\n".
"Firstname: $firstname\n".
"Lastname: $lastname \n".
$headers = "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body, $headers);
header('Location: thankyou.html');
}
}
function doesConnectionExist
{
var xhr = new XMLHttpRequest();
var file = "http://www.?????.com/somefile.png";
var randomNum = Math.round(Math.random() * 10000);
xhr.open('HEAD', file + "?rand=" + randomNum, false);
try {
xhr.send();
if (xhr.status >= 200 && xhr.status < 304) {
return true;
} else {
return false;
}
} catch (e) {
return false;
}
}
?>
如果有人可以帮助我将绝对好! 预先感谢您。
你错过()在函数“doesConnectionExist”中。 –
您似乎在混合使用JavaScript和PHP。他们是两种完全不同的语言。 – David
Oeps! 这当然是一个主要问题.... 感谢您的回答 – Erwin