引导模式中的PHP联系表单

时间:2015-09-16 15:07:15

标签: php twitter-bootstrap modal-dialog

好的,我是新手,所以欢迎任何反馈

我想使用bootstrap模式作为联系表单。我已经在php中有一个联系表单但是如何以模式格式显示它

谢谢,

David

这是php表单

<?php

if ($_POST["submit"]) {
$result = '<div class="alert alert-success">Form submitted</div>';

  if (!$_POST['name']) {
    $error="<br />Please enter your name";
  }

  if (!$_POST['email']) {
    $error.="<br />Please enter your email address";
  }

   if (!$_POST['comment']) {
    $error.="<br />Please enter a comment";
  }  

  if ($_POST ['email']!="" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){

    $error.="<br />Please enter a valid email address";
  }


  if ($error){
    $result = '<div class="alert alert-danger"><strong>There were error(s) with your form:</strong> '.$error.'</div>';
  } else {

   if (mail("david@naturallypizza.org", "Comment from website!",

"You have an email from the website contact form\r\n"."
Name: ".$_POST['name']."
Email: ".$_POST['email']."
Comment: ".$_POST['comment'])) {

    $result = '<div class="alert alert-success"><strong>Thank you for your email! I\'ll get back to you soon</strong></div>';

   } else {

      $result = '<div class="alert alert-danger"><strong>Sorry there was an error with your message, please try again later!</strong></div>';
      }

  }

  }
?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Contact Form</title>

    <!-- Bootstrap -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->


    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  <style>


    .emailForm {

        border:1px solid grey;
        border-radius: 10px;
        margin-top: 20px;
        padding-bottom: 10px;

    }

    textarea {

        height: 220px;
    }

  </style>






  </head>




  <body>

    <div class="container">

        <div class="row">

            <div class="col-md-6 col-md-offset-3 emailForm">
             <h1>Contact us!</h1>


             <?php echo $result; ?>

            <p class="lead">Please get in touch!</p>

            <form method="post">

                <div class="form-group">
                    <label for ="name">Your name:</label>
                    <input type="text" name="name" class="form-control" placeholder="Name" value= "<?php echo $_POST['name']; ?>"/>   
                </div>

                <div class="form-group">
                    <label for ="email">Your email:</label>
                    <input type="email" name="email" class="form-control" placeholder="Email" value="<?php echo $_POST['email'];?>"/>   
                </div>

                <div class="form-group">
                    <label for ="comment">Your comment:</label>
                    <textarea class="form-control" name="comment"><?php echo $_POST['comment'];?></textarea>
                </div>

                <input type="submit" name="submit" class="btn btn-success btn-lg" value ="submit">



            </form> 
            </div>
        </div>

    </div> 


    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

3 个答案:

答案 0 :(得分:0)

这是一个基本示例,请查看文档&gt; Bootstrap Javascript。您还需要删除容器/行/列格式,因为它会在视口更改时将输入移出窗体。

&#13;
&#13;
.emailForm {
  border-radius: 10px;
  margin-top: 20px;
  padding-bottom: 10px;
}
textarea {
  height: 220px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<!-- Button trigger modal -->
<button type="button" class="btn btn-success btn-lg" data-toggle="modal" data-target="#myModal">Launch Form</button>
<!-- Modal -->
<div class="modal fade slide left" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>

        </button>
        <h1 class="modal-title" id="myModalLabel">Contact Us!</h1>

      </div>
      <div class="modal-body">
        <p class="lead">Please get in touch!</p>
        <form method="post" id="myForm">
          <div class="form-group">
            <label for="name">Your name:</label>
            <input type="text" name="name" id="name" class="form-control" placeholder="Name" value="" required/>
          </div>
          <div class="form-group">
            <label for="email">Your email:</label>
            <input type="email" name="email" id="email" class="form-control" placeholder="Email" value="" required/>
          </div>
          <div class="form-group">
            <label for="comment">Your comment:</label>
            <textarea class="form-control" id="comment" name="comment" required></textarea>
          </div>
          <input type="submit" name="submit" class="btn btn-success btn-lg" value="submit">
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-danger" data-dismiss="modal">Cancel Form</button>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

<强>的index.html

 <?php
    if ($_POST["submit"]) {
    $result = '<div class="alert alert-success">Form submitted</div>';
      if (!$_POST['name']) {
        $error="<br />Please enter your name";
      }
      if (!$_POST['email']) {
        $error.="<br />Please enter your email address";
      }
       if (!$_POST['comment']) {
        $error.="<br />Please enter a comment";
      }  
      if ($_POST ['email']!="" AND !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){

        $error.="<br />Please enter a valid email address";
      }
      if ($error){
        $result = '<div class="alert alert-danger"><strong>There were error(s) with your form:</strong> '.$error.'</div>';
      } else {
       if (mail("david@naturallypizza.org", "Comment from website!",
    "You have an email from the website contact form\r\n"."
    Name: ".$_POST['name']."
    Email: ".$_POST['email']."
    Comment: ".$_POST['comment'])) {
        $result = '<div class="alert alert-success"><strong>Thank you for your email! I\'ll get back to you soon</strong></div>';
       } else {
          $result = '<div class="alert alert-danger"><strong>Sorry there was an error with your message, please try again later!</strong></div>';
          }
      }
    }
    ?>

    <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>Contact Form</title>

        <!-- Bootstrap -->
        <link href="css/bootstrap.min.css" rel="stylesheet">

        <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->

      <style>
        .emailForm {

            border:1px solid grey;
            border-radius: 10px;
            margin-top: 20px;
            padding-bottom: 10px;
        }
        textarea {
            height: 220px;
        }
      </style>
      </head>
      <body>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 emailForm">
                 <h1>Contact us!</h1>
                 <?php echo $result; ?>
                <p class="lead">Please get in touch!</p>
                <a class='Contact' href="#form_modal" data-toggle="modal">
                Contact
            </a>
                </div>
            </div>

        </div> 

        <div id="form_modal" class="modal fade" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">

                </div>
            </div>
        </div>
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>
        <script>
            $('.Contact').click(function(){
                $.ajax({url:"Contact.php",cache:false,success:function(result){
                    $(".modal-content").html(result);
                }});
            });
        </script>
      </body>
    </html>

Contact.php 现在,创建一个Contact.php页面。

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
    <h4 class="modal-title">Contact</h4>
</div>
</form>
    <div class="modal-body" style='text-align:justify;'>
        <div class="form-group">
                    <label for ="name">Your name:</label>
                    <input type="text" name="name" class="form-control" placeholder="Name" value= "<?php echo $_POST['name']; ?>"/>   
                </div>

                <div class="form-group">
                    <label for ="email">Your email:</label>
                    <input type="email" name="email" class="form-control" placeholder="Email" value="<?php echo $_POST['email'];?>"/>   
                </div>

                <div class="form-group">
                    <label for ="comment">Your comment:</label>
                    <textarea class="form-control" name="comment"><?php echo $_POST['comment'];?></textarea>
                </div>
    </div>
    <div class="modal-footer">
        <button type="submit" class="btn btn-primary">Yes</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">No</button>
    </div>
</form>

答案 2 :(得分:0)

#include <stdio.h>
#define MAX_INPUT_CHAR 100
int main( ) {

   char str[MAX_INPUT_CHAR ];

   printf( "Enter a value :");
   gets( str );   

   return 0;
}
.emailForm {
  border-radius: 10px;
  margin-top: 20px;
  padding-bottom: 10px;
}
textarea {
  height: 220px;
}