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

7
  • I would suggest you to use jQuery Commented Jul 23, 2013 at 9: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. Commented Jul 23, 2013 at 9: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. Commented Jul 23, 2013 at 9: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. Commented Jul 23, 2013 at 9:46
  • 1
    @John You seem to misunderstand the differences between what client-side and server-side language are. Try reading this question to see if that helps. Also, jQuery is a library for JavaScript, in case that confuses you. Commented Jul 23, 2013 at 10:02

3 Answers 3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.