简体   繁体   English

没有ajax的联系表格只是php和html页面

[英]Contact Form without ajax just php and html page

I have a simple Contact Form it works very freat but I want the result on the same page in a small box just under the form where the fail messages are coming and success too我有一个简单的联系表,它工作得非常好,但我希望结果在同一页面上的一个小框中,就在失败消息即将到来和成功的表单下方

<form method="post" action="contact.php" name="contactform" id="contactform">
    <div class="one_half">
        <input name="name" type="text" id="name" size="30" onfocus="if(this.value == 'Name') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Name'; }" value="Name">
        <input name="alter" type="text" id="alter" size="3" onfocus="if(this.value == 'Alter') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Alter'; }" value="Alter">
        <input name="email" type="text" id="email" size="30" onfocus="if(this.value == 'E-Mail') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'E-Mail'; }" value="E-Mail">
        <input name="phone" type="text" id="phone" size="30" onfocus="if(this.value == 'Handynummer') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Handynummer'; }" value="Handynummer">
        <input name="facebook" type="text" id="facebook" size="200" onfocus="if(this.value == 'Facebook') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Facebook'; }" value="Facebook">
        <input name="instagram" type="text" id="instagram" size="200" onfocus="if(this.value == 'Instagram') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Instagram'; }" value="Instagram">
    </div>
    <div class="one_half last">
        <textarea name="comments" cols="40" rows="3" id="comments" onfocus="if(this.value == 'Nachricht') { this.value = ''; }" onblur="if(this.value == '') { this.value = 'Nachricht'; }">Nachricht</textarea>
    </div>
    <input type="submit" class="send_message" id="submit" value="Senden"/>
</form>

and here the PHP file这里是 PHP 文件

<?php
    if (!$_POST) exit;

    if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $comments = $_POST['comments'];
    $alter = $_POST['alter'];
    $facebook = $_POST['facebook'];
    $instagram = $_POST['instagram'];

    if (get_magic_quotes_gpc()) {
        $comments = stripslashes($comments);
    }

    $address = "xxx@xxx.de";
    $e_subject = 'Contact from ' . $name;
    $e_body = "von: $name" . PHP_EOL . PHP_EOL;
    $e_reply = "Alter: $alter\r\nE-mail: $email\r\nHandynummer: $phone";
    $e_content = "Nachricht:\r\n$comments" . PHP_EOL . PHP_EOL;
    $e_links = "Facebook:\r\n$facebook\r\nInstagram:\r\n$instagram" . PHP_EOL . PHP_EOL;

    $msg = wordwrap($e_body . $e_links . $e_content . $e_reply, 70);

    $headers = "From: $email" . PHP_EOL;
    $headers .= "Reply-To: $email" . PHP_EOL;
    $headers .= "MIME-Version: 1.0" . PHP_EOL;
    $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
    $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

    if (mail($address, $e_subject, $msg, $headers)) {

        // Email has sent successfully, echo a success page.

        echo "<fieldset>";
        echo "<div id='success_page'>";
        echo "<h1>Bewerbung erfolgreich</h1>";
        echo "<p>Danke <strong>$name</strong>, deine Bewerbung wurde erfolgreich an uns gesendet</p>";
        echo "</div>";
        echo "</fieldset>";

    } else {
        echo 'FEHLER!';
    }

It works PERFECT它工作完美

but it's always getting on a new site, no matter if error or success.但它总是进入一个新站点,无论是错误还是成功。

I want to add a small block under the form where all results are displayed我想在显示所有结果的表单下添加一个小块

I hope u can help me.我希望你能帮助我。

I don't want ajax or something else.我不想要 ajax 或其他东西。 I just want it added like it is.我只是希望它按原样添加。

A solution for your problem might be to have in the same PHP file the logic with the form rendering.您的问题的解决方案可能是在同一个 PHP 文件中包含表单渲染的逻辑。 Doing this, the form action will be the same PHP file, so it will load the PHP code before rendering the form view.这样做,表单操作将是同一个 PHP 文件,因此它将在呈现表单视图之前加载 PHP 代码。 By that, you'll be able to render bellow the form whatever you want to according with the output from the sending email.这样,您就可以根据发送电子邮件的输出呈现您想要的任何形式的表单。

