Orada bu örnekte yanlış trilyonlarca şey muhtemelen, ama size doğru parça aşağı almak gerekir :)
dosya a.php
<html>
<body>
<script type="text/javascript" src="stuff.js"></script>
Data to process please: <input type="text" name="datatoprocess" id="datatoprocess" />
<br />
<input type="button" value="Do Something" onclick="dostuff();" />
<hr />
<div id="resultarea">
</div>
</body>
</html>
dosya b.php
<?php
$message = '';
if ( isset( $_REQUEST['datatoprocess'] ) ) {
$datatoprocess = $_REQUEST[ 'datatoprocess' ];
if ( strlen( $datatoprocess ) > 0 ) {
if ( $datatoprocess == 'foo' ) {
$message = '<span style="color:#ffff00;background:#ff0000;">bar</span>';
} else {
$message = 'You sent me: "'.htmlentities($datatoprocess).'"';
}
} else {
$message = 'That is really short data.';
}
} else {
$message = 'No data to process in request?';
}
echo $message;
?>
dosya stuff.js
// poor man quick and dirty ajax :|
function dostuff() {
var xmlHttp=GetXmlHttpObject();
if (xmlHttp==null) {
alert ("Browser does not support HTTP Request.");
return;
}
var datatoprocess = document.getElementById('datatoprocess').value;
var url="b.php"
+"?datatoprocess=" + escape(datatoprocess)
;
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
var AjaxAnswer = xmlHttp.responseText ;
document.getElementById('resultarea').innerHTML = AjaxAnswer;
}
};
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function GetXmlHttpObject() {
var objXMLHttp=null;
if (window.XMLHttpRequest) {
objXMLHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}