-2

Is there any way to handle events in php, I'm looking for a way to handle event's like posting a form. Thanks, Sreejith

sreejith
  • 779
  • 3
  • 11
  • 20

2 Answers2

4

The reason (probably) why people are confused by "PHP" + "events" is that there is no such thing as events in PHP technically speaking. PHP scripts receive "requests", and you can discern requests via the $_POST and $_GET global variables. (There is also the $_REQUEST variable, but it's use is... sometimes questionable.)

Anyhow, there is no concept of "form submission" in PHP either; this is a client-side concept. Simply because you can perform a POST or a GET request to a PHP script with or without going through a form element and, from a PHP point of view, you cannot tell the difference.

Also, since a PHP request is single threaded (to the extend of my knowledge at least), your "event" will probably look like this (for a POST request) :

if (isset($_POST['btn_submit'])) {
    // we suppose that the submit button was pressed and the form was sent
    // we suppose also that other data is also present in $_POST
    //
    // handle form post event here
}
Community
  • 1
  • 1
Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
3

Posting a form is not really an event: the page is just called (that's the only event you'll get as far as i know), but you can check for certain values to be present in $_GET, $_POST or the combined $_REQUEST, and act on them.

Nanne
  • 64,065
  • 16
  • 119
  • 163
  • Yes, I was actually looking to figure out a way if any event occurs when the server recieves the POST – sreejith Feb 05 '11 at 17:50
  • No real event, the server just receives a request for you page, but there is something in the `$_POST` variable.. and you can check for that ofcourse in your file. It's just your basic file-loading. As if you'd load the file without posting, or as if you're doing a reload. But then with extra data in those pre-mentioned vars. – Nanne Feb 05 '11 at 17:59