php联系表单下拉“名称”字段

时间:2013-10-14 12:10:05

标签: php email

我有一个带有“姓名”字段的联系表格:

<form class="form-inline" role="form" method="post" action="contactengine.php" id="commentForm" style="display:table; margin:20px auto">

 <div class="well">

 <div class="form-group">
 <label class="sr-only" for="Email">Email address</label>
 <input type="email" name="Email" id="Email" class="form-control required" placeholder="Enter email address" autofocus>
 </div>

 <div class="form-group">
 <label class="sr-only" for="Name">Name</label>
 <input type="text" name="Name" id="Name" class="form-control required" placeholder="Last, First" >
 </div>

 <div class="form-group">
 <label class="sr-only" for="Rating">Rating</label>
 <input class="form-control" placeholder="rating" type="number" name = "Rating" id = "Rating" min="1200" max="2500" value="2000">
 </div> 

 <div class="form-group">
 <label class="sr-only" for="City">Location</label>
 <input type="text" class="form-control required" placeholder="City, Country" name="City" id="City" pattern=".{2,23}" />
 </div> 

 </div>

<div class="well">
<div class="col-md-12 col-md-offset-0">

<textarea placeholder = "Optionally, list some chess background info here about your abilities and style. Include experience, books read, goals, strengths and weaknesses, even a pgn file." name="Message" id="Message" rows="4" class="required" style="width:100%"></textarea>

</div>
</div>

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

<div style="margin: 0 auto; display:table;">

<button type="reset" name="submit" class="btn btn-warning bold" style="float:left; font-size:18px; margin:20px 40px">Reset</button>

<button type="submit" name="submit" class="btn btn-success bold" style="float:right; font-size:18px; margin:20px 40px">Submit</button>

</div>
</div>

</form> 

我有一个PHP脚本来处理它:

<?php

// CHANGE THE VARIABLES BELOW


$EmailFrom = "joeblow@gmail.com";
$EmailTo = "joeblow@gmail.com";
$Subject = "Contact Form Submission";

$Name = Trim(stripslashes($_POST['Name'])); 
$Rating = Trim(stripslashes($_POST['Rating'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$City = Trim(stripslashes($_POST['City'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

// prepare email body text
$Body = "";

$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";

$Body .= "Rating: ";
$Body .= $Rating;
$Body .= "\n";

$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";

$Body .= "Location: ";
$Body .= $City;
$Body .= "\n";

$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
// CHANGE THE URL BELOW TO YOUR "THANK YOU" PAGE
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
?>

问题是脚本有效,除了它没有发送名称,只是“名称:”并且没有从表单中获取名称。这有什么问题?

1 个答案:

答案 0 :(得分:0)

对我来说很好。您可以像这样验证表单字段“名称”(以及许多其他方式,但这里是快速修复):

if (isset($_POST['Name']) && !empty($_POST['Name'])) {
    //your PHP code 
}
相关问题