简体   繁体   中英

PHP script in form submit button

I am more than new and tried a lot but can't get it to work.

I need to have a check for internet-connection when the submit button is clicked. If there is Internet-connection the email must be send, if not I need a message to appear that says: "No internet-connection please try again later".

Here the code for the form:

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

And here the script for checking the Internet-connection:

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

I really hope somebody can "tie" these 2 together.

.

Well, if your script is working you can use the below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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