如何在单击FORM中的按钮时以HTML格式刷新多个文本区域(HTML内容最多4000个字符)?

如何在单击FORM中的按钮时以HTML格式刷新多个文本区域(HTML内容最多4000个字符)?

问题描述:

I'm storing Data in PHP Two dimentional Array . Sample Values in PHP ARRAY is mentioned below:

First Element in Array
ID = 1
QUES= <p>ABCD</p>
OPT_A = <p>A</p>
OPT_B = <p>B</p>

Second Element in Array
ID = 2
QUES= <p> TWOABCD </p>
OPT_A = <p>TWA </p>
OPT_B = <p>TWB</p>

I am displaying values of QUEST, OPT_A, OPT_B in TEXTAREA in a HTML FORM (using TinyMCE jquery plugin for displaying rich text). The Form has the following buttons : NEXT & PREVIOUS.

On click of NEXT, I want to get TEXT AREAS refeshed with data from next element in array for all values. As per search over internet, I can achieve the same using AJAX, but not sure, how to refresh data of multiple text areas at the same time on click of a button in HTML FORM. Also note data is stored in the form of HTML Tags (with max length upto 4000 chars).

Currently, I'm using a very old method, of refreshing entire page on click of button on a form. It's having lot of drawbacks, including when the page gets refreshed, momentarily the user sees HTML tags before the page is loaded completely.

Can you please suggest any reference for (fixing these two issues) :

  1. Refreshing Data in Multiple Textares of a HTML-FORM on click of Button (inside HTML-FORM) using AJAX
  2. Avoiding momentary display of HTML code in Text area before page is completely loaded.

Updated CODE is mentioned below:

<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
    <form>
        <input id="id" type="hidden" name="id">
        <p>Question:</p>
        <p id="myInstance3" style="border: 1px solid #000;"><textarea id="quest" readonly></textarea></p>
        <p>Option A:</p>
        <textarea id="opt_a" ></textarea>
        <p>Option B:</p>
        <textarea id="opt_b" ></textarea>
        <p>
            <button id="previous">Previous</button>
            <button id="next">Next</button>
        </p>
    </form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript"> 
var array = [{"id":1,"quest":"<p>ABC<\/p>","opt_a":"A","opt_b":"B"}, {"id":2,"quest":"<p>DEF<\/p>","opt_a":"1","opt_b":"2"}];
    var ArrayWalker = (function () {
        var _array = null,
            _index = 0,
            _walk  = function (step) {
                if (_array === null) {
                    throw 'Array is not initialized';
                }
                _index += step;
                if (_index < 0 || _index === _array.length) {
                    _index -= step;
                }
                return _array[_index];
            };
        return {
            init: function (array) {
                _array = array;
                _index = 0;
            },
            current: function () {
                return _walk(0);
            },
            next: function () {
                return _walk(1);
            },
            previous: function () {
                return _walk(-1);
            },
            index: function (seek) {
                if (seek !== undefined) {
                    _index = window.parseInt(seek);
                }
                return _index;
            }
        };
    })();

    function fillElements(data) {
        $('#id').val(data['id']);
        $('#quest').val(data['quest']);
        $('#opt_a').val(data['opt_a']);
        $('#opt_b').val(data['opt_b']);
    }

    ArrayWalker.init(array);
    fillElements(ArrayWalker.current());

    $('#previous').click(function (e) {
        e.preventDefault();
        fillElements(ArrayWalker.previous());
    });
    $('#next').click(function (e) {
        e.preventDefault();
        fillElements(ArrayWalker.next());
    });
</script>
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">
     bkLib.onDomLoaded(function() {
          var myNicEditor = new nicEditor();
          myNicEditor.setPanel('myNicPanel');
          myNicEditor.addInstance('myInstance3');
     });
  </script> 
</body>
</html>

This method is based on traversing a given array that could be get via AJAX or directly on the page request.

You can convert your PHP array into a Javascript one by doing this:

// This is your given array
$array = array(
    array(
        'id'    => 1,
        'quest' => '<p>ABC</p>',
        'opt_a' => 'A',
        'opt_b' => 'B',
    ),
    array(
        'id'    => 2,
        'quest' => '<p>DEF</p>',
        'opt_a' => '1',
        'opt_b' => '2',
    ),
);

// This is how you convert it to Javascript
echo 'var array = ', json_encode($array), ';';

Output:

var array = [{"id":1,"quest":"<p>ABC<\/p>","opt_a":"A","opt_b":"B"}, {"id":2,"quest":"<p>DEF<\/p>","opt_a":"1","opt_b":"2"}];

Now you need a Javascript object that can use that array in order to fill your textarea elements:

var ArrayWalker = (function () {
    var _array = null,
        _index = 0,
        _walk  = function (step) {
            if (_array === null) {
                throw 'Array is not initialized';
            }
            _index += step;
            if (_index < 0 || _index === _array.length) {
                _index -= step;
            }
            return _array[_index];
        };
    return {
        init: function (array) {
            _array = array;
            _index = 0;
        },
        current: function () {
            return _walk(0);
        },
        next: function () {
            return _walk(1);
        },
        previous: function () {
            return _walk(-1);
        },
        index: function (seek) {
            if (seek !== undefined) {
                _index = window.parseInt(seek);
            }
            return _index;
        }
    };
})();

Assuming this markup:

<form>
    <input id="id" type="hidden" name="id" />
    <p>Question:</p>
    <textarea id="quest"></textarea>
    <p>Option A:</p>
    <textarea id="opt_a"></textarea>
    <p>Option B:</p>
    <textarea id="opt_b"></textarea>
    <p>
        <button id="previous">Previous</button>
        <button id="next">Next</button>
    </p>
</form>

This function will fill the elements:

function fillElements(data) {
    $('#id').val(data['id']);
    $('#quest').val(data['quest']);
    $('#opt_a').val(data['opt_a']);
    $('#opt_b').val(data['opt_b']);
}

And finally you need to use your tools to get the job done:

ArrayWalker.init(array);
fillElements(ArrayWalker.current());

$('#previous').click(function (e) {
    e.preventDefault();
    fillElements(ArrayWalker.previous());
});
$('#next').click(function (e) {
    e.preventDefault();
    fillElements(ArrayWalker.next());
});

Here is a working demo.