我的jquery代码:
var getid;
var urllink;
$("li").click(function(){
getid = $(this).attr('id');
switch(getid){
case "brief":
urllink = "allbriefs.txt";
$.ajax({
url: 'showtext.php',
data: "urlname=" + urllink,
type: "POST"
})
break;
default: break;
}
})我的“showtext.php”代码:
<!DOCTYPE html>
<html>
<body>
<?php
$filename = $_POST["urlname"];
$keyword = $_POST["keyword"];
$myfile = fopen($filename,"r") or die("unable to open file");
echo fread($myfile, 120);
fclose($myfile);
?>
</body>
</html>html代码:
<p id="brief" > <?php include 'showtext.php' ?></p>这些错误导致“未定义索引: urlname in.”的错误。我不知道问题出在哪里。
发布于 2017-11-22 10:11:24
不要在html中包含php file,所以删除此行:
<p id="brief" > <?php include 'showtext.php' ?></p>ajax将自动调用该特定页面并在变量中返回其内容:
$.ajax({
url: 'showtext.php',
data: {urlname: urllink},
type: "POST",
success: function(result){
console.log(result); // showtext.php result
}
});如果你想达到你的例子的效果,你可以这样做:
HTML:
<p id="brief"></p>
JS:
$.ajax({
url: 'showtext.php',
data: {urlname: urllink},
type: "POST",
success: function(result){
console.log(result); // showtext.php result
$('#brief').html(result);
}
});https://stackoverflow.com/questions/47431667
复制相似问题