gpt4 book ai didi

php - 如何避免ajax响应文本中的html dom代码

转载 作者:行者123 更新时间:2023-11-29 23:39:07 25 4
gpt4 key购买 nike

当我使用 jquery ajax 从数据库获取数据时,响应文本也包含 html 代码。到目前为止我的代码是:

$("#onclick").click(function() {
$.ajax({
async:"true",
type:"POST",
url:"process.php",
dataType: "text",
data:"tempid="+tid,
success:function(dat) {
$("#templ").html(dat);
}
});
});

文件process.php仅包含以下代码

include "connect.php";
if(isset($_POST['tempid'])&& $_POST['tempid']>0)
{
$temp_id= $_POST['tempid'];
$sql="SELECT template FROM templates WHERE tid='".$temp_id."'";
$res= mysql_query($sql);
if((mysql_num_rows($res))>0)
{
While($row = mysql_fetch_array($res))
echo '<b>'.$row['template'].'</b>';
}
else
{
echo 'NOT';
}
}

我得到的输出是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
<b>/* The mysql value */</b>

我的代码有错误吗?我只想在文本框中显示 id templ 的 mysql 值。如何避免响应中出现所有 html 代码?如果需要任何进一步的详细信息,请告诉我..

最佳答案

只需在 process.php 中回显之前放置 ob_clean(); 即可解决您的问题,但我非常同意 Quentin 的观点

关于php - 如何避免ajax响应文本中的html dom代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26271685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com