-1

我使用 html bootstrap 设计了一个联系表单,但我不知道如何使它工作。我有以下代码。我在网上看到很少有使用 php 来获取联系表单的教程,但我对 php 没有任何了解。谁能帮我这个。如果有人可以帮助我完成表格工作,我将不胜感激。以下是我到目前为止所拥有的

<form method="POST" action="contact-form-submission.php" class="form-horizontal">

<div class="control-group">

    <label class="control-label" for="name">Name</label>

    <div class="controls">

        <input type="text" class="input-medium" id="name">

    </div>

</div>

<div class="control-group">

    <label class="control-label" for="email">Email</label>

    <div class="controls">

        <input type="text" class="input-medium" id="email">

    </div>

</div>

<div class="control-group">

    <label class="control-label" for="website">Website</label>

    <div class="controls">

        <input type="text" class="input-medium" id="website">

    </div>

</div>

<div class="control-group">

    <label class="control-label" for="comment">Comment</label>

    <div class="controls">

        <textarea class="input-medium" id="comment" rows="5"></textarea>

    </div>

</div>

<div class="form-actions">

    <button type="submit" class="btn btn-danger">Submit</button>

    <button type="reset" class="btn">Reset</button>
    <input type="hidden" name="thankyou_url" value="#">

</div>

</form>
4

2 回答 2

2

使用下面的页面并将其命名为contact.php。创建另一个名为“thanks.html”的 html。它会在用户提交表单后显示

<?php
    $from ="xyz@gmail.com";
    if(isset($_POST) && count($_POST)){
        $name = (isset($_POST['name'])) : trim($_POST['name']) : "";
        $email = (isset($_POST['email'])) : trim($_POST['email']) : "";
        $website = (isset($_POST['website'])) : trim($_POST['website']) : "";
        $comment = (isset($_POST['comment'])) : trim($_POST['comment']) : "";

        if(!empty($email)){
            $subject = "new mail";
            $content ="A user has send email<br />
            name: $name<br />
            email: $email<br />
            website: $website<br />
            comment: $comment<br />";

            $headers  = "From: $from\r\n";
            $headers .= "Content-type: text/html\r\n"; 
            mail($email, $subject, $content, $headers); 
            header("location:thanks.html");

        }
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form method="POST" action="" class="form-horizontal">
  <div class="control-group">
    <label class="control-label" for="name">Name</label>
    <div class="controls">
      <input type="text" class="input-medium" id="name" name="name">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="email">Email</label>
    <div class="controls">
      <input type="text" class="input-medium" id="email" name="email">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="website">Website</label>
    <div class="controls">
      <input type="text" class="input-medium" id="website" name="website">
    </div>
  </div>
  <div class="control-group">
    <label class="control-label" for="comment">Comment</label>
    <div class="controls">
      <textarea class="input-medium" id="comment" name="comment" rows="5"></textarea>
    </div>
  </div>
  <div class="form-actions">
    <button type="submit" class="btn btn-danger">Submit</button>
    <button type="reset" class="btn">Reset</button>
    <input type="hidden" name="thankyou_url" value="">
  </div>
</form>
</body>
</html>
于 2013-05-07T11:14:13.140 回答
0

您按照以下步骤操作:

1.在数据库ex中创建一个表。Data

CREATE TABLE IF NOT EXISTS `user_data` (
  `userid` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `email` varchar(255) NOT NULL,
  `comment` varchar(255) NOT NULL,
  `website` varchar(255) NOT NULL,
  PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;

2. 现在修改您的 HTML 代码,如下为每个输入字段分配名称:

 <form method="POST" action="contact-form-submission.php" class="form-horizontal">

     <div class="control-group">

       <label class="control-label" for="name">Name</label>

       <div class="controls">

         <input type="text" class="input-medium" id="name" name="name">

       </div>

     </div>

     <div class="control-group">

       <label class="control-label" for="email">Email</label>

       <div class="controls">

         <input type="text" class="input-medium" id="email" name="email">

       </div>

     </div>

     <div class="control-group">

       <label class="control-label" for="website">Website</label>

       <div class="controls">

         <input type="text" class="input-medium" id="website" name="website">

       </div>

     </div>

     <div class="control-group">

       <label class="control-label" for="comment">Comment</label>

       <div class="controls">

         <textarea class="input-medium" id="comment" rows="5" name="comment"></textarea>

       </div>

     </div>

     <div class="form-actions">

       <button type="submit" class="btn btn-danger">Submit</button>

       <button type="reset" class="btn">Reset</button>
       <input type="hidden" name="thankyou_url" value="http://www.sarahmustafa.com/contact.html">
    </div>
 </form>

3.现在更改你的contact-form-submission.php,如果你愿意,可以更改ttable名称:

$dbhost='localhost';//you can change to your host (www.google/fff/fff')
$dbuser='root';//you can change to your Username of DB (ex. root)
$dbpass='';//you can change to your Password (ex. '')
$dbname='Data';//you can change to your Database created on DB   (ex.user_data)


db_connect($dbhost,$dbuser,$dbpass,$dbname);
$name=$_POST["name"];
$email=$_POST["email"];
$website=$_POST["website"];
$comment=$_POST["comment"];
//change the field to actual Databse fields 
$sql="INSERT INTO `user_data` (`name`,`email`,`comment`,`website`) VALUES ('$name','$email','$comment','$website')";
$result=mysql_query($sql);



function db_connect($dbhost,$dbuser,$dbpass,$dbname)
{
    $conn = mysql_pconnect($dbhost,$dbuser,$dbpass);
    if (!$conn)
        return "connect_failed";

    if (!mysql_select_db($dbname))
        return "db_select_failed";
    return "succes";
}
于 2013-05-07T11:32:36.673 回答