繁体   English   中英

表单提交按钮中的 PHP 脚本

[英]PHP script in form submit button

我不仅仅是新手,尝试了很多,但无法让它工作。

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

这里是表单的代码:

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

我真的希望有人可以将这两个“捆绑”在一起。

.

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM