Layout in use: layout_alt_two

AJAX framework

This built-in JavaScript framework provides simple but powerful AJAX capabilities. Check out the calculator example below.

Calculator

  The HTML markup for this example:

<!-- The form -->
<form role="form" data-request="onTest" data-request-update="calcresult: '#result'">
    <input type="text" value="15" name="value1">
    <select name="operation">
        <option>+</option>
        <option>-</option>
        <option>*</option>
        <option>/</option>
    </select>
    <input type="text" value="5" name="value2">
    <button type="submit">Calculate</button>
</form>

<!-- The result -->
<div id="result">{% partial "framework/calcresult" %}</div>

  The calcresult partial:

{% if result %}
    The result is {{ result }}.
    Processed in partial theme/framework/calcresult.htm
{% else %}
    Click the <em>Calculate</em> button to find the answer.
    i am file framework/calcresult.htm
    
    Why am i not found?
{% endif %}

  The onTest PHP code:

function onTest()
{
    $value1 = input('value1', '');
    $value2 = input('value2', '');
    $operation = input('operation', '');

    if (!is_numeric($value1) || $value1 === '' || !is_numeric($value2) || $value2 === '') {
        $this['result'] = null;
    } else {
        switch ($operation) {
            case '+' :
                $this['result'] = $value1 + $value2;
                break;
            case '-' :
                $this['result'] = $value1 - $value2;
                break;
            case '*' :
                $this['result'] = $value1 * $value2;
                break;
            default :
                $this['result'] = $value1 / $value2;
                break;
        }
    }

    // Debug: Check if result is set correctly
    \Log::info('Result: ' . $this['result']);  // Check Laravel logs

    return [
        '#result' => $this->renderPartial('calcresult')
    ];
}
function onTest()
{
    $value1 = input('value1', '');
    $value2 = input('value2', '');
    $operation = input('operation', '');

    if (!is_numeric($value1) || $value1 === '' || !is_numeric($value2) || $value2 === '') {
        $this['result'] = null;
        return;
    }

    switch ($operation) {
        case '+' :
            $this['result'] = $value1 + $value2;
            break;
        case '-' :
            $this['result'] = $value1 - $value2;
            break;
        case '*' :
            $this['result'] = $value1 * $value2;
            break;
        default :
            $this['result'] = $value1 / $value2;
            break;
    }
}

Continue to ToDoList component

page: framework.htm