For example, take a closer look at the $mailResult variable:例如,仔细看看$mailResult变量:

<?php

$name = $_POST['name'] ?? null;
$email = $_POST['email'] ?? null;
$phone = $_POST['phone'] ?? null;
$comments = $_POST['comments'] ?? null;
$alter = $_POST['alter'] ?? null;
$facebook = $_POST['facebook'] ?? null;
$instagram = $_POST['instagram'] ?? null;

if (get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);
}

$address = "xxx@xxx.de";
$e_subject = 'Contact from ' . $name;
$e_body = "von: $name" . PHP_EOL . PHP_EOL;
$e_reply = "Alter: $alter\r\nE-mail: $email\r\nHandynummer: $phone";
$e_content = "Nachricht:\r\n$comments" . PHP_EOL . PHP_EOL;
$e_links = "Facebook:\r\n$facebook\r\nInstagram:\r\n$instagram" . PHP_EOL . PHP_EOL;
$msg = wordwrap($e_body . $e_links . $e_content . $e_reply, 70);

$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
$mailResult = '';

if (isset($name, $email)) {
    if (mail($address, $e_subject, $msg, $headers)) {
        $mailResult = "<fieldset>";
        $mailResult .= "<div id='success_page'>";
        $mailResult .= "<h1>Bewerbung erfolgreich</h1>";
        $mailResult .= "<p>Danke <strong>$name</strong>, deine Bewerbung wurde erfolgreich an uns gesendet</p>";
        $mailResult .= "</div>";
        $mailResult .= "</fieldset>";
    } else {
        $mailResult .= 'FEHLER!';
    }
}
?>
<html lang="en">
<head>
    <title>Title page!</title>
</head>
<body>
<form method="post" name="contactform" id="contactform">
    <div class="one_half">
        <input name="name" type="text" id="name" size="30"
               onfocus="if(this.value === 'Name') { this.value = ''; }"
               onblur="if(this.value === '') { this.value = 'Name'; }"
               value="Name">
        <input name="alter" type="text" id="alter" size="3"
               onfocus="if(this.value === 'Alter') { this.value = ''; }"
               onblur="if(this.value === '') { this.value = 'Alter'; }"
               value="Alter">
        <input name="email" type="text" id="email" size="30"
               onfocus="if(this.value === 'E-Mail') { this.value = ''; }"
               onblur="if(this.value === '') { this.value = 'E-Mail'; }"
               value="E-Mail">
        <input name="phone" type="text" id="phone" size="30"
               onfocus="if(this.value === 'Handynummer') { this.value = ''; }"
               onblur="if(this.value === '') { this.value = 'Handynummer'; }"
               value="Handynummer">
        <input name="facebook" type="text" id="facebook" size="200"
               onfocus="if(this.value === 'Facebook') { this.value = ''; }"
               onblur="if(this.value === '') { this.value = 'Facebook'; }"
               value="Facebook">
        <input name="instagram" type="text" id="instagram" size="200"
               onfocus="if(this.value === 'Instagram') { this.value = ''; }"
               onblur="if(this.value === '') { this.value = 'Instagram'; }"
               value="Instagram">
    </div>
    <div class="one_half last">
        <textarea name="comments" cols="40" rows="3" id="comments"
                  onfocus="if(this.value === 'Nachricht') { this.value = ''; }"
                  onblur="if(this.value === '') { this.value = 'Nachricht'; }">
            Nachricht
        </textarea>
    </div>
    <input type="submit" class="send_message" id="submit" value="Senden"/>
</form>
<?php echo $mailResult ?>
</body>
</html>

I would anyway avoid this kind of solution.我无论如何都会避免这种解决方案。 I would approach this problem with AJAX.我会用 AJAX 来解决这个问题。 In these solutions, we are mixing the logic with the rendering.在这些解决方案中,我们将逻辑与渲染混合在一起。

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

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