5

I'm new to PHP programming and I wanted to know that is it possible to handle PHP events as we do in ASP.NET

I mean I've got a img and I want to perform some task on click event of this img.

I know how to do it in ASP.NET but please help me in context of PHP

Thanks, GURU

1
  • By "ASP.net" you mean webforms? Commented Mar 21, 2010 at 12:33

1 Answer 1

14

PHP itself does not handle client-side events. And PHP paradigm slightly differs from ASP.NET where client and server-side scripds bound together in the same page of code. On client-side, use javascript to handle onClick event, and in event-handler code issue AJAX call to your designated PHP page to send back a response.

<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>

And this is a test.php code:

<?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']));
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks dear for your information about clarification between ASP.NET and PHP but could you give me a simple example regarding AJAX to call a function(function name is myfunction) on a PHP File(myfile.php).
Wonderful answer, Thanks again for your co-operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.