I am new to PHP, and have a simple calculator implemented to perform basic calculations. The result(or when a button clicked, the respective number) is displayed in the input <input type="text" id="textbox" value="<?php echo $result ?>" name="result"/>
.
I have created a clear button using <input value="Clear" type='submit' onclick='document.getElementById("textbox").value="";' />
I want to implement a button similar to 'backspace' key in keyboard, which when clicked once, would clear one character in the input field. Is it possible in PHP? Please help me out with the code. tq for suggestions:)
It would likely make more sense to do this on the client side using JavaScript, but if you really wanted to do it in PHP you could use:
$new = substr($old, 0, -1);
You can do this via JavaScript
function delete_num ()
{
var field = document.getElementById('textbox');
field.value = field.value.slice(0, -1); //Extract from index 0 to the before-last character
textbox.pop(); //Remove the last element from the number. It's length is maintained by js itself.
return false;
}
If you want to do this with PHP you'd need to make an HTTP Request to the server, but that's pretty redundant.
You could do this through jQuery doing something like this:
var text = $('input').val();
$('input').val(text.substring(0, (text.length -1)));
Here is an example.