1

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

John
  • 61
  • 11
  • I would suggest you to use jQuery – Terence Jul 23 '13 at 09:40
  • It is possible with PHP, but I don't see why you want to use PHP when JavaScript seems more suitable for this. If you use PHP, it will be sent to a server, then you will return a field with one less character. This processing can be done on the client-side with JavaScript. – BLaZuRE Jul 23 '13 at 09:41
  • 2
    @John They are two totally different languages. PHP is server-side and JavaScript is client-side. You can use *both* separately, but you are thinking the wrong way if you want to use one inside the other. You can use an HTML page with CSS stylesheets, JavaScript client-side, and PHP server-side. – BLaZuRE Jul 23 '13 at 09:45
  • 1
    Before learning PHP, learn WHERE to use it, WHAT it should be used for, and WHEN to use javascript instead. After you mastered this, learn HOW to use PHP. – STT LCU Jul 23 '13 at 09:46
  • @John what BLaZuRE just said – Terence Jul 23 '13 at 09:46
  • @Terence I am in the process of learning PHP. I have implemented similar function using jQuery. (I don't know if jQuery can be used with PHP, can we?) – John Jul 23 '13 at 09:53
  • 1
    @John You seem to misunderstand the differences between what client-side and server-side language are. Try reading [this question](http://stackoverflow.com/questions/6369313/difference-between-javascript-and-php) to see if that helps. Also, jQuery is a library for JavaScript, in case that confuses you. – BLaZuRE Jul 23 '13 at 10:02

3 Answers3

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;
}
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
0

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);
Tom Walters
  • 15,366
  • 7
  • 57
  • 74
0

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.

MisterBla
  • 2,355
  • 1
  • 18
  • 29