2

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?

kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27
RBk
  • 189
  • 1
  • 1
  • 7
  • it s look like ok, what do you see in the POST & header in the network – MouradK Jul 09 '15 at 14:47
  • Try removing the quotes in `'text_string'` and name it to something else like `this_string`. Then change the `$_POST` in PHP to `$_POST['this_string']` – frz3993 Jul 09 '15 at 14:48
  • How do you open the php page to get the `Undefined index: text_string`? – Alvaro Montoro Jul 09 '15 at 14:50
  • No change in error when I used this_string. – RBk Jul 09 '15 at 14:52
  • If you're trying to open "process.php" in the browser, this is expected behaviour - as `$_POST['text_string']` is no longer defined when you load the page after the AJAX request finishes. If you want to persist that value for future usage, consider [storing it in a session variable in your PHP](http://stackoverflow.com/questions/9650930/how-to-update-session-variable-with-jquery-ajax-is-it-even-possible), and reading that when you try to load the page later. – Serlite Jul 09 '15 at 14:54
  • what about userName and text val vars? if you alert them? it's good thing to declare text_string in javascript adding var before. – Luca Olivieri Jul 09 '15 at 14:55
  • I'm using wamp server for the whole project, so everything is in localhost directory. So I have tabs ready for process.php and home.html (the static html with jquery function) and refresh them. First I refresh home and add data. Then I get those alerts. Then I switch to another tab and open process.php . It shows the error I mentioned in the description. – RBk Jul 09 '15 at 14:55
  • @LucaOlivieri the alerts are not the problem, they're showing correct values – RBk Jul 09 '15 at 14:58
  • @RijurekhBose how do you want to read the variable without passing it? And even if you passed it, you are reading the $_POST but if you open the file from the browser you should be reading $_REQUEST or $_GET. If you want to keep the value of the variable from the AJAX call, you should use sessions as suggested by Serlite – Alvaro Montoro Jul 09 '15 at 14:59
  • @Serlite I'm using a browser (FF) to access localhost (that's where my project is) – RBk Jul 09 '15 at 15:00
  • @AlvaroMontoro so I can't read the variable when I separately open process.php ? Should I enter a $.get to check whether the variable is being stored? – RBk Jul 09 '15 at 15:02
  • if you try to change success: function( data ){ alert(data); } what response you have? – Luca Olivieri Jul 09 '15 at 15:02
  • 1
    You can read it, but you need to pass it. If you directly open "process.php" on the browser, you'll get that error as you are not passing text_string anywhere. If you open "process.php?text_string=value" you'll be passing the variable, but you'll still get that error too because you are reading the $_POST but passing it through the $_GET – Alvaro Montoro Jul 09 '15 at 15:05
  • @LucaOlivieri (username=bbbbbbbbb textval=eeeeeeeeeeeee) Server string submitted is

    bbbbbbbbb:eeeeeeeeeeee

    – RBk Jul 09 '15 at 15:06
  • @AlvaroMontoro Can you suggest a $.get function such that it adds(appends) text_string to end of div (id="#here). This way I can see if it's working properly or not. – RBk Jul 09 '15 at 15:12
  • @LucaOlivieri well I don't want the body and html tags, just the text_string, for abovementioned reason in above comment – RBk Jul 09 '15 at 15:12
  • Use `$_REQUEST['text_string']` instead of `$_POST['text_string']`. And try opening `process.php?text_string=test` in the browser – Alvaro Montoro Jul 09 '15 at 15:14
  • ok you don 't need tags in php file. Then you can echo only $text_string and in jquery : success:function(data){ var variabile=data; } and do what do you want with text string – Luca Olivieri Jul 09 '15 at 15:18
  • The answer written by @Serlite works, I just wanted to check whether data was being sent correctly or not. For project future, I plan to use the value stored in the php file to append my html. Also for multiple users, would appending it work? Or is a database absolutely required for storing the text_string when multiple users will use html file? – RBk Jul 09 '15 at 15:23
  • If you're planning to store data for a dynamic number of users, I'd definitely suggest going the database route. Things will get really messy if you try to rely solely on the session for that (and it won't persist after the session ends, something you'd probably need for a user list). You could probably have two pages for that too, eg. "StoreUser.php" and "ViewUsers.php" to keep your code more organized. – Serlite Jul 09 '15 at 15:28

1 Answers1

0

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.

Serlite
  • 12,130
  • 5
  • 38
  • 49
  • Yup this worked! So that means it is sending the value. – RBk Jul 09 '15 at 15:16
  • @AlvaroMontoro is this what you were suggesting? Also I plan to use database in the future – RBk Jul 09 '15 at 15:18
  • Yeah! Everything was working fine in your code - just, you had no way to store the value you passed between page loads. Was this what you wanted? – Serlite Jul 09 '15 at 15:18
  • Yes, wanted to check if the data was being sent. Please check question comment thread for my another query. – RBk Jul 09 '15 at 15:24