2

I am trying to execute a simple PHP script on click of a <div>. I've searched around SO and some similar questions appear, but all deal with posting data when I have no data to post.

<div name="logout">
    logout
</div>
<script>
    $(document).ready(function() {
        $('div[name="logout"]').click(function() {
            $.ajax({
                url: './scripts/logout.php',
                dataType: 'json'
            }).done(function(result) {
                alert(result.message);
            });
        });
    });
</script>

logout.php is as below:

if (isset($_SESSION["username"]) && (isset($_SESSION["logintoken"]))) {
    session_destroy();
    $jsonArr = array('success' => true, 'message' => "You've logged out.");
    echo json_encode($jsonArr);
} else {
    $jsonArr = array('success' => false, 'message' => "You're not logged in.");
    echo json_encode($jsonArr);
}

No alert is made on click and console shows no errors. Adding a .fail condition to the AJAX call pops every time, but again nothing is in console.

All other times I use AJAX it's to handle forms, not quite this exact circumstance. I have no data to post or get, and most solutions on SO say to add a data: {} bit to the AJAX call, but I'd have nothing to put here.

What have I done wrong here? Tracing through my code, I figured maybe I was reinitializing $_SESSION variables, but I haven't.

4
  • are you sure that the url you're requesting does not return an error / contains valid json? this could be a reason Commented Aug 31, 2015 at 20:10
  • You do not have to post or get anything. You can post/get to server without any data. Commented Aug 31, 2015 at 20:10
  • 1
    Try viewing the request in the network tab of the developer tools. That will show you if you are having trouble with the request and not a JS error. Just a shot in the dark, but are you rewriting urls at all? This is the sort of thing you see when you are using relative paths to files in the ajax url. You likely need to use a full path or at least relative to the root. Rewriting a url like /post/{id}/ to post.php?id={id} will mean that the request is going to /post/{id}/scripts/logout.php and the file won't exist under that directory. Commented Aug 31, 2015 at 20:12
  • @JonathanKuhn everything looks kosher in the network tab. It's showing the correct path to the page and it's got the green circle icon with "OK" beside it. Commented Aug 31, 2015 at 20:15

2 Answers 2

2

Try this

<script>
    $(document).ready(function() {
        $('#logoutBtn').click(function() {
            $.ajax({
                url: './scripts/logout.php',
                dataType: 'json'
            }).done(function(result) {
                alert(result.message);
            });
        });
    });
</script>
<div id="logoutBtn">
    logout
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

<div> don't have name. That's for form fields. Try

<div class="logout">
      ^^^^^^^^^^^^
...
        $('div.logout').click(function() {
           ^^^^^^^^^^

instead.

1 Comment

that is not going to help. the css selector searches for a div with a name attribute. you can basically use any attribute name/value combination and access it using jquery or css selectors in general.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.