weixin_33739523 2013-11-01 18:24 采纳率: 0%
浏览 14

带有引导程序的eldarion ajax

Does anyone use eldarion ajax plugin? formerly known as bootstrap ajax...

I have it working where it sends the call to the other page and its successful, I just cant get the data back from that page.

 // this is ajax.php
 <form method="post"  action="ajax-here.php" class="ajax" data-append="#result">
 <input type="text" name="text" />
 <input type="submit" />  
 </form>
 <div id="result">  </div>

 // this is ajax-here.php
 $text = $_POST['text'];
 echo $text;

All I want is to have the result of the ajax-here.php to be displayed in the div with the ID result on the page where the ajax is happening. Any suggestions would be great. I like using this plugin because it works nicely, just don't know how to get the data to append to the div that I want. Thanks

  • 写回答

3条回答 默认 最新

  • weixin_33693070 2013-11-08 08:30
    关注

    I am not familiar with the Eldarion AJAX plugin. I've just read the README, and to be honest I don't like it. (Though it does win points for having a cool name.)

    There's too much "magic" that goes on. You don't get to specify the format in which you send and receive data. If you want to change things, you have to do so with HTML data- attributes. To me, violates the principle of separation of concerns, i.e. that each part of the structure of the site should have one purpose. For instance, the HTML should define the structure of the page, the CSS the styling, and the Javascript the interactivity. To have HTML attributes defining AJAX activity seems absurd to me.

    But no matter. Let's look at the specific question you have, which is how to return the data to Eldarion. Let's look at the README file again:

    Easily extend support on the server side code for this by adding a top-level attribute to the JSON you are already returning called "html" that is the rendered content. Unlike a backbone.js approach to building a web app, eldarion-ajax leverages server side template rendering engines to render and return HTML fragments.

    So Eldarion expects to receive a JSON structure, with a top-level property called html that contains the rendered content. So it expects a structure approximately like this:

    {
        "html": "<p>The content that should be appended to #result.</p>"
    }
    

    So let's do that with your content. Let's not mess about trying to make the JSON ourselves. It's too difficult to do right and far too easy to make mistakes. Fortunately, PHP comes to our aid with the json_encode function, which encodes an array as a valid JSON structure for you.

    $text = $_POST['text'];
    $data = array(
        'html' => $text
    );
    echo json_encode($data); // output the data as JSON
    

    I've set up a jsFiddle example that shows how this works. Obviously it's a bit different, because I had to use the jsFiddle API, but you can see approximately how it works.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算