duanqi5333 2010-03-04 15:44
浏览 77

JQuery - 通过AJAX调用将变量传递给PHP脚本,然后显示文件

I'm trying to generate Outlook event files for my events, doing so on the fly as and when someone requests it by pressing a link on the page.

Here's what i've got so far, but I can't find out how to get the browser to download the content which is returned.

I know how I could do this if I sent everything via _GET, but I'd prefer to do it via _POST, hence I'm going down this route..

Any thoughts? Thanks!

HTML / Javascript

<script>
$(function() {
    $(".button").click(function() {
        // validate and process form
        // first hide any error messages
        var start = $("input#start").val();
        var end = $("input#end").val();
        var dataString = 'start='+ start + '&end=' + end;
        $.ajax({
            type: "POST",
            url: "/calendar.php",
            data: dataString,
            success: function(data) {
                //Need to return the file contents somehow!
            }
        });
    return false;
    });
});
</script>
<form name="calendar" method="post" action="">
      <input type="hidden" name="start" id="start" value="<?php echo $start; ?>" />
      <input type="hidden" name="end" id="end" value="<?php echo $end; ?>" />
      <input type="submit" name="submit" class="button" id="submit_btn" value="Outlook" />
    </fieldset>
</form>

PHP File

<?php
if (isset($_POST['start'])) {
    $start = $_POST['start'];
    $end = $_POST['end'];
    $c = header("Content-Type: text/Calendar");
    $c .= header("Content-Disposition: inline; filename=calendar.ics");
    $c .= "BEGIN:VCALENDAR
";
    $c .= "VERSION:2.0
";
    $c .= "PRODID:-//xxxyyyy//NONSGML //EN
";
    $c .= "METHOD:REQUEST
"; // requied by Outlook
    $c .= "BEGIN:VEVENT
";
    $c .= "UID:". $start . $end ."-" . "-xxxxyyyy.com
"; // required by Outlook
    $c .= "DTSTAMP:".date('Ymd').'T'.date('His')."
"; // required by Outlook
    $c .= "DTSTART:20080413T000000
"; 
    $c .= "SUMMARY:" . "
";
    $c .= "DESCRIPTION:" .  "
";
    $c .= "END:VEVENT
";
    $c .= "END:VCALENDAR
";
    echo $c;        
} else {
    echo "Sorry you can't access this page directly";   
}
?>
  • 写回答

3条回答 默认 最新

  • dongza6247 2010-03-04 15:47
    关注

    The output of the PHP file will be put into the "data" variable in your success callback.

    Also, while we're here, you can also shortcut a few things. Instead of concatenating strings to generate the dataString (and hope that no one puts an = or & in there), you can use objects:

    dataObj = { start : start, end : end };
    

    And then you just pass that to the data property of .ajax

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图