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>
no ajax needed
<?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>
if you want ajax, do this
<?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
}
?>
Google is your friend. I searched for "ajax for beginners jquery php". There are hundreds, thousands of resources to walk you through this very thing. Here's one tutorial that I found:
http://www.devirtuoso.com/2009/07/beginners-guide-to-using-ajax-with-jquery/
Try to google for ajax and jquery tutorials. Basically it would work something like this:
Get user text input and call a javascript function that will send that text to process.php page
//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
}
});
}
on the process.php page you take that text from a $_GET variable
//process.php
$sentText = $_GET['sentText'];
echo $sentText.' - some new text added';
精彩评论