2013-11-20 82 views
-2

我有一个看起来像这样的代码:PHP不工作

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,然后转到下一页?

+3

可能要考虑更改您的标题。尽管它引起了我的注意。 –

+0

我知道那个标题是错误的,但我不知道我该怎么称呼它 – Jake

+1

在您的AJAX请求成功时调用window.location.href。我认为您可能遇到的问题是发送之前的重定向,因为您的代码布局方式不同。喜欢的东西: '$阿贾克斯({ 类型: “POST”, 网址: “http://we.com/”, 数据:{somedata:datahere}, 成功:函数(){ 窗口。 location.href =“你想要他们去的地方”; } });' – Cameeob2003

回答

3

将在后成功回调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'; }); 
+0

它就像你说的那样工作。 – Jake

1

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

最简单的事情很可能会

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

虽然有一些相当的技术。如果该帖子可能失败,那么您也可以使用.fail()来处理该帖子。

+0

在最近的jQuery版本中,还有一个'.always()'回调,完成时触发,无论是成功还是失败。 http://api.jquery.com/jQuery.post/ – showdev