src/HtmlGenerator.php
/**
     * Generates a button that triggers an ajax request. (using POST and expecting json response)
     * 
     * @param string label     - label to appear on the ajax button. e.g. 'click me'
     * @param postData - associative array of name/value pairs to send in the ajax request.
     * @param updateButtonText - flag for whether the buttons text should change to reflect status
     * @param onSuccess - name of javascript function to run upon successful request
     * @param onError   - name of javascript function to run if there was an ajax comms error.
     * @param onAlways  - name of javascript function to run if success or error.
     * 
     * @return string - the generated html for the ajax button.
    */
Generates a button that triggers an ajax request. (using POST and expecting json response)

@param string label     - label to appear on the ajax button.

@return string - the generated html for the ajax button.
Generates a button that triggers an ajax request . ( using POST and expecting json response ) @param string label - label to appear on the ajax button . @return string - the generated html for the ajax button .
public static function generateAjaxButton($label, 
                                              $postData, 
                                              $updateButtonText=true,
                                              $onSuccess = '', 
                                              $onError   = '', 
                                              $onAlways  = '')
    {
        $ajaxParams = array(
            'data'     => $postData,
            'type'     => 'POST',
            'dataType' => $postData
        );
        $callbacks = '';
        if ($updateButtonText)
        {  
            $callbacks .= 
                'var originalText = this.value;' . PHP_EOL .
                'this.value = "Updating...";' . PHP_EOL .
                'ajaxRequest.fail(function(){this.value="Error"});' . PHP_EOL;
                'ajaxRequest.done(function(){this.value="Complete"});' . PHP_EOL;
                'var timeoutFunc = function(){this.value=originalText};' . PHP_EOL .
                'ajaxRequest.done(function(){setTimeout(timeoutFunc, 2000)});' . PHP_EOL;
        }
        if ($onSuccess != '')
        {
            $callbacks .= 'ajaxRequest.done(' . $onSuccess . ');' . PHP_EOL;
        }
        if ($onError != '')
        {
            $callbacks .= 'ajaxRequest.fail(' . $onError . ');' . PHP_EOL;
        }
        if ($onAlways != '')
        {
            $callbacks .= 'ajaxRequest.always(' . $onAlways . ');' . PHP_EOL;
        }
        $onclick = 
            'var ajaxUrl     = "ajax_handler.php";' . PHP_EOL .
            'var ajaxParams  = ' . json_encode($ajaxParams) . ';' . PHP_EOL .
            'var ajaxRequest = $.ajax(ajaxUrl, ajaxParams);' .  PHP_EOL .
            $callbacks;
        $html = 
            "<input " .
                "type='button' " .
                "value='" . $label . "' " .
                "onclick='" . $onclick . "' " .
            "/>";
        return $html;
    }
public static function generateAjaxButton ( $ label , $ postData , $ updateButtonText = true , $ onSuccess = '' , $ onError = '' , $ onAlways = '' ) { $ ajaxParams = array ( 'data' => $ postData , 'type' => 'POST' , 'dataType' => $ postData ) ; $ callbacks = '' ; if ( $ updateButtonText ) { $ callbacks .= 'var originalText = this.value;' . PHP_EOL . 'this.value = "Updating...";' . PHP_EOL . 'ajaxRequest.fail(function(){this.value="Error"});' . PHP_EOL ; 'ajaxRequest.done(function(){this.value="Complete"});' . PHP_EOL ; 'var timeoutFunc = function(){this.value=originalText};' . PHP_EOL . 'ajaxRequest.done(function(){setTimeout(timeoutFunc, 2000)});' . PHP_EOL ; } if ( $ onSuccess != '' ) { $ callbacks .= 'ajaxRequest.done(' . $ onSuccess . ');' . PHP_EOL ; } if ( $ onError != '' ) { $ callbacks .= 'ajaxRequest.fail(' . $ onError . ');' . PHP_EOL ; } if ( $ onAlways != '' ) { $ callbacks .= 'ajaxRequest.always(' . $ onAlways . ');' . PHP_EOL ; } $ onclick = 'var ajaxUrl     = "ajax_handler.php";' . PHP_EOL . 'var ajaxParams  = ' . json_encode ( $ ajaxParams ) . ';' . PHP_EOL . 'var ajaxRequest = $.ajax(ajaxUrl, ajaxParams);' . PHP_EOL . $ callbacks ; $ html = " <input  " . " type='button'  " . " value=' " . $ label . " '  " . " onclick=' " . $ onclick . " '  " . " /> " ; return $ html ; }
Generates a button that triggers an ajax request.
Generates a button that triggers an ajax request .
# Important that only double quotes appear within onclick not singles. # Have to use an 'input type=button' rather than button here because we  # want to change value property