PHP脚本在表单提交按钮

时间:2016-03-28 16:03:25

标签: php

我不仅仅是新手并且尝试过很多但却无法让它发挥作用。

点击提交按钮时,我需要检查互联网连接。 如果有互联网连接,则必须发送电子邮件,否则我需要显示一条消息:"没有互联网连接,请稍后重试"。

这里是表单的代码:

<?php header('Content-type: text/html; charset=iso8859-15');
    $your_email = '????@????.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 (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');
        }
    } ?>

<html>
<head>
    <script language="JavaScript"
        src="scripts/gen_validatorv31.js" type="text/javascript"></script>
</head>
<body>

<?php if (!empty($errors)) {
    echo "<p class='err'>" . nl2br($errors) . "</p>";
} ?>
<div
  id='contact_form_errorloc' class='err'></div>
<form method="POST"
      name="booktransfer" action="<?php echo
htmlentities($_SERVER['PHP_SELF']); ?>"><label
      for='firstname'>Firstname:</label> <input type="text"
                                                name="firstname" value='<?php echo htmlentities($firstname) ?>'>
    <br><label for='lastname'>Lastname:</label> <input type="text" name="lastname"
                                                       value='<?php echo htmlentities($lastname) ?>'>
    <br><label for='email'>Email:</label> <input type="text"
                                                 name="email" value='<?php echo htmlentities($visitor_email) ?>'>
    <input type="submit" value="Senden" name='submit'>
</form>
</body> </html>

这里是检查互联网连接的脚本:

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

我真的希望有人可以&#34;领带&#34;这两个在一起。

1 个答案:

答案 0 :(得分:1)

好吧,如果您的脚本正常运行,您可以使用以下内容:

<form method="POST"
name="booktransfer" action="<?php echo
htmlentities($_SERVER['PHP_SELF']); ?>" onsubmit="doesConnectionExist()">