PHP会话和Ajax

Within my index.php file I have an AJAX function that will call a function within another php file which should increment a number and return it whenever i call the AJAX function.

The problem is that the number never changes. I have tried lots of different things. Too many to list them all unfortunately.

My index.php file.

<?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>

my 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'];
}
 ?>

Right now the output is always '1' whenever i hit the button that calls the AJAX function.

Any help appreciated.

Live Code:here

Thank you all for the help so far. One problem down, a new problem appears.

Extended Functionality:

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>';
    }    
}

The problem that lies here, is, I click my button once at my site

and it increments and shows the new content. I click again and it reverts back to 0. If I click the button numerous times fast, it seems to work. It seems that chrome is having this problem whereas Firefox is not.

Add session_start(); to blogFunction.php

You need to call session_start () in your blogFunction.php file. It has to be called before any output to the brower. Probably best case would be to call it first in the script.

Here's the properly working code... 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'];
}
?>

Notice that I'm not including blogFunction.php in the index.php file! That's important.

The other way you had it, you were setting the variable to 0 each time the page loaded, which was how the function was called (if you used the console to call it).

I added a button for you to click to call the function via Ajax (per your conditions in the original question).

Hope that helps!

I think that you should first unset $_SESSION['views'] and than write again.

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