Axschech
Axschech

Reputation: 295

PHP / Ajax code not working

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?

Upvotes: -1

Views: 96

Answers (2)

Axschech
Axschech

Reputation: 295

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

Upvotes: 0

cssyphus
cssyphus

Reputation: 40028

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();

Upvotes: 1

Related Questions