我在我的index.php:
<?php
$sContent.='<script src="https://code.jquery.com/jquery-1.12.4.js"></script>';
$sContent.='<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>';
$sContent.='<script src="in.js" type="text/javascript"></script>';
$id=2;
$sContent.='<div id="agree">Agree to terms: <input type="checkbox" id="agreed" value=1></div>';
$sContent.='<br><br><div class="show"></div>';
if($_GET['f'] == 'showInfo')
{
$sContent.= 'This is the info about item id: '.$_GET['id'];
}
$sAction = $_SERVER['PHP_SELF']."?id=".$id."&f=showInfo";
$sDest.= '<br><input class="'.$id.'" type="button" id="'.$id.'" name="'.$id.'" value="Click to show info about item '.$id.'" onClick="javascript:showInfo(\''.$sAction.'\');">';
$sContent.= $sDest;
echo $sContent;
?>
在我的js档案里:
function showInfo (action)
{
var aryAction = action.split('?');
params = aryAction[1];
var theUrl = 'index.php';
alert( aryAction[1]);
$.ajax ({
url: theUrl,
data: params,
async:true,
success: function (data, textStatus)
{
$('.show').html (data);
}
});
}
这显示了div中包含类“show”的整个index.php。我不需要thant,我只需要‘这是关于项目id: 2’的信息,在那个div中,没有其他的。
我一直在阅读其他类似的文章,也许我不应该使用ajax函数。对怎么做有什么想法吗?
非常感谢!!
发布于 2016-11-05 06:31:00
您必须将HTML内容与字符串分开。
把这两件事都放在if
/ else
子句中,那么它就能工作了:
<?php
$sContent = '';
if($_GET['f'] == 'showInfo')
{
$sContent .= 'This is the info about item id: '.$_GET['id'];
}
else
{
$sContent .= '<script src="https://code.jquery.com/jquery-1.12.4.js"></script>';
$sContent .= '<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>';
$sContent .= '<script src="in.js" type="text/javascript"></script>';
$id=2;
$sContent .= '<div id="agree">Agree to terms: <input type="checkbox" id="agreed" value=1></div>';
$sContent .= '<br><br><div class="show"></div>';
$sAction = $_SERVER['PHP_SELF']."?id=".$id."&f=showInfo";
$sDest .= '<br><input class="'.$id.'" type="button" id="'.$id.'" name="'.$id.'" value="Click to show info about item '.$id.'" onClick="javascript:showInfo(\''.$sAction.'\');">';
$sContent .= $sDest;
}
echo $sContent;
https://stackoverflow.com/questions/40439309
复制相似问题