【发布时间】:2016-08-03 22:09:09
【问题描述】:
我正在尝试将此代码转换为 smarty,以便它可以通过我的 whmcs 正常工作,尽管我尝试仅将 php 文件包含在包含 php 的 tpl 中(它自己完美地工作,只是它不显示虽然 whmcs 正确)所以我想将当前的 php 更改为 smarty 以便他的代码直接从 tpl 文件运行顺利。
This 是我包含 php 页面时的样子,this 基本上是我想要的样子{/php} 但它不能正常工作,因为我相信还有其他聪明的语法需要改变,但我不知道什么和在哪里
有人可以帮忙吗?
<script type="application/javascript">
$(function(){
$("input[type='radio']").change(function(){
var val = $(this).val();
$(this).parent().find("input[type='radio']").each(function(){
if ($(this).val() < val){
$(this).addClass("belowchecked");
} else {
$(this).removeClass("belowchecked");
}
});
});
});
</script>
<?php
//Require Database
require('connect.php');
//Variables
$name = $_POST['name'];
$email = $_POST['email'];
$website = $_POST['website'];
$service = $_POST['service'];
$comment = $_POST['comment'];
$rating = $_POST['star'];
$robotest = $_POST['robotest'];
$captcha = $_POST['captcha'];
date_default_timezone_set('Europe/London');
$date = date('Y-m-d');
// If Form Is Submitted
if(isset($_POST['add'])){
// Check User Entered Name
if(empty ($name)){
$error[] = "<b>Name</b>";
}
// Check User Entered Email
if(empty ($email)){
$error[] = "<b>Email Address</b>";
}
// Checks Users Email is valid
else if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$error[] = "<b>Email</b>";
$error2[] = "Email is invalid";
}
// If Website Is Not Empty
if (!empty ($website)) {
// Check Website Is Valid
if(!filter_var($website, FILTER_VALIDATE_URL))
{
$error[] = "<b>Website</b>";
$error2[] = "<b>Website</b> must begin with <b>http://</b> and end with your domain extention";
}
}
// Else Website feild = null
else {
$website = "";
}
//Check User Entered Rating
if(empty($rating)) {
$error[] = "<b>Rating</b>";
}
// Check User Entered Feedback
if(empty ($comment)){
$error[] = "<b>Feedback</b>";
}
// Check User Is Human
if($robotest) {
$error[] = "You are a gutless robot.";
}
if ($captcha != $_SESSION['captcha']){
$error2[] = "Please reenter <b>Captcha</b>";
}
// If No Errors Insert To Database & Redirect To Feedback
if (empty ($error)) {
if (empty ($error2))
{
mysql_query("INSERT INTO comment (id, name, email, comment, rating, postdate, service, website) VALUES ('','$name','$email','$comment','$rating','$date','$service','$website')");
$status = "Thankyou for your feedback";
ob_end_flush;
header ("location: feedback.php#main");
}
}
}
?>
<section class="post-info" id="addfb" name="add">
<form action="#addfb" class="forms" method="POST">
<div class="stars">
<input class="star star-5" id="star-1" type="radio" name="star" value="5" <?php if (isset($rating) && $rating=="5") echo "checked";?>/>
<label class="star star-5" for="star-1"></label>
<input class="star star-4" id="star-2" type="radio" name="star" value="4" <?php if (isset($rating) && $rating=="4") echo "checked";?>/>
<label class="star star-4" for="star-2"></label>
<input class="star star-3" id="star-3" type="radio" name="star" value="3" <?php if (isset($rating) && $rating=="3") echo "checked";?>/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-4" type="radio" name="star" value="2" <?php if (isset($rating) && $rating=="2") echo "checked";?>/>
<label class="star star-2" for="star-4"></label>
<input class="star star-1" id="star-5" type="radio" name="star" value="1" <?php if (isset($rating) && $rating=="1") echo "checked";?>/>
<label class="star star-1" for="star-5" ></label>
</div>
<br>
Name <span class="Required">*Required</span><br>
<input name="name" type="text" value="<?php echo $name; ?>">
</p>
<p>Email <span class="Required">*Required</span><br>
<input name="email" type="text" value="<?php echo $email; ?>">
</p>
<p>Website <span class="Required">http://</span><br>
<input name="website" type="text" id="website" value="<?php echo $website; ?>">
</p>
<p>Service <span class="Required">*Required</span></p>
<select name="service" class="services">
<optgroup label="Peronal Services">
<option value="Buisness Card Design" selected="selected">Business Card Design</option>
<option value="Website Design">Website Design</option>
<option value="Banner Design">Banner Design</option>
<option value="Poster Design">Poster Design</option>
<option value="Flyer Design">Flyer Design</option>
<option value="Logo Design">Logo Design</option>
</optgroup>
<optgroup label="Service Packages" class="services">
<option value="Buisness Package">Business Package</option>
<option value="Personal Package">Personal Package</option>
<option value="Charity Package">Charity Package</option>
<option value="Mixed Services">Mixed Services</option>
</optgroup>
</select>
</p>
<p>Feedback<span class="Required"> *Required</span><br>
<textarea id="comment" rows="8" cols="20" name="comment"><?php echo $comment; ?></textarea>
</p>
<!-- The following field is for robots only, invisible to humans: -->
<p class="robotic" id="pot">
<label>If you're human leave this blank:</label>
<input name="robotest" type="text" id="robotest" class="robotest" />
<p>
<img class="img-responsive" src="captcha.php" />
<br>
<input name="captcha" placeholder="Enter Captcha" type="text" />
</p>
<p>
<input type="submit" name="add" value="Add Feedback">
<p>
<?php
// Show validation errros here
if(!empty($error)){
echo "Please check the following fields<br>";
echo implode($error, ', ');
}
if(!empty($error2)){
echo "<br><br>";
echo implode($error2, '<br>');
}
echo $status;
?>
</p>
</p>
</form>
</section>
【问题讨论】:
-
伙计,你没有考虑应该如何使用 smarty。这不仅仅是为了让您可以找到/替换所有 php 标记。您应该向 smarty 模板提供它需要的所有变量,并让它发挥作用。你根本不应该有任何 {php} 代码。我相信最新的聪明人默认情况下不允许它们。更不用说您的代码非常不安全,尤其是对于 SQL 注入。
-
您的验证码还应该依赖于机器人想要填写字段的事实。如果您有一个名为robotest 的字段,它可能不会被填充,那么它的意义何在。您应该使用他们感兴趣的名称,例如“地址”。抱歉,我没有回答您的问题,但您的代码块很难阅读并且包含许多奇怪的编程。
-
我知道关于验证码的问题,这是正在用另一种方法解决的过程中还有什么是 sql 的问题?奇怪的编程是什么意思,什么你看不懂?
-
基本上我只打算使用 smarty,因为在这种情况下我还不明白如何使用它,因为看起来我没有其他选择,因为它没有显示页面应该当我只是在tpl里面的相关div中包含代码的页面 php代码基本上如果表单提交检查所有相关字段是否设置如果没有输出错误,如果是输入数据到sql并返回到页面主页顶部此代码还引用了其他代码的其他部分,这些部分显示了 sql 中的数据和 marquee 的它
-
如果你像这样使用 smarty,你不妨坚持使用 PHP 作为模板语言。没有区别。