我有在我的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。我不需要这个,我只需要在div中写上" This is the info about item id: 2 "就行了。
我一直在阅读其他类似的帖子,也许我不应该使用ajax函数。有什么办法吗?非常感谢!
必须将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;