我制作了一个PHP代码,通过从JSON文件中获取信息来跟踪软件包,并将其作为Wordpress插件。但是每次搜索时,搜索结果都会显示在页面顶部。我可以将其显示在需要的位置吗?
<?php
$url = "a url where i get my json fille";
$code = $_POST[code];
$data = file_get_contents("$url$code");
$json = json_decode($data);
echo "<table class=\"table\">";
foreach ($json as $key => $row) {
echo "<tr>";
foreach ($row as $key0 => $row0) {
echo "<th>" . $key0 . " : " . $row0 . "</th>";
}
echo "</tr>";
}
echo "</table>";
?>
最简单的方法是使用简码。
// NB: you can name your function anything
function trackingplugin_print_table(){
// Not part of your code, but add it just in case
// This simply checks if user submitted anything
if( ! $_POST['code'] ) {
return;
}
$url = "a url where i get my json fille";
$code = $_POST[code];
$data = file_get_contents("$url$code");
$json = json_decode($data);
echo "<table class=\"table\">";
foreach ($json as $key => $row) {
echo "<tr>";
foreach ($row as $key0 => $row0) {
echo "<th>" . $key0 . " : " . $row0 . "</th>";
}
echo "</tr>";
}
echo "</table>";
}
// NB: you can also name your shortcode whatever you want
add_shortcode( 'trackingplugin', 'trackingplugin_print_table' );
[trackingplugin]
。在这种情况下,它将在表格之后。尝试进行Ajax调用并将结果加载到响应div。
在此处查看一些想法How to call ajax in wordpress
您可以将console.log(msg);
替换为jQuery('#response_div').html(msg);