这是我第一次使用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>
答案 0 :(得分:0)
<强>编辑:强> 正如评论部分所述,第一部分是不正确的,我很抱歉给出了错误的答案。我不知道动作属性在html 5上是可选的,应该在发布前完成我的研究。道歉。
这是不正确的:
没有任何内容显示,因为您没有指定将在哪个php文件中处理表单。 编辑您的表单标签(将index.php替换为您的php文件名):
<form action="index.php" method="post">
此外,
的“提交”部分isset($_POST['submit'])
必须与提交按钮的名称匹配:
<input type="submit" name="send" value="Submit Details Now!">
应该是:
<input type="submit" name="submit" value="Submit Details Now!">