2016-02-27 21 views
0

我是一个PHP表单的极端新手。几天前我刚刚介绍给他们,开始研究并找到了适合新手的教程,但在调整时我完全失去了意义。如何调整PHP表格

我与下面的代码有两个问题,我不希望主题是自动的,因为表单将被用于几个原因。我也不知道那些“标题”是什么。我不想将我的电子邮件发送给使用该表单的人。

这里是我的HTML代码:

<form action="send-mail.php" method="post" enctype="text/plain" name="contact-form"> 
        <!--RECOMMENDED TO USE POST METHOD FOR SENSITIVE DATA--> 


        <div id="floatLeft"> <!--TO ALLOW FOR SIDE X SIDE BOXES AS SCREEN SIZE INCREASES--> 

         <!--NAME BOX--> 
         <p><input type="text" name="name" placeholder="Name" autocomplete="off" oninvalid="setCustomValidity('This Field Cannot Be Left Blank')" oninput="setCustomValidity('')" required > 
         <small class="error"></small></p> 

         <!--EMAIL BOX--> 
         <p><input type="email" name="email" placeholder="Email" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,3}$" oninvalid="setCustomValidity('Please Enter A Valid Email')" oninput="setCustomValidity('')" autocomplete="off" required> 
         <small class="error"></small></p> 


         <!--PHONE BOX--> 
         <p><input type="tel" name="telephone" onBlur='addDashes(this)' placeholder="Phone (no dashes)" pattern='^\d{3}-\d{3}-\d{4}$' oninvalid="setCustomValidity('Minimum length is 10 digits')" oninput="setCustomValidity('')" autocomplete="off" required> 
         <small class="error"></small></p> 


         <!--SUBJECT BOX--> 
         <p><input id="subject" type="text" name="subject" placeholder="Subject" pattern=".{3,}" oninvalid="setCustomValidity('Must Enter A Minimum of 3 Characters')" oninput="setCustomValidity('')" required></p> 

         <div class="clear"></div> <!--////// Necessary to properly push comments box to next line ////////--> 

        </div> 

        <div id="floatRight"> <!--TO ALLOW FOR SIDE X SIDE BOXES AS SCREEN SIZE INCREASES--> 

         <!--FREEFORM BOX--> 
         <p><textarea name="comments" cols="110" rows="12" placeholder="Message" oninvalid="setCustomValidity('This Field Cannot Be Left Blank')" oninput="setCustomValidity('')" required></textarea></p> 
         <p><input type="submit" onClick='addDashes(this.form)' value="Send"></p> 

        </div> 

        </form> 

这里是我的PHP代码:

<?php 
$mail_to = '[email protected]'; // specify your email here 

// Assigning data from the $_POST array to variables 
$name = $_POST['name']; 
$mail_from = $_POST['email']; 
$phone = $_POST['telephone']; 
$message = $_POST['comments']; 

// Construct email subject 
$subject = 'www.mysite.com Message from visitor ' . $name; 

// Construct email body 
$body_message = 'From: ' . $name . "\r\n"; 
$body_message .= 'E-mail: ' . $mail_from . "\r\n"; 
$body_message .= 'Phone: ' . $phone . "\r\n"; 
$body_message .= 'Message: ' . $message; 

// Construct email headers 
$headers = 'From: ' . $mail_from . "\r\n"; 
$headers .= 'Reply-To: ' . $mail_from . "\r\n"; 

$mail_sent = mail($mail_to, $subject, $body_message, $headers); 

if ($mail_sent == true){ ?> 
    <script language="javascript" type="text/javascript"> 
    alert('Thank you for the message. I will get back to you shortly.'); 
    window.location = 'index.html'; 
    </script> 
<?php } else { ?> 
<script language="javascript" type="text/javascript"> 
    alert('Message not sent. Please, notify the site administrator [email protected]'); 
    window.location = 'index.html'; 
</script> 
<?php 
} ?> 
+0

你在使用Bootstrap吗? –

+0

我没有使用Bootstrap。我以前从未使用过。到现在为止,我的网站只有HTML,CSS和JS。 – Brenda813

+0

在你的地方,我会考虑使用它。它很简单,并且在调整表单时有很大帮助。 –

回答

0

您是否尝试过改变由获得这个职位的主题。如果您不希望它以该网站开头,则可能需要删除构建电子邮件主题。

我不认为我明白给人们你的电子邮件问题的一部分。

$subject = $_POST['subject']; 

// Construct email subject 
$subject = 'www.mysite.com Message - ' . $subject; 
+0

谢谢!我会试试看。 – Brenda813

+0

对于电子邮件 - 基本上,我没有任何人访问该网站能够得到我的电子邮件。我不想让自己暴露于垃圾邮件,就像我说的,我完全不熟悉这一点,所以如果我解释错误,或者不了解电子邮件表单的工作方式,我很抱歉 – Brenda813

+0

