PHP kendisi istemci tarafı olayları işlemek değil. Ve PHP paradigma hafif istemci ve sunucu tarafı scripds kod aynı sayfada birbirine bağlı ASP.NET farklıdır. Istemci tarafında, onClick olay işlemek için javascript kullanmak ve belirlenen PHP sayfasına olay işleyicisi kodu sorunu AJAX çağrısına bir yanıt geri göndermek.
<script type="text/javascript">
var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
http = new XMLHttpRequest();
}
function click() {
http.open("GET", "test.php?name=" + document.getElementById("name").value, true);
http.onreadystatechange=function() {
if(http.readyState == 4) {
document.getElementById('foo').innerHTML = http.responseText;
}
}
http.send(null);
}
</script>
name:<input id="name" type="text">
<p><button onclick="click()">Click me</button></p>
<div id="foo">
Hell
</div>
Ve bu bir dnm.php kodu:
<?php
function validate($name) {
if($name == '') {
return '';
}
if(strlen($name) < 3) {
return "<span id=\"warn\">Username too short</span>\n";
}
switch($name) {
case 'bob':
case 'jim':
case 'joe':
case 'carol':
return "<span id=\"warn\">Username already taken</span>\n";
}
return "<span id=\"notice\">Username ok!</span>\n";
}
echo validate(trim($_GET['name']));
?>