0

Ok, so when I execute the initial function it works fine, the username gets stored in the database, however when I run the second function that appends the username to the text the user chooses to enter the IF statement returns 'no user' - when a user is defined...

If anyone knows how to fix this that would be great - I am currently learning PHP and mysql so I am sorry if any of this is incorrect

<?php

session_start()

    // connect to the database
    mysql_connect("localhost", "root", "");
    mysql_select_db("ajaxchat");



    // read the stage

    $stage = $_POST['stage'];

    // primary code
    if($stage == 'initial') {

        // check the username       
        $user = $_POST['user'];

        $query = mysql_query("SELECT * FROM chat_active WHERE user='$user'");
        if (mysql_num_rows($query) == 0) {

            $time = time();

            // 
            mysql_query("INSERT INTO chat_active VALUES ('$user', '$time')");

            // set the session
            $_SESSION['user'] = $user;

            echo 'good';
        }
        else {
            echo 'taken';
        }
    }

/////////////// PROBLEM FUNCTION ///////////////
================================================

    else if($stage == 'send') {
        // get the textdomain
        $text = $_POST['text'];

        // check for user_error
        if (isset($_SESSION['user'])) {
            $user = $_SESSION['user'];
            echo $user.' - '.$text.'<br />';
        }
        else {
            echo 'no user';
        }
    }
    else {
        echo 'error';
    }
?>

This is the javascript:

<script type="text/javascript">
    function chat_initialise() {
        var user = document.getElementById("chat_user").value;

        $.post("./chat.php", {stage:"initial", user:user}, function(data) {
            if (data == "good") {
                $('#initial').css('display', 'none');
                $('#content').css('display', 'inline')
            }
            else {
                alert("That username is taken! Please try another.");
            }
        });
    }

    function chat_send() {
        var text = document.getElementById("chat_text").value;

        $.post("./chat.php", {stage:"send", text:text}, function(data) {
            document.getElementById("chat_text").value = '';
            $('#window').text($('#window').text() + data);
            // alert(data)
        });
    }
</script>
18
  • Do you get the output: good ? Commented Feb 17, 2015 at 15:17
  • yes - gyazo.com/1933a147f866a4e057b3d48d06353abd @Rizier123 Commented Feb 17, 2015 at 15:18
  • 2
    Obligatory comment 1) you're vulnerable to SQL injection attacks and 2) don't use the mysql_ extension : stackoverflow.com/questions/12859942/… Commented Feb 17, 2015 at 15:23
  • 1
    Do you have a session_destroy() or a session_unset() somewhere after this code ? And check if SESSION is enabled. Commented Feb 17, 2015 at 15:26
  • 1
    else if($stage == 'send') { might be failing and is related to your POST in your form as per $stage = $_POST['stage']; which you should probably show us what it looks like. Add error reporting to the top of your file(s) right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); then the rest of your code, to see if it yields anything. Commented Feb 17, 2015 at 15:29

1 Answer 1

1

I fixed it - changed the POST function to take the current username then redefine it as a variable in the second function:

else if($stage == 'send') {
    // get the textdomain
    $text = $_POST['text'];
    $user = $_POST['user'];

    echo $user;

    // check for user_error
    if (isset($_SESSION['user'])) {
        $_SESSION['user'] = $user;
        echo $user.' - '.$text.'<br />';
    }
    else {
        echo 'no user';
        var_dump($_SESSION);
    }
}

Thanks for all your help guys!!

Sign up to request clarification or add additional context in comments.

4 Comments

Glad to know you've found your own solution, cheers and you're welcome ;-)
Essentially bypassing the session entirely by sending the user through again with the second AJAX call ... that's one way around it; I've still got this weird niggly feeling that you're losing your session here somewhere though ;)
@CD001 I'm sure it will break something else in the near future haha! I honestly don't know where it could be losing my session as the php in the question is literally the entire php script
yes you done that but it's not the correct way to use session and there is no user in chat_send function then how you can access the value of user in else if send part ?

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.