If there is something written in the input text field and the " /> If there is something written in the input text field and the " /> If there is something written in the input text field and the "/>

In PHP how can I detect when a user is deleting text from an input text field

162 Views Asked by At
<form action="" method="POST">
<input type="text" name="phonenumber" value="" />
<form>

If there is something written in the input text field and the user start to delete the string meaning the last letter of the string I want to do something.

So something like this:

if(user deletes last letter of isset($_POST['phonenumber'])){
//do something
}

I tried to use strlen() with -1 but did not seem to work. Anyone have any ideas?

Thank you!

3

There are 3 best solutions below

2
Clarence On BEST ANSWER

You should use javascript to do something when user is deleting char. One way is to listen to 'keyup' event of the text input. E.g.:

<script>
$(":text[name=phonenumber]").on('keyup', function(event){
   if (event.keyCode == 8){ // means BACKSPACE pressed
        // do something
   }
});
</script>
1
Vivek Singh On

you can get it by using javascript. on every keyup you have to count the textbox value by id if it is decreasing from the max count means user is deleting some text..

2
Ahmed Ziani On

you should use javascript with event keyup ,

jQuery(function($) {
$('input[name=phonenumber]').on('keyup', function(e) {
       if(e.keyCode == 46)
       {
          $.ajax({
             type: 'POST',
             url : 'url',
             data :{'phonenumber': $('input[name=phonenumber]').val()},});
       }
});

});