Hello friends,

I want to call a php function on the onBlur event of a text box .
The code is as follows..

<?php

session_start();

if (empty($_SESSION['username'])){

header("location:login.php");

exit;

}
function show(){
	echo "HHHHHHHHHIIIIIIII";
}

?>

and the html code is

<input type="Text" name="timestamp" value="" onBlur="show();">

But the code does not work.
Any help how can I do this?
Or if someone could guide me how to call a php function from a javascript function,that too would help.:o

    PHP is a server side language meaning that the HTML cannot directly interact with the PHP as you want. What you want is a Javascript script that will be called on the event you wish. So onBlur="show();" is actually trying to fire the javascript function "show()".

    One thing you could do is make an ajax call that runs your specific function and/or convert your PHP to javascript and save some time (but lose some functionality if JS is disabled).

    Now, when the user submits the form, you should never assume that the javascript did it's job and you should always revalidate user-input.

      Ok i keep this in my mind.
      Actually I want to calculate the difference between two dates on onBlur event of a textbox and i have a php class that does it fine(includes the holidays between the dates).
      Can you suggest any javascript code to do this??
      Any help in this thing would be very helpful.πŸ™‚

      And also thanks for the replyπŸ™‚

        AJAX would be the easiest way. Just send an AJAX request to your server to let PHP do the work. Javascript dates can be finicky and more work if you don't know what you're doing. But by utilizing a javascript library like Prototype or jQuery, you can easily boil this down to a few lines:

        $('some-element-id').observe('blur', function(evt) {
            evt.stop(); // stop event bubbling
        
        new Ajax.Request('/some/url/to/post/to', {
            method: 'post', // use POST instead of GET
            params: $('form-id').serialize(), // serialize the data to be sent in the request (the entire form)
            onSuccess: function(tx) {
                alert('Done!');
                alert(tx.responseText);
            },
            onFailure: function(tx) {
                alert('Failed check.');
            }
        });
        });

        Or for jQuery:

        $('#some-element-id').bind('blur', function(evt) {
            $.post('/some/url/to/post/to', { 
                    $('#form-element-id').attr('name') : $('#form-element-id').val(), 
                    $('#form-element-id2').attr('name') : $('#form-element-id2').val() 
                }, 
                function(data) {
                    alert('Done');
                    alert(data);
                }
            });
        });

        Very similar code, but use the one you like. You could also use Dojo or MooTools or whatever JS library you wish.

          πŸ™‚
          Thank you very much. It worked the way you said using AJAX. It was tough for me as I did not know AJAX...bur after a reading through the tutorials I happily implemented it.
          Thank you very much for the reply and help.πŸ˜ƒ

            No problem. Don't forget to mark this thread as resolved if it is.

              I meant in "Thread Tools" there's a links to "Mark this thread resolved" which I will do for you πŸ˜‰

                bpat1434,

                Thanks buddy. i am new to this forum s/w hence could not figure it out that I had to use thread tools.Thanks again:p

                  Write a Reply...