我假设邮件在php代码将用于存储你的电子邮件,当服务器向用户提供页面时,它不应该在部分显示任何内容。你可以在服务器上加载后查看页面源,以确保它正在服务它正确,并验证您看不到您的电子邮件。 – Joel

0

埃德·阿尔梅达......我不认为它与引导有任何关系。

如果我正确地得到你,你想要 1.主题要动态 2.你想了解标题工作?

<?php 
if (isset($_POST['submit'])) 
{ 
// You specify the email you want to recieve your message. This way, no one wud see the email because it is server side and would not be visible to all 
    $mail_to = '[email protected]'; 





// Assigning data from the $_POST array to variables and clear it of every possible intrusuin. Just make this a tradition ok? 
    $name = mysql_real_escape_string(htmlentities($_POST['nname'])); 
    $mail_from = mysql_real_escape_string(htmlentities($_POST['email'])); 
    $phone = mysql_real_escape_string(htmlentities($_POST['telephone'])); 
    $message = mysql_real_escape_string(htmlentities($_POST['comments'])); 
    $subject = mysql_real_escape_string(htmlentities($_POST['subject'])); 

    // To send HTML mail, the Content-type header must be set 
     $headers = 'MIME-Version: 1.0' . "\r\n"; 
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

     // Create email headers The format below. gives you a perfect arrangment 

     $headers .= 'From: '.'www.mysite.com Message from visitor '.$name .'<'.$mail_from.'>'."\r\n". 
       'Reply-To: '.$mail_from."\r\n" . 
       'X-Mailer: PHP/' . phpversion(); 

     // Construct email body 
     $body_message = 'From: ' . $name . "\r\n"; 
     $body_message .= 'E-mail: ' . $mail_from . "\r\n"; 
     $body_message .= 'Phone: ' . $phone . "\r\n"; 
     $body_message .= 'Message: ' . $message;  

     // Construct email subject 
     $subjectToSend = 'message subject:' . $subject; 

     $mail_sent = mail($mail_to, $subjectToSend, $body_message, $headers); 

     if ($mail_sent == true){ 
     ?> 
      <script language="javascript" type="text/javascript"> 
      alert('Thank you for the message. I will get back to you shortly.'); 
      window.location = 'index.html'; 
      </script> 
     <?php } else { ?> 
     <script language="javascript" type="text/javascript"> 
      alert('Message not sent. Please, notify the site administrator [email protected]'); 
      window.location = 'index.html'; 
     </script> 
     <?php } ?> 

<?php }?> 

<!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=iso-8859-1" /> 
<title>Untitled Document</title> 
</head> 

<body> 

<form action="send-mail.php" method="post" enctype="text/plain" name="contact-form"> 
        <!--RECOMMENDED TO USE POST METHOD FOR SENSITIVE DATA--> 


        <div id="floatLeft"> <!--TO ALLOW FOR SIDE X SIDE BOXES AS SCREEN SIZE INCREASES--> 

         <!--NAME BOX--> 
         <p><input type="text" name="name" placeholder="Name" autocomplete="off" oninvalid="setCustomValidity('This Field Cannot Be Left Blank')" oninput="setCustomValidity('')" required > 
         <small class="error"></small></p> 

         <!--EMAIL BOX--> 
         <p><input type="email" name="email" placeholder="Email" pattern="[a-z0-9._%+-][email protected][a-z0-9.-]+\.[a-z]{2,3}$" oninvalid="setCustomValidity('Please Enter A Valid Email')" oninput="setCustomValidity('')" autocomplete="off" required> 
         <small class="error"></small></p> 


         <!--PHONE BOX--> 
         <p><input type="tel" name="telephone" onBlur='addDashes(this)' placeholder="Phone (no dashes)" pattern='^\d{3}-\d{3}-\d{4}$' oninvalid="setCustomValidity('Minimum length is 10 digits')" oninput="setCustomValidity('')" autocomplete="off" required> 
         <small class="error"></small></p> 


         <!--SUBJECT BOX--> 
         <p><input id="subject" type="text" name="subject" placeholder="Subject" pattern=".{3,}" oninvalid="setCustomValidity('Must Enter A Minimum of 3 Characters')" oninput="setCustomValidity('')" required></p> 

         <div class="clear"></div> <!--////// Necessary to properly push comments box to next line ////////--> 

        </div> 

        <div id="floatRight"> <!--TO ALLOW FOR SIDE X SIDE BOXES AS SCREEN SIZE INCREASES--> 

         <!--FREEFORM BOX--> 
         <p><textarea name="comments" cols="110" rows="12" placeholder="Message" oninvalid="setCustomValidity('This Field Cannot Be Left Blank')" oninput="setCustomValidity('')" required></textarea></p> 
         <p><input type="submit" onClick='addDashes(this.form)' value="Send" name="submit"></p> 

        </div> 

        </form> 

</body> 
</html> 
+0

正确。这就是我正在寻找。 – Brenda813

+0

我wud工作出你somthing很快!尝试代码完美的你。请给我几分钟Brenda813 – Legendchyke

+0

没问题。非常感谢! – Brenda813