如何发送文本到服务器并操纵它 [英] How to send text to the server and manipulate it
问题描述
我知道一点的PHP,AJAX和放大器; JavaScript的(和jQuery) - 但因为我是一个有点初学者的我需要一些帮助来连接点对于这个简单的任务:
我想让用户sometext输入(可以说:我看到了太阳,太阳是如此漂亮,有光泽),我想这个文本发送到服务器,然后替换单词太阳 。月亮) - 它发回给用户,并写信给他:我看到了月亮,月亮是那么的漂亮,有光泽)
我只需要和帮助理解:
- 如何使用PHP我将文本发送到服务器。
- 如何操作它,并将其发送给用户。
我肯定有一个教程 - 我只是不知道如何寻找它,所以我问在这里 - 获得一个良好的开端
< PHP
如果(使用isset($ _ POST ['文本']))//只有一些文字被送到这个脚本执行
{
回声str_replace函数(太阳,月亮,$ _ POST ['文本']); //处理文本,打印
死(); //站脚本执行
}
?>
< HTML>
< HEAD>
<脚本>
$(文件)。就绪(函数(){//当文件完成加载
$('#发送)。点击(函数(){//当用户点击发送按钮
VAR文字= $('#文本)VAL(); //从输入字段中的文本
.post的$('',{文字:文本},功能(数据){//将文本发送到当前脚本作为一个所谓的后可变文本
警报(数据); //当我们接收到响应的警告窗口中显示它
});
});
});
< / SCRIPT>
< /头>
<身体GT;
<输入类型=文本ID =文本/>
<输入类型=按钮值=发送ID =发送/>
< /身体GT;
< / HTML>
I know a bit php, ajax & javascript (and jQuery) - but since I am a bit of a begginer I need a little help to connect the dots for this easy task:
I want to let the user enter sometext (lets say: "I saw the sun, the sun is so nice and shiny") and I want to send this text to the server and then replace the word "sun" with "moon") - send it back to the user and write to him: "I saw the moon, the moon is so nice and shiny").
I just need help with understanding:
- How do I send the text to the server using php.
- How do I manipulate it and sends it back to the user.
I am sure there is a tutorial for it - I just didn't know how to look for it so I am asking here - to get a good start.
<?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>
这篇关于如何发送文本到服务器并操纵它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!