【发布时间】:2016-01-31 02:50:11
【问题描述】:
我正在制作一个网站,让您可以将信息提交到表单中并在单独的页面上查看。在该页面上,我希望用户能够按语言过滤数据。 带有下拉菜单的按钮选择一种语言:
<button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown">
Select a language <span class="caret"></span>
</button>
<ul class="dropdown-menu">
<li><a class="selectlang" id="arabic" data-language="arabic" href="#">Arabic</a></li>
...
...
<li><a class="selectlang" id="spanish" data-language="spanish" href="#">Spanish</a></li>
</ul>
</div>
这是我必须这样做的 jQuery:
$(".selectlang").click( function() {
var language = $(this).data('language'); //this gets the selected language from the dropdown. I want to use this var in the PHP to complete the query "SELECT * FROM slangdata WHERE language = ?"
$.ajax({
url: "test.php",
type: "post",
data: language,
success: function() {
$("#fill").load("test.php");
}
});
});
在 PHP 方面 (test.php),我真的不知道从哪里开始。我发现这个页面我认为可能会有所帮助 (PHP: Filtering SQL query with select options),但我不知道如何开始实施它。
这是我到目前为止所写的——它是从另一个示例中复制的,该示例使用一个函数来清理 PHP,并且最初有一个表,所以我尝试将它转换为与我的代码一起使用,但我真的不知道我在做什么' 这样做只是一团糟。
<?php include 'database.php'; ?>
<?php
function sanitizeMySQL($conn, $var) {
$var = strip_tags($var);
$var = mysqli_real_escape_string($conn, $var);
return $var;
}
if ( isset($_POST['language']) ) {
?>
<?php
$language = sanitizeMySQL($conn, $_POST['language']);
$query = "SELECT * FROM slangdata WHERE language = ?";
// code runs only if the statement was prepared
if ($stmt = mysqli_prepare($conn, $query)) {
mysqli_stmt_bind_param($stmt, 's', $language);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $language, $word, $pronunciation, $translation, $notes, $example, $nsfw);
// handle the data we fetched with the SELECT statement ...
while (mysqli_stmt_fetch($stmt)) {
printf ("<div class='%s'>", $nsfw);
printf ("<p><span class='word'>%s</span>", stripslashes($word));
printf ("<span class='pronounce'> %s </span></p>", stripslashes($pronunciation));
printf ("<p class='translation'>%s</p>", stripslashes($translation));
printf ("<p id='notes'>%s</p>", stripslashes($notes));
printf ("<p id='ex'>%s</p>", stripslashes($example));
printf ("<p class='%s'></p><hr></div>", $nsfw);
} // end while-loop
mysqli_stmt_close($stmt);
}
mysqli_close($conn);
} else {
?>
<p>Error</p>
<?php
} // end of if-else isset
?>
(或者,我有这段代码,我认为它可以替换我已经拥有的 while 循环 - 不确定哪个更好):
<?php while( $row = mysqli_fetch_assoc($run)) : ?>
<div class="<?php echo stripslashes($row['nsfw']); ?> entry <?php echo stripslashes($row['language']); ?>">
<p><span class="word"><?php echo stripslashes($row['word']); ?></span> <span class="pronounce"> <?php echo stripslashes($row['pronunciation']); ?> </span></p>
<p class="translation"><?php echo stripslashes($row['translation']); ?></p>
<p id="notes"><?php echo stripslashes($row['notes']); ?></p>
<p id="ex"><?php echo stripslashes($row['example']); ?></p>
<p class="<?php echo stripslashes($row['nsfw']); ?>"></p>
</div>
<?php endwhile; ?>
现在,当我选择一种语言时,它只是返回错误。很抱歉,这是一个很长的帖子,但我已经坚持了两天,真的不知道该怎么做。我怎样才能编写 PHP 让它做我想做的事?
编辑:删除的 cmets
【问题讨论】:
-
“当我选择一种语言时,它只会返回错误。” - 究竟是什么?
-
顺便说一句,你的脚本评论太多了
-
要在网页上显示详细的错误,请在代码开头添加
error_reporting(E_ALL); ini_set('display_errors', 'on');。否则,可以检查您的 apache 错误日志。 -
@Devashish 但对于初学者来说,这可能很有用
-
@Fred-ii- 我的意思是它正在运行我的 PHP 的“else”部分,告诉它显示
Error