user3588220
user3588220

Reputation: 3

Save a message to a text file with Ajax

I use the following code to detect an "enter" key and send the value of a text input to a php page:

$(document).ready(LoadMe);

function LoadMe()
{


$('#test').bind('keypress', function(e){
   var code = e.keyCode ? e.keyCode : e.which;
   if(code == 13) // Enter key is pressed
   {
        var theval = $(this).val();
        var sessName = '<?php echo $_SESSION['username']?>';
        //alert(theval);
        $('<p>'+sessName+': '+theval+'</p>').appendTo('#content');
   }
        $.ajax({
                type: "GET",
                url: "savechat.php?msg="+theval,
                //data: { value : theval },
                success: function(data)
                {
                    //alert("success!");
                }
            });
});
}

savechat.php

// echo $_GET['msg'];
    $file = 'people.txt';
    $current = file_get_contents($file);
    $current .= $_GET['msg']."\n";
    file_put_contents($file, $current);

html:

<input id="test" type="text" >
<div id="content"> </div>

Event though the code is a bit cluttered it works but the txt file gets updated with a lot of undefined lines:

undefined
undefined
1
undefined
12
undefined
123
1234
undefined
123456
1234566
12345667
123456678
undefined
undefined
undefined
undefined
undefined
undefined
undefined
undefined
r

Thanks for your valuable feedback.

Upvotes: 0

Views: 75

Answers (3)

Ben
Ben

Reputation: 101

Try this

$(document).ready(LoadMe);

function LoadMe()
   {
       $('#test').bind('keypress', function(e){
       var code = e.keyCode ? e.keyCode : e.which;
        if(code == 13) // Enter key is pressed
    {
        var theval = $(this).val();
        var sessName = '<?php echo $_SESSION['username']?>';
        //alert(theval);
        $('<p>'+sessName+': '+theval+'</p>').appendTo('#content');

        $.ajax({
            type: "GET",
            url: "savechat.php?msg="+theval,
            //data: { value : theval },
            success: function(data)
            {
               alert("success");
            }
        });
    }
   });
 }

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try your ajax code in enter key like

if(code == 13) // Enter key is pressed
{
    var theval = $(this).val();
    var sessName = '<?php echo $_SESSION['username']?>';
    //alert(theval);
    $('<p>'+sessName+': '+theval+'</p>').appendTo('#content');   
    $.ajax({
         type: "GET",
         url: "savechat.php?msg="+theval,
         //data: { value : theval },
         success: function(data)
         {
              //alert("success!");
         }
    });
}

Upvotes: 1

thedom
thedom

Reputation: 2518

The problem is that you're triggering the ajax event if it is not code 13 (= Enter).

theval is only defined if it you hit enter (code 13) - otherwise it is undefined;-). Change that to:

$(document).ready(LoadMe);

function LoadMe()
{
    $('#test').bind('keypress', function(e){
        var code = e.keyCode ? e.keyCode : e.which;
        if(code == 13) // Enter key is pressed
        {
            var theval = $(this).val();
            var sessName = '<?php echo $_SESSION['username']?>';
            //alert(theval);
            $('<p>'+sessName+': '+theval+'</p>').appendTo('#content');

            $.ajax({
                type: "GET",
                url: "savechat.php?msg="+theval,
                //data: { value : theval },
                success: function(data)
                {
                    //alert("success!");
                }
            });
        }
    });
}

and it should work fine ;-)) - Although untested...

Upvotes: 1

Related Questions