PHP不起作用


PHP does not work

我有一个代码如下:

jQuery.post("test1send.php", { browName : browser, timeCan: testTimeCanvas , timeSVG: testTimeSVG });
window.location.href = 'http://localhost/start/prosteTest2.html';

.php文件看起来像:

<?php
 $browName = $_POST['browName'];
 $timeCan = $_POST['timeCan'];
 $timeSVG = $_POST['timeSVG'];
    if($browName=='Opera'){
    $rowsCount = count(file('Data'Test1'Test1Opera.csv'));
    $fp = fopen('Data'Test1'Test1Opera.csv', 'a');
    fwrite($fp, $rowsCount);
    fwrite($fp, ',');
    fwrite($fp, $timeCan);
    fwrite($fp, ',');
    fwrite($fp, $timeSVG);
    fwrite($fp, "'r'n");
    fclose($fp);
    }else if($browName=='Firefox'){
    $rowsCount = count(file('Data'Test1'Test1Firefox.csv'));
    $fp = fopen('Data'Test1'Test1Firefox.csv', 'a');
    fwrite($fp, $rowsCount);
    fwrite($fp, ',');
    fwrite($fp, $timeCan);
    fwrite($fp, ',');
    fwrite($fp, $timeSVG);
    fwrite($fp, "'r'n");
    fclose($fp);
    }
 ?>

它应该将变量发送到php并加载下一页。PHP应该将变量保存到.csv文件中,但它不起作用。若并没有"window.location.href",它可以正常工作,但当有命令转到下一页时,PHP什么也不做。先做PHP然后转到下一页需要一些延迟吗??

将window.location.href放在帖子的成功回调中-http://api.jquery.com/jQuery.post/

jQuery.post("test1send.php", { browName : browser, timeCan: testTimeCanvas , timeSVG: testTimeSVG }, function(){ window.location.href = 'http://localhost/start/prosteTest2.html'; });

是的,它需要一个延迟。当您执行window.location.href = ...时,您会告诉浏览器在有时间处理之前取消当前请求。

最简单的可能是

jQuery.post("test1send.php", { ...}).done(function() {
    window.location.href = ...
});

尽管存在许多等效的技术。如果帖子可能会失败,您可能也希望使用.fail()来处理它。