单击提交时,Bootstrap 和 PHP 联系表单显示空白页

杰斐逊 X 共济会

嗨,伙计们,我需要一些帮助。

当我尝试测试我的引导程序表单时,它显示一个白屏。这是我的代码。NB 我是 Web 开发的新手,#ExcuseMyFrenchThough

这是我的 index.html

<html>
<div class="container">
<div class="row">
      <div class="col-md-6 col-md-offset-3" id="offer">

            <h2 id="form"> LET'S WORK ? </h2>

            </div>

            <div class="col-md-6 col-md-offset-3">

            <form role="form" method="post" action="contact.php">


            <div class="form-group">
              <input type="text" class="form-control" placeholder="Enter Your Name">
                <?php echo "<p class='text-danger'>$errName</p>";?>
              </div>

              <div class="form-group">

               <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email">
               <?php echo "<p class='text-danger'>$errEmail</p>";?>
              </div>

              <div class="form-group">
              <textarea class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here"> </textarea>
              <?php echo "<p class='text-danger'>$errMessage</p>";?>

              </div>

              <div class="form-group">
              <button type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text">SEND MESSAGE</button>
              </div>

              <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                      <?php echo $result; ?>  
                   </div>
  </div>
            </form>
            </div>

</div>
</div>

PHP代码[CONTACT.PHP]

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        /*$human = intval($_POST['human']); */
        $from = 'Geofrey Zellah'; 
        $to = '[email protected]'; 
        $subject = 'Message from Geofrey Zellah ';

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

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }

        /*
        //Check if simple anti-bot test is correct
        if ($human !== 5) {
            $errHuman = 'Your anti-spam is incorrect';
        } */

// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage /*&& !$errHuman*/) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>

在我的页面上点击提交后我得到的屏幕截图,NB live not local
在此处输入图片说明

任何人 ?

优洛
  1. submit 变量永远不会在您的表单中设置。请注意,该按钮现在是具有提交名称属性的提交类型的输入。

  2. 您的其他表单变量也未设置。

  3. 你从来没有回应任何事情。所以我在你的最后一个条件中放了一个回声。

  4. 如果您希望表单在提交表单后显示,您需要将您的 index.html 重命名为 index.php 并在顶部包含 contact.php。见下文。

  5. 如果您只是简单地检查 $_POST 变量是否为真,PHP 将抛出 E_NOTICE 错误。所以最好将变量包装到isset()(就像这个变量集一样)函数中。见下文。

我重构以防止 E_NOTICE 错误并评论更改。

联系方式

<?php
if (isset($_POST["submit"])) {

    $error = [];

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    /*$human = intval($_POST['human']); */
    $from = 'Geofrey Zellah';
    $to = '[email protected]';
    $subject = 'Message from Geofrey Zellah ';

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

    // Check if name has been entered
    if (!isset($_POST['name']) || strlen($_POST['name']) === 0) {
        $error['name'] = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $error['email'] = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!isset($_POST['message']) || strlen($_POST['name']) === 0) {
        $error['message'] = 'Please enter your message';
    }

    /*
    //Check if simple anti-bot test is correct
    if ($human !== 5) {
        $errHuman = 'Your anti-spam is incorrect';
    } */

// If there are no errors, send the email
    if (empty($error)) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
        }
    }

}
?>

index.php <-- 注意文件扩展名的变化

<?php include 'contact.php';?>
<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3" id="offer">

            <h2 id="form"> LET'S WORK ? </h2>

        </div>

        <div class="col-md-6 col-md-offset-3">

            <form role="form" method="post">

                <div class="form-group">
                    <input name="name" type="text" class="form-control" placeholder="Enter Your Name">
                    <?php if(isset($error['name'])) echo '<p class="text-danger">'.$error['name'].'</p>'; ?>
                </div>

                <div class="form-group">

                    <input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email">
                    <?php if(isset($error['email'])) echo '<p class="text-danger">'.$error['email'].'</p>'; ?>
                </div>

                <div class="form-group">
                    <textarea name="message" class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here"> </textarea>
                    <?php if(isset($error['message'])) echo '<p class="text-danger">'.$error['message'].'</p>'; ?>

                </div>

                <div class="form-group">
                    <input name="submit" type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text">SEND MESSAGE</input>
                </div>

                <div class="form-group">
                    <div class="col-sm-10 col-sm-offset-2">
                        <?php if(isset($result)) echo $result; ?>
                    </div>
                </div>
            </form>
        </div>

    </div>
</div>

更新

如果您不想在提交表单时重新加载页面,您将需要一些 jQuery ajax 操作并更改您的 HTML 和 PHP 文件。

首先删除我们之前添加的 index.php 的第一行:

 <?php include 'contact.php';?><!-- Remove this one -->

您不希望包含该文件,而是希望向其发送数据。

接下来编辑 HTML 文件并在 HTML 下包含 jQuery 库(在 HTML 下执行 JS 内容的常见做法)。然后相应地更改您的 PHP 文件。

所以你的新 HTML:

