2012-01-20 193 views
0

在我的index.php文件中,我有一个AJAX函数,它将调用另一个php文件中的函数,该函数应该增加一个数字并在我调用AJAX函数时返回它。Php会话和Ajax

问题是数字从未改变。我尝试了很多不同的东西。很多人不幸地列出他们。

我的index.php文件。

<?php 
    session_start(); 
    $_SESSION['views'] = 0; 
?> 
<?php include 'blogFunction.php';?> 

<script type="text/javascript"> 
function doSomething() 
{ 
    $.ajax({ url: '/blogFunction.php', 
    data: {action: 'test'}, 
    type: 'post', 
    success: function(output) { 
    document.getElementById("blog").innerHTML = ''; 
       document.getElementById("blog").innerHTML = output; 
       } 
    }); 
    } 
</script> 

<div class ="blog" id = "blog"></div> 

我blogFunction.php

<?php 
     if(isset($_POST['action']) && !empty($_POST['action'])) { 
     $action = $_POST['action']; 
     switch($action) { 
      case 'test' : blogreturn();break; 
     } 
     } 

function blogreturn(){ 

     $_SESSION['views'] = $_SESSION['views']+ 1; 
     echo "THIS number is:" .$_SESSION['views']; 
} 
?> 

眼下输出始终是 '1',每当我打调用AJAX功能的按钮。

任何帮助表示赞赏。

实时代码:here

谢谢大家的帮助至今。一个问题出现了,出现了一个新问题。

扩展功能:

session_start(); 
if(isset($_POST['action']) && !empty($_POST['action'])) { 
$action = $_POST['action']; 

switch($action) { 
    case 'test' : blogreturn();break; 
} 

}

function blogreturn(){ 
    $request_url = "http://retrovate.tumblr.com/api/read?type=posts"; 
    $xml = simplexml_load_file($request_url); 

    $a = $_SESSION['views']; 
    $b = $_SESSION['views'] +4; 
    echo "A = ".$a; 
    echo "B = ".$b; 
    $_SESSION['views'] = $_SESSION['views']+ 1; 
    for ($i = $a; $i <= $b; $i=$i+1) { 
      echo '<h2>'.$xml->posts->post[$i]->{'regular-title'}.'</h2>'; 
      echo '<br>'; 
      echo $xml->posts->post[$i]->{'regular-body'}; 
      echo '<br>'; 
      echo '<br>'; 
    }  
} 

说就在这里,是一个问题,我在my site

单击我的按钮一次,它增加和显示新的内容。我再次点击,它恢复为0.如果我多次点击按钮很快,它似乎工作。似乎铬是有这个问题,而Firefox不是。

+0

为什么使用vanilla DOM方法输出AJAX的结果而不是jQuery? '$('#blog')。text(output);' –

回答

0

这里的正常工作代码... 的index.php

<?php 
session_start(); 
$_SESSION['views'] = 0; 
?> 
<!doctype html> 
<html> 
    <head> 
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js" /> 
    <body> 
     <div class ="blog" id = "blog"></div> 
     <input type="button" value="Add to the count!" id="call_ajax"/> 
     <script type="text/javascript"> 
      $('#call_ajax').click(function() { 
       $.ajax({ url: '/blogFunction.php', 
        data: {action: 'test'}, 
        type: 'post', 
        success: function(output) { 
         document.getElementById("blog").innerHTML = ''; 
         document.getElementById("blog").innerHTML = output; 
        } 
       }); 
      }); 
     </script> 
</body> 

blogFunction.php

<?php 
session_start(); 
    if(isset($_POST['action']) && !empty($_POST['action'])) { 
    $action = $_POST['action']; 
    switch($action) { 
     case 'test' : blogreturn();break; 
    } 
    } 

function blogreturn(){ 
    $_SESSION['views'] = $_SESSION['views']+ 1; 
    echo "THIS number is:" .$_SESSION['views']; 
} 
?> 

请注意,我不包括blogFunction.php索引.php文件!这很重要。

另一种方式,你每次加载页面时都将变量设置为0,这就是函数的调用方式(如果使用控制台调用它的话)。

我添加了一个按钮,让您点击通过Ajax调用函数(根据您在原始问题中的条件)。

希望有帮助!

+0

这有助于感谢。我在blogreturn功能中添加了更多功能,这些功能一直给我带来更多麻烦。如果你能协助我会很感激。 –

+0

你应该清理你的代码以避免糟糕的操作。 jQuery库在那里被调用至少三次,我可以看到,版本1.3,1.3.2和1.4。 当我将新的blogFunction()插入我的测试页面时,它工作正常(直到您用完博客文章)。它看起来像在你没有发布的PHP中存在一些错误。 – Tim

+0

是的,我同意,在过去几天里一直在尝试很多事情。很多代码已经被抛在后面。我现在添加了整个文件。 –

2

添加session_start();到blogFunction.php

+3

最好在脚本开始处;) – Polynomial

+0

Thanks.This解决了我最初的问题,从此出现了新的问题。 –

0

您需要调用session_start()在你的blogFunction.php文件。它必须在任何输出到浏览器之前被调用。可能最好的情况是在脚本中首先调用它。

-3

我认为你应该先取消设置$_SESSION['views']然后重新写入。

function blogreturn(){ 
    $temp = $_SESSION['views']; 
    unset($_SESSION['views']); 
    $_SESSION['views'] = $temp + 1; 
    echo "THIS number is:" .$_SESSION['views']; 
} 
+0

为什么,这会做什么? – afuzzyllama

+0

这没有什么不幸的。 –