-1

I can't figure out why this isn't working! The idea is the code is supposed to run a php script on another page that will check for changes in the DB state. Then it will return a string that will update the CURRENT page.

(External) PHP:

$QgetShift = mysql_query("SELECT * FROM shifts");

$num = mysql_num_rows($QgetShift);

if(isset($_POST['ajax'])) {
    if(isset($_SESSION['data'])) {

        $data = $_SESSION['data'];      
        if($data != $num){
            $_SESSION['data'] = $num;
            echo "WORKING";
        } else {
            echo "NOT WORKING";
        }
    } else {
        $_SESSION['data'] = $num;
        echo "started";
    }
}

HTML:

<button type="button" id="clickMe">Click Here</button> <br />
                <div id="data"></div>

Javascript:

$('#clickMe').click(function(){
  $.ajax({
    method: 'post',
    url: 'function.php',
    data: {
      'ajax': true
    },
    success: function(data) {
      $('#data').text(data);
    }
  });
});

Can anyone tell me if there is an error somewhere?

7
  • 1
    By not working, do you mean that you're getting an output of "NOT WORKING", or that the script is failing to run? Commented Aug 12, 2013 at 18:17
  • You need to use "GET", not "POST". Commented Aug 12, 2013 at 18:20
  • 3
    @Trendy There's nothing wrong with using POST here. Commented Aug 12, 2013 at 18:20
  • 2
    May be session_start() could be a problem you have to put this on the top of the file Commented Aug 12, 2013 at 18:24
  • For one thing, you success function is changing a DIV. It should read: $('#data').html(data); Commented Aug 12, 2013 at 18:29

2 Answers 2

1

This line in your AJAX success function is incorrect ($('#data').text(data);): Try instead:

success: function(data) {
    $('#data').html(data);
}

Another thing to try is to add this to the top of your function.php file (just for one test):

<?php
    echo 'Received OK from PHP';
    die();
Sign up to request clarification or add additional context in comments.

1 Comment

Weird... it's not pulling in that string. Any ideas?
0

Wow I'm dumb, the link was wrong :/

1 Comment

If any of the answers helped you to find the solution yourself, please upvote and select a correct answer. Otherwise, after the waiting period, please select your own answer as correct.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.