弹出窗口和 PHP 表单

新手上路,请多包涵

我创建了一个联系页面和一个单独的 PHP 页面来接收发布的数据。我想让 PHP 在弹出窗口中打开。我已经尝试过在线方法但没有成功,我可以使弹出窗口出现但我不能让 PHP 发送数据。

 <!------Contact Page------->

<form method='post' action='sendemail.php' >
    <label>Name</label>
    <input name="name" placeholder="Type Here">
    <label>Email</label>
    <input name="email" placeholder="Type Here" id="email">
    <label>Message</label>
    <textarea name="message" placeholder="Type Here"></textarea>

    <label>Human Verification</label>
      <input name="human" placeholder="2 + 2 = ? " id="human">
      <input id="submit" name="submit" type="submit" value="Submit">
    </label>
</form>

 <?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $email;
    $to = 'my@email.com';
    $subject = 'New Message';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if ($name != '' && $email != '') {
            if ($human == '4') {
                if (mail ($to, $subject, $body, $from)) {
                    echo '<h4>Your message has been sent!</h4>';
            } else {
                echo '<h4>Something went wrong, go back and try again!</h4>';
            }
        } else if ($_POST['submit'] && $human != '4') {
            echo '<h4>You answered the anti-spam question incorrectly!</h4>';
            }
        } else {
            echo '<h4>You need to fill in all required fields!!</h4>';
        }
    }
?>

原文由 user2999825 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 211
1 个回答

特别是对于表单之类的东西,我会确保它不依赖 JavaScript,以防用户在浏览器中关闭 JavaScript。

为什么不保持简单,将所有内容都放在同一个页面上,在表单上方显示错误消息,然后用 PHP 重新填充字段?

例子:

 <?php

// Check if coming from a POST
if ($_SERVER['REQUEST_METHOD']=='POST' && isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $email;
    $to = 'you@youremail.com';
    $subject = 'New Message';
    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {
        if ($name != '' && $email != '') {
            if ($human == '4') {
                if (mail ($to, $subject, $body, $from)) {
                    echo '<h4>Your message has been sent!</h4>';
                } else {
                    echo '<h4>Something went wrong, go back and try again!</h4>';
                } //endif
            } else if ($_POST['submit'] && $human != '4') {
                echo '<h4>You answered the anti-spam question incorrectly!</h4>';
            }
            } else {
                echo '<h4>You need to fill in all required fields!!</h4>';
            } //endif
        } //endif

} //endif

?>
<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>' >

    <label>Name</label>
    <input name="name" placeholder="Type Here" value="<?php if (isset($_POST['name'])) { echo $name;} ?>" >
    <label>Email</label>
    <input name="email" placeholder="Type Here" id="email" value="<?php if (isset($_POST['email'])) { echo $email;} ?>">
    <label>Message</label>
    <textarea name="message" placeholder="Type Here"><?php if (isset($_POST['message'])) { echo $message;} ?></textarea>

    <label>Human Verification</label>
        <input name="human" placeholder="2 + 2 = ? " id="human" value="<?php if (isset($_POST['human'])) { echo $human;} ?>">
        <input id="submit" name="submit" type="submit" value="Submit">
    </label>
</form>

原文由 happypete 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题