这是我第一次使用 PHP,我拼凑了一个可以在一个页面上运行的表单。问题是,当您第一次访问该页面时,它会显示“请输入您的电子邮件地址以便我们与您联系”,但我只希望在按下提交按钮后出现错误/成功消息。
此外,我希望所有消息在输出时都包含在 div 中。
我读了一篇建议使用类似以下内容的帖子,但是当我尝试实施时,按下提交时不会出现任何消息:
if (isset($_POST['submit']))
{
// Process Form
}
else
{
// Show Form
}
无论如何,这是我的完整代码:
<?php
$to = "me@gmail.com" ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$startmonth = $_REQUEST['StartMonth'];
$startyear = $_REQUEST['StartYear'];
$endmonth = $_REQUEST['EndMonth'];
$endyear = $_REQUEST['EndYear'];
$message = $_REQUEST['Message'];
$fields = array();
$fields{"Name"} = "Name";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$selectedProjects = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
$selectedProjects = implode(', ', $_POST['projects']);
}
$selectedSkills = 'None';
if(isset($_POST['skills']) && is_array($_POST['skills']) && count($_POST['skills']) > 0){
$selectedSkills = implode(', ', $_POST['skills']);
}
$selectedNoRush = 'None';
if(isset($_POST['norush']) && is_array($_POST['norush']) && count($_POST['norush']) > 0){
$NoRush= implode(', ', $_POST['norush']);
}
$selectedWhenReady = 'None';
if(isset($_POST['whenready']) && is_array($_POST['whenready']) && count($_POST['whenready']) > 0){
$WhenReady= implode(', ', $_POST['whenready']);
}
$selectedBudget = 'None';
if(isset($_POST['budget']) && is_array($_POST['budget']) && count($_POST['budget']) > 0){
$selectedBudget= implode(', ', $_POST['budget']);
}
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);}
$body .= "\n" . 'Selected Projects: ' . $selectedProjects . "\n";
$body .= 'Selected Skills: ' . $selectedSkills . "\n\n";
$body .= 'Start Date: ' . $startmonth . " " . $startyear . " " . $NoRush . "\n";
$body .= 'End Date: ' . $endmonth . " " . $endyear . " " . $WhenReady . "\n";
$body .= 'Budget: ' . $selectedBudget . "\n\n";
$body .= 'Message:' . $message . "\n";
$headers2 = "From: me@gmail.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours.";
if($from == '') {print "Please enter your email address so we can contact you.";}
else {
if($name == '') {print "Please enter your name so we know who sent this.";}
else {
if($message == '') {print "Please give a brief description so we can understand how best to help.";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{print "Thank you. Your request has been successfully submitted.";}
else
{print "We encountered an error sending your mail, please check your details are correct or email us at hello@lyonempire.co.uk"; }
}}}
?>
谢谢。
PS。如果有人想指出括号和代码布局的更好做法,请随意。我知道我可以做得更好,但不知道怎么做。
MC
更新 - 这是html:
<form method="post">
<h3>Your Details</h3>
<div class="formrow">
<input type="text" name="Name" maxlength="99" id="fullname" placeholder="Name" />
<input type="email" name="Email" maxlength="99"placeholder="Email Address" />
<input type="tel" name="Phone" maxlength="25" placeholder="Phone Number" />
</div>
<h3>Project Type</h3>
<div class="formrow">
<fieldset>
<input type="checkbox" id="type1" name="projects[]" value="Web">
<label for="type1">Web</label>
<input type="checkbox" id="type2" name="projects[]" value="Digital Media">
<label for="type2">Digital Media</label>
<input type="checkbox" id="type3" name="projects[]" value="Consultancy">
<label for="type3">Consultancy</label>
</fieldset>
</div>
<h3>A Brief Description</h3>
<div class="formrow">
<textarea name="Message"></textarea>
</div>
<h3>What do you do?</h3>
<div class="formrow">
<fieldset>
<div class="skillrow">
<input type="checkbox" id="skill1" name="skills[]" value="Website Design">
<label for="skill1">Website Design</label>
<input type="checkbox" id="skill2" name="skills[]" value="Social Networking">
<label for="skill2">Social Networking</label>
<input type="checkbox" id="skill3" name="skills[]" value="E-Commerce">
<label for="skill3">E-Commerce</label>
</div>
<div class="skillrow">
<input type="checkbox" id="skill4" name="skills[]" value="Content Management Systems">
<label for="skill4">Content Management Systems</label>
<input type="checkbox" id="skill5" name="skills[]" value="Video">
<label for="skill5">Video</label>
<input type="checkbox" id="skill6" name="skills[]" value="Images">
<label for="skill6">Images</label>
</div>
<div class="skillrow">
<input type="checkbox" id="skill7" name="skills[]" value="Social Media">
<label for="skill7">Social Media</label>
<input type="checkbox" id="skill8" name="skills[]" value="Strategy">
<label for="skill8">Strategy</label>
<input type="checkbox" id="skill9" name="skills[]" value="Best Practice">
<label for="skill9">Best Practice</label>
</div>
<div class="skillrow">
<input type="checkbox" id="skill10" name="skills[]" value="Not Sure">
<label for="skill10">Not Sure</label>
</div>
</fieldset>
</div>
<h3>Ideal Start Date</h3>
<div class="formrow">
<label class="drops">
<select name="StartMonth">
<option value="">---</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
</label>
<label class="drops">
<select name="StartYear">
<option value="">---</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
</label>
<input type="checkbox" id="norush" name="norush[]" value="No Great Rush">
<label for="norush">No Great Rush</label>
</div>
<h3>Ideal End Date</h3>
<div class="formrow">
<label class="drops">
<select name="EndMonth">
<option value="">---</option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
</label>
<label class="drops">
<select name="EndYear">
<option value="">---</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
</select>
</label>
<input type="checkbox" id="whenready1" name="whenready[]" value="When It's Ready">
<label for="whenready1">When it's good and ready</label>
</div>
<h3>Estimated Budget</h3>
<div class="formrow">
<input type="radio" id="budget1" name="budget[]" value="Under 800">
<label for="budget1">Under £800</label>
<input type="radio" id="budget2" name="budget[]"value="800 to 1600">
<label for="budget2">£800 - £1600</label>
<input type="radio" id="budget3" name="budget[]" value="Over 1600">
<label for="budget3">Over £1600</label>
</div>
<h3>Good to Go?</h3>
<div class="goodtogo">
<input type="submit" name="send" value="Submit Details Now!">
</div>
</form>