how to retain previous value of textbox and input new value in same textbox.please help me .i am tyring to make a calculator programm in php only withh all buttons etc .

Dani AI

Generated

As suggested, the usual pattern for a one-textbox calculator is to keep the current expression across form posts and re-send it on every submit. Each calculator button is a form submit; the server reads the posted expression and the pressed button, updates the expression (append, backspace, clear) and returns the new expression in the textbox value. This fits ’s requirement for a PHP-only calculator with one visible textbox that holds both input and the result.

A compact, working approach (visible textbox holds the expression; buttons submit values; server appends or evaluates):

<?php
$expr = isset($_POST['expr']) ? $_POST['expr'] : '';

if (isset($_POST['btn'])) {
    $btn = $_POST['btn'];

    if ($btn === 'C') {
        $expr = '';
    } elseif ($btn === 'BS') {
        $expr = substr($expr, 0, -1);
    } elseif ($btn === '=') {
        if (preg_match('/^[0-9\+\-\*\/\.\(\) ]+$/', $expr)) {
            // eval used only after strict sanitization and when input comes only from buttons
            eval('$result = ' . $expr . ';');
            $expr = (string)$result;
        } else {
            $expr = 'Error';
        }
    } else {
        $last = substr($expr, -1);
        if ($last !== false && in_array($last, array('+','-','*','/')) && in_array($btn, array('+','-','*','/'))) {
            $expr = substr($expr, 0, -1) . $btn; // replace last operator
        } else {
            $expr .= $btn; // append digit/operator
        }
    }
}
?>
<form method="post">
<input type="text" name="expr" value="<?php echo htmlspecialchars($expr, ENT_QUOTES); ?>">
<!-- buttons (examples) -->
<button type="submit" name="btn" value="7">7</button>
<button type="submit" name="btn" value="8">8</button>
<button type="submit" name="btn" value="9">9</button>
<button type="submit" name="btn" value="/">/</button>
<!-- add other buttons similarly -->
<button type="submit" name="btn" value="C">C</button>
<button type="submit" name="btn" value="BS">BS</button>
<button type="submit" name="btn" value="=">=</button>
</form>

Notes and tips: never echo raw input—use htmlspecialchars as shown. eval() is risky; it is only acceptable here after strict sanitization and when the page only accepts input from controlled buttons. For a safer long-term solution, replace eval() with a small expression parser or a math-eval library. Handle edge cases: prevent multiple decimal points per number, replace consecutive operators instead of appending, check divide-by-zero after evaluation, and consider using $_SESSION if the calculator state must persist across multiple pages or tabs.

while making calculator it is required and please help me..

well not quite sure what u mean but if u want to retain inputed value and at thesame time input another value without loosing previous value then u need to create a hidden field and for every input you entered u submit the form from there u can always grad your values. And why not provide two seperate input field instead of one?

,actually in calculator prigram we want only one textbox and put values and operator in same textbox anc after calculation i.e. result is also entered in same textbox so...there is again and again requirement of same textbox. and retaining value of previous to calculate result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.