too long

The code snippet for the jQuery function looks like:

function addMessage() {
    if (textval != "") {
        text_string='<div class="alert-box round"><p class="text-left">' + userName + ':' + textval + '</p></div></br>';
        alert(text_string);
        $.ajax({
            type:"POST",
            url:"process.php",
            data: {'text_string': text_string},
            cache:false,
            success:function(){
                alert("submitted")
            }
        });
        $("input[type=text]:last").val("");
    }
    enterButton = 0;
}

The process.php code looks like:

<body>
    <?php 
        //$host = "localhost";
        $text_string=$_POST['text_string'];
        echo "string submitted is".$text_string;        
    ?>
</body>

I get alerts showing value of text_string and then the "submitted", but when I open the php page, it shows an error:

Undefined index: text_string

I've seen various answers, none of them seem to be the case for mine. Is the problem in PHP code or jQuery code or both?

If you want to save the value passed by the AJAX request for the next time you load "process.php", try saving it in the session. So, you could change your code to:

<?php
    session_start();

    // Store value in session if it is passed
    if (isset($_POST['text_string'])){
        $_SESSION['text_string'] = $_POST['text_string'];
    }
    // Read and echo value from session if it is set
    else if (isset($_SESSION['text_string'])){
        $text_string=$_SESSION['text_string'];
        echo "string submitted is".$text_string;
    }
?>

Now, your PHP script will store the passed value in the session, and will echo that stored value should you load the page elsewhere. (Another alternative is to store the value in a database...though I'm not sure if you have one set up at the moment.)

Hope this helps! Let me know if you have any questions.