2011-07-27 5 views
2

私はちょっとphp、ajaxを知っています。& javascript(とjQuery) - しかし、私は初心者の方ですから、この簡単な作業のためにドットを接続するには少し助けが必要です。サーバにテキストを送信し、それを操作する方法

私はユーザーにsometextを入力させたいと思います(「私は太陽を見ました、太陽は素晴らしく光沢があります」)、このテキストをサーバーに送信してから「sun」という単語を"月") - それをユーザーに送り返し、彼に書いてください: "私は月を見ました、月はとても素敵で光沢があります")。

私は理解を助ける必要があります。

  1. 私はPHPを使用してサーバにテキストを送信するにはどうすればよいです。
  2. どのように操作してユーザーに送り返すのですか。

私は確かにそれのためのチュートリアルがあります - 私はここでそれを探す方法を知らなかったので、良いスタートを得るために。

答えて

1

は何AJAXを使用すると、AJAXをしたい場合は、この

<?php 
if ($_POST){ 
    if ($_POST['name']){ 
     echo str_replace('sun', 'moon', $_POST['name']); 
    } 
}else{ 
?> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script> 
<script> 
$("form").submit(function(){ 
     $.post('<?php echo $_SERVER['PHP_SELF']; ?>', {name:$("#name").val()},                             
       function(html){ 
        $("body").append(html); 
       } 
     ); 
     return false; 
</script> 
<body> 
<form method="post" action=""> 
<input type="text" id="name" name="name" /> 
<input type="submit" /> 
</form> 
</body> 
<?php 
} 
?> 
2
<?php 
     if (isset($_POST['text'])) //only execute if some text were sent to this script 
     { 
      echo str_replace('sun','moon',$_POST['text']); //manipulate the text and print it 
      die(); // stop script execution 
     } 
    ?> 
    <html> 
    <head> 
     <script> 
     $(document).ready(function(){//when document finished to load 
      $('#send').click(function(){//when user clicked on send button 
      var text = $('#text').val();//get the text from input field 
      $.post('',{text:text},function(data){ // send the text to the current script as a post variable called text 
       alert(data); // when we received the response display it in alert window 
      }); 
      }); 
     }); 
     </script> 
    </head> 
    <body> 
     <input type="text" id="text" /> 
     <input type="button" value="send" id="send" /> 
    </body> 
    </html> 
+0

助けてくれてありがとう! 1つの質問 -

タグ内の両方の入力を閉じてはいけませんか?そうでない場合 - 私はいつを使用し、私はそれを必要としないのですか? – Alon

+0

httpを使用してデータを送信する予定がある場合は、入力タグをformタグで囲む必要があります。 AJAXを使用している場合、それを行う必要はありません。しかし、それは怪我をすることはありません。また、スタイリングとセマンティクスのために、とにかくフォームタグを追加することもできます。 – Ivan

0

は、AJAXとjQueryのためのgoogleしてみます

<?php 
if ($_POST){ 
    if ($_POST['name']){ 
     echo str_replace('sun', 'moon', $_POST['name']); 
    } 
} 
?> 
<form method="post" action=""> 
<input type="text" name="text" /> 
<input type="submit" /> 
</form> 

を必要としませんチュートリアル。基本的にはこのような何かを動作します:

は、ユーザのテキスト入力を取得し、あなたからそのテキストを取るページにprocess.phpページで

//input page  
function getAjax(text){ 

     var $data = "sentText="+ text +"; 

      $.ajax({ 
      type: "GET", 
      dataType: 'text', 
      url: "process.php", 
      data: $data, 
      success: function(output){    
       $('#responseDiv').html(output); //output is the text echoed from the php page 
       } 
     }); 
    } 

をprocess.phpし、そのテキストを送信するJavaScript関数を呼び出します$ _GET変数

//process.php 
    $sentText = $_GET['sentText']; 

echo $sentText.' - some new text added'; 
関連する問題