2016-03-30 13 views
-2

Ich muss überprüfen, ob es eine Internetverbindung gibt, bevor ich die E-Mail tatsächlich sende. Alles funktioniert gut, bis ich diesen Code hinzufügen und Funktion:Was ist hier falsch? (Neuling)

if(doesConnectionExist()) 
{ 
    $errors .= "\n No internet connection!"; 
} 

und diese Funktion:

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

Der gesamte Code wie folgt aussieht:

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

Wenn jemand könnte mir helfen, wäre absolut perfekt! Vielen Dank im Voraus.

+1

Sie verpasst() in der Funktion "doesConnectionExist". –

+3

Sie scheinen JavaScript und PHP zu mischen. Sie sind zwei völlig verschiedene Sprachen. – David

+0

Oeps! Das ist natürlich ein Hauptproblem .... Danke für Ihre Antwort – Erwin

Antwort

0

Ihre doesConnectionExist ist Javascript, PHP nicht =)

Sie diesen Beitrag verwenden können, um den Server pingen und Internetverbindung überprüfen:

StackOverflow : Ping IP addresses

EDIT:

<?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('http://www.?????.com/somefile.png')) 
{ 
    $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($host) 
{ 
    exec("ping -c 4 " . $host, $output, $result); 

    if ($result == 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
?> 
+0

Vielen Dank ThinkTank für Ihre Antwort. Wie implementiere ich das in meinem Code? Ist es möglich, dass Sie es eingeben? Danke – Erwin

+0

Bearbeiten mit Code! – ThinkTank