<div class="container">
    <div class="row">
        <div class="col-md-6 col-md-offset-3" id="offer">

            <h2 id="form"> LET'S WORK ? </h2>

        </div>

        <div class="col-md-6 col-md-offset-3">

            <form role="form" name="contact" method="post">

                <div class="form-group">
                    <input name="name" type="text" class="form-control" placeholder="Enter Your Name" value="test">
                </div>

                <div class="form-group">

                    <input name="email" type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter Your Email" value="[email protected]">
                </div>

                <div class="form-group">
                    <textarea name="message" class="form-control" id="textarea1" rows="3" placeholder="Enter Your Message here">test </textarea>
                </div>

                <div class="form-group">
                    <input name="submit" type="submit" class="default-submit btn btn-large propClone bg-fast-pink btn-circle font-weight-300 text-white tz-text" value="SEND MESSAGE">
                </div>

                <div class="form-group" id="result">
                </div>
            </form>
        </div>

    </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){ // launch when DOM is fully loaded

        $('form[name="contact"]').submit(function(event){ // fire when you hit submit

            event.preventDefault(); // prevent default form submission since you want to send data via ajax

            $('#result').html('');
            $('.alert').remove();

            var values = $(this).serialize();

            // Post form data to your contact.php script and work with response
            $.ajax({
                url: "contact.php",
                type: "POST",
                data: values ,
                success: function (response) {

                    if(response.success) {
                        $('#result').html('<div class="alert alert-success">'+response.success+'</div>');
                    }
                    if(response.error_form) {
                        $.each( response.error_form, function( key, value ) {
                            $('input[name="'+key+'"]').parent().append('<p class="help-block text-danger">'+value+'</p>');
                        });
                    }
                    if(response.error_mail) {
                        $('#result').html('<div class="alert alert-danger">'+response.error_mail+'</div>');
                    }

                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(textStatus, errorThrown);
                }


            });

        });
    });
</script>

最后改变了 PHP:

<?php

ini_set('display_errors',0);
$result = [];

// Check if name has been entered
if (!isset($_POST['name']) || strlen($_POST['name']) === 0) {
    $result['error_form']['name'] = 'Please enter your name';
}

// Check if email has been entered and is valid
if (!isset($_POST['email']) || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    $result['error_form']['email'] = 'Please enter a valid email address';
}

//Check if message has been entered
if (!isset($_POST['message']) || strlen($_POST['message']) === 0) {
    $result['error_form']['message'] = 'Please enter your message';
}

/*
//Check if simple anti-bot test is correct
if ($human !== 5) {
    $errHuman = 'Your anti-spam is incorrect';
} */

// If there are no errors, send the email
if (empty($result['error_form'])) {


    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    /*$human = intval($_POST['human']); */
    $from = 'Geofrey Zellah';
    $to = '[email protected]';
    $subject = 'Message from Geofrey Zellah ';

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

    if (mail ($to, $subject, $body, $from)) {
        $result['success']='Thank You! I will be in touch';
    } else {
        $result['error_mail']='Sorry there was an error sending your message. Please try again later';
    }
}

header('Content-type: application/json'); // tell browser what to expect
echo json_encode($result); // encode array to json object so javascript can work with it

我做了一个如此详尽的例子,因为许多人在成功发送常规表单后决定使用 ajax,但请注意页面重新加载;)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

提交表单时出错(php和bootstrap)

来自分类Dev

运行表单脚本时,php pdo空白页

来自分类Dev

运行表单脚本时,php pdo空白页

来自分类Dev

表单在提交时显示空白页

来自分类Dev

提交表单后使用会话登录的PHP返回空白页

来自分类Dev

单击表单提交时执行PHP函数

来自分类Dev

PHP和Mysql返回空白页的问题

来自分类Dev

表单上的空白页提交codeigniter

来自分类Dev

表单提交返回空白页

来自分类Dev

PHP!_GET显示空白页

来自分类Dev

PHP文件显示空白页

来自分类Dev

Apache显示PHP的空白页

来自分类Dev

打开.php显示空白页

来自分类Dev

PHP Update sql 显示空白页

来自分类Dev

从mailer.php提交的表单进入空白页。可能重定向到上一个HTML页面?

来自分类Dev

提交表单时为什么会出现空白页?

来自分类Dev

PHP联系表不发送邮件,导致空白页

来自分类Dev

用JavaScript和PHP在Bootstrap中制作的模态表单不会在提交时发送电子邮件

来自分类Dev

用户单击提交按钮时如何更新PHP表单?

来自分类Dev

PHP电子邮件联系表单显示错误

来自分类Dev

带有取消和提交按钮的Bootstrap模式弹出表单,无论单击哪个按钮提交表单

来自分类Dev

Facebook分享使用php和javascript产生白页/空白页

来自分类Dev

jQuery 提交表单和 php

来自分类Dev

PHP 表单(使用 POST)不提交或发送电子邮件,只显示一个空白页面

来自分类Dev

Rails 4 content_for和yield显示空白页

来自分类Dev

php和html注册页面结果为空白页

来自分类Dev

Nginx php5.6 fpm显示空白页

来自分类Dev

单击时提交表单

来自分类Dev

Bootstrap表单提交和模式