我一直在使用Bootstrap开发一个单页响应式网站(来自youtube教程)。该教程直到前端才开始。现在我想联系我们表格,所以我添加了我在网上找到的PHP代码。
我觉得" echo"在我的代码中,任何人都可以指示我错误的位置???对不起,如果这是一个基本问题!
HTML代码(使用PHP代码):
<div class="col-lg-8">
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="user-name" class="col-lg-2 control-label"> Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="user-name" id="user-name" placeholder="Enter your name">
<?php
echo $errName;?>
</div>
</div><!--end form-group-->
<div class="form-group">
<label for="user-email" class="col-lg-2 control-label"> Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="user-email" id="user-email" placeholder="Enter your Email Address">
<?php echo "<p class='text-danger'> $errEmail </p>";?>
</div>
</div><!--end form-group-->
<div class="form-group">
<label for="user-url" class="col-lg-2 control-label"> Your website</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="user-url" id="user-url" placeholder="If you have any">
</div>
</div><!--end form-group-->
<div class="form-group">
<label for="user-message" class="col-lg-2 control-label"> Any message</label>
<div class="col-lg-10">
<textarea name="user-message" id="user-message" class="form-control"
cols="20" rows="10" placeholder="Enter your Message"></textarea>
<?php echo "<p class='text-danger'> $errMessage </p>";?>
</div>
</div><!--end form-group-->
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
PHP代码:
<?php
$errName = '';
$errMessage = '';
$errEmail = '';
$result = '';
$submit = isset($_POST['submit']) ? $_POST['submit'] : NULL;
if (is_string($submit)) {
$name = $_POST['user-name'];
$email = $_POST['user-email'];
$url = $_POST['user-url'];
$message = $_POST['user-message'];
$from = 'Demo Contact Form';
$to = 'mahendran.vattekat@gmail.com';
$subject = 'Message from Contact Demo ';
$body = "From: $name\n E-Mail: $email\n URL: $url\n Message:\n $message";
// Check if name has been entered
if (!$_POST['user-name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['user-email'] || !filter_var($_POST['user-email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['user-message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
更新:好的,这非常令人沮丧。我有一个本地服务器设置,获得这些更新的代码,他们看起来像一个单独的PHP文件。但是当我尝试执行html + php文件(以及我的网站)和index.php文件时,它会变得疯狂,因为在按下提交按钮后我会留下一个空白页面。这非常令人沮丧,因为我将index.php和我的主要php文件放在与我的XAMPP本地服务器htdocs文件夹相同的文件夹中。
Aren的两个php文件应该相互交互?当我将它作为一个整体网站使用时,为什么错误检查代码不起作用?我也继续得到&#34;注意:(主php文件)中的未定义变量,即使我在同一个文件夹上有index.php文件!
请帮忙。谢谢。
答案 0 :(得分:1)
快速浏览一下,我没有看到问题,但更换了这种代码:
<?php echo "<p class='text-danger'>$errEmail</p>";?>
由此:
<p class='text-danger'>
<?php echo $errEmail; ?>
</p>
可能会帮助您发现错误。它看起来更清洁不是吗?
您不需要使用echo打印HTML代码。不要使用超过你需要的PHP打印。
同时将顶部的if更改为:
if ($_POST)
只是为了确保在您提交表单时输入条件(我不知道您是否有名称为&#39;提交&#39;的字段)
答案 1 :(得分:0)
我已经解决了你脚本中的几个问题:
此脚本一个文件'index.php'将其上传到您的服务器上并进行实验/更改
<?php
$errName = '';
$errMessage = '';
$errEmail = '';
$result = '';
$submit = isset($_POST['submit']) ? $_POST['submit'] : NULL;
if (is_string($submit)) {
$name = $_POST['user-name'];
$email = $_POST['user-email'];
$url = $_POST['user-url'];
$message = $_POST['user-message'];
$from = 'Demo Contact Form';
$to = 'mahendran.vattekat@gmail.com';
$subject = 'Message from Contact Demo ';
$body = "From: $name\n E-Mail: $email\n URL: $url\n Message:\n $message";
// Check if name has been entered
if (!$_POST['user-name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['user-email'] || !filter_var($_POST['user-email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['user-message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
<div class="col-lg-8">
<form class="form-horizontal" role="form" method="post" action="index.php">
<div class="form-group">
<label for="user-name" class="col-lg-2 control-label"> Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="user-name" id="user-name" placeholder="Enter your name">
<?php
echo $errName;?>
</div>
</div><!--end form-group-->
<div class="form-group">
<label for="user-email" class="col-lg-2 control-label"> Email</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="user-email" id="user-email" placeholder="Enter your Email Address">
<?php echo "<p class='text-danger'> $errEmail </p>";?>
</div>
</div><!--end form-group-->
<div class="form-group">
<label for="user-url" class="col-lg-2 control-label"> Your website</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="user-url" id="user-url" placeholder="If you have any">
</div>
</div><!--end form-group-->
<div class="form-group">
<label for="user-message" class="col-lg-2 control-label"> Any message</label>
<div class="col-lg-10">
<textarea name="user-message" id="user-message" class="form-control"
cols="20" rows="10" placeholder="Enter your Message"></textarea>
<?php echo "<p class='text-danger'> $errMessage </p>";?>
</div>
</div><!--end form-group-->
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
答案 2 :(得分:0)
首先在html中删除form标签中的两个动作并给出一个动作=&#34; index.php&#34;