drnf09037160 2013-04-21 03:11
浏览 133
已采纳

通过Wordpress中的XMLHttpRequest加载数据库内容

I have created a new wordpress-template-page. The idea is, that when I click on a link in my sidebar-div, it should load content from an database in my content-div. On a simple php-page it works, but in combination with my wordpress template-page it doesn't work...

And that's my code: (short version)

<?php // Template Name: Weinkarte
get_header(); ?>

<div id="container-sidebar">
     <span id="wineList"></span>
</div>

<div id="sidebar-right">
    <li><a href='#' id='1' onclick='loadWine(this.id)'>Click</a></li>
</div>

get_footer();

<script>
function loadWine(id)
{
xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("wineList").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","loadWine.php?landID="+id,true); //I think here is probably the fault
xmlhttp.send();
}
</script>

Thanks for any help! :)

  • 写回答

1条回答 默认 最新

  • douzhao4071 2013-04-21 04:45
    关注

    In wordPress, you have to use action for ajax call, something like this (basically in your functions.php)

    add_action( 'wp_ajax_nopriv_myAjaxFunction', 'myAjaxFunction' );  
    add_action( 'wp_ajax_myAjaxFunction', 'myAjaxFunction' );
    function myAjaxFunction(){  
        //get the data from ajax call  
        $landID = $_POST['landID'];
        // rest of your code
    }
    

    Also use post instead of get something like this in you javascript file

    var data = "action=myAjaxFunction&landID="+id;
    xmlhttp.open("POST","http://your_site.com/wp-admin/admin-ajax.php",true);
    xmlhttp.send(data);
    

    Given example is just an idea, you should read more about it and use jQuery for ease. You can read more on Codex and here is another nice article.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?