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";   
}
?>
douhaodang0403
douhaodang0403 当被问及:2010年3月4日15:44,你还在寻找答案吗?
7 年多之前 回复

3个回答



PHP文件的输出将被放入成功回调中的“data”变量中。</ p>

此外,虽然我们在这里,你也可以快捷一些事情。 您可以使用对象来代替串联字符串以生成dataString(并希望没有人放置=或者&amp;):</ p>

  dataObj = {start:start, 结束:结束}; 
</ code> </ pre>

然后你只需将它传递给 .ajax </ code> </ p>
的数据属性</ DIV>

展开原文

原文

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

dongzi1209
dongzi1209 没有运气我害怕。 我认为这与您需要发起新的HTTP请求才能下载文件有关。 AJAX旨在规避什么?
10 年多之前 回复
duangu1868
duangu1868 ...或dataType:'text'
10 年多之前 回复
doukuangxun5382
doukuangxun5382 尝试设置dataType:'html',看看会发生什么
10 年多之前 回复
duanditang2916
duanditang2916 Hiya,谢谢 - 但是当我尝试输出数据变量时,没有任何反应 - 它没有意识到它应该是一个新的下载文件。 当我直接访问calendar.php但PHP标头启动并且浏览器下载文件时。
10 年多之前 回复



在ajax请求中我没有找到任何方法来处理重定向标头,所以我使用window.location代码进行下载操作url Ajax成功回调。 在你的情况下,除了显示一些进展,他们不应该需要发送ajax请求。 所以你可以直接执行window.location ='calendar.php?start ='+ start +'&amp; end ='+ end;。 </ p>

或者,如果您确定只希望通过POST执行它,请使用直接Form Post而不是使用Ajax。</ p>
</ div>

展开原文

原文

As in ajax request i did not found any way to work redirection headers, So i used window.location code for download action url in the Ajax success callback. In your case except showing some progress, Their should not be need for send ajax request. So you can directly execute window.location = 'calendar.php?start=' + start + '&end=' + end;.

Alternatively if you are sure that only way you want it to execute through POST, Use direct Form Post instead of using Ajax.



请检查以下代码提示。 这可能对你有帮助.. </ p>

  type:“POST”,

data:({“start”:&lt;?php echo $ start;?&gt;,“ 结束“:&lt;?php echo $ end;?&gt;}),
成功:function(){
//做某事
}
});
</ code> </ pre>
</ DIV>

展开原文

原文

Please check the following code hint. This may help you..

type: "POST",

data: ({"start" : <?php echo $start; ?> , "end" : <?php echo $end; ?>}) ,
success: function() {
                    //Do something
        }
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问