2014-04-30 76 views
0

我用下面的代碼來檢測「ENTER」鍵和文字輸入的值發送到PHP頁面:保存消息到一個文本文件中使用Ajax

$(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> 

事件雖然代碼有點凌亂它的工作原理,但txt文件得到了很多的不確定行更新:

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

感謝您的寶貴意見。

回答

1

問題是,如果它不是代碼13(= Enter),則觸發ajax事件。

theval只定義如果它你輸入(代碼13) - 否則它是undefined ;-)。修改成:

$(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!"); 
       } 
      }); 
     } 
    }); 
} 

,它應該正常工作;-)) - 雖然未經測試...

+0

enter key您的Ajax代碼謝謝你,這解決了這個問題! – user3588220

+0

不客氣;-) – thedom

1

嘗試像

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!"); 
     } 
    }); 
} 
0
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"); 
      } 
     }); 
    } 
    }); 
} 
相關問題