Ajax interactive form generation

It – Hardware & Software | | 2 views

Interactive form formation

The last time, in the past time, SQL the SQL bulletin board and postal code, was address search with JavaScript which utilizes the tool, but this time, it tries making the simple Ajax sample the way of the visor.

The fact that this time it tries trying, in combination with the part of form, to the response of the user, is something which it will try reading to interactive.

As for the FORM element of normal HTML, from first all elements are completed inside the page, but if Ajax is used, to tell the truth, either the FORM element, even the INPUT element do not exist to first HTML and it means that also the te is good.

Because as for the necessary element, from after, there is no page transition with it is possible, to read, it corresponded to the input of the user, from midst of countless combination, consecutively reading just the user private form element, from after, it is to be possible to keep assembling to interactive.

* V036 of the library for provisional Ajax is utilized.

    1: Just one reads the INPUT element
    2: It reads to interactive
    3: The library v036 for provisional Ajax

Just one reads the INPUT element

-> Sample
(* As for this sample the above-mentioned link reference)

anyway, let’s try a simple example. the firstHTMLthere is no element to form, with the click of a button, INPUTtry to load only one of the elements.

browser behavior

win mac linux
n7 m1 e6 o7 o8 n7 m1 e5 s1 n7 m1 k3
× ×

this sample is, when you click the button, functionloadForms()launch is, read a text file, written in the fileINPUTdisplays the elements.

are using, library, so be familiar, but also operate as usual after the call back, this time, i am using only a little new tricks. Mac SafariandLinux KonquerorisUTF-8well can not be read to support a string of, until now, was to encode all. but, this time, this2i tried to to how to branch the only one browser.

file2although one must be made, disconnect the only treatment of the problem of the browser, IEorFierfox, Operain a browser such as is you can avoid an increase in the amount of character to be encoded by (of course, to the person who can provide a server-side program is generally unnecessary processing…) .

was usedJavaScript/HTMLside andTEXTsource of the side, are as follows:.

(JavaScriptside) <!– Ajaxsimple library jslb_ajax.js http://jsgt.org/mt/archives/01/ n/a html–> <script languege = “JavaScript” src = “http://jsgt.org/ajax/ref/lib/jslb_ajax036.js” charset = “utf-8″></script> <script language = “JavaScript”> <!– var ua = navigator.userAgent var safari = ua.indexOf(‘Safari’) != -1, var khtml = ua.indexOf(‘Konqueror’) != -1 /the callback function ( will be executed when the receiving ) function on_loadedToDIV1(oj){ /URLdecode res = decodeURIComponent(oj.responseText) /a string that has been the responseDIVto output document.getElementById(“div1″).innerHTML=res } /functions for sending function loadForms(){ /branch browser if(safari||khtml) url = ‘test2.txt’ /URImay encode else url = ‘test1.txt’ /URIno encoding sendRequest( on_loadedToDIV1, /DIV1callback function for ”, ‘POST’, /data and HTTPmethod url, /URL true,true /asynchronous force load ) } /–> </script> <body> when you press the buttonINPUTtag will be loaded <br> <input type=”button” value=”INPUTread the” onclick=”loadForms()”> <!– the output here –> <div id=”div1″></div>

(textside)
(test1.txt) <input value=”hello” type=”text” name=”input1″> (test2.txt) the abovetest1.txttheencodeURIComponent()wasSafariandKHTMLfor %3Cinput%20value%3D%22%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%8 1%AF%22%20type%3D%22text%22%20name%3D%22input1%22%3E

interactive form generation

    1: INPUTthe elements1only one load
    2: loaded into the interactive
    3: interimAjaxlibrary forv036

2: loaded into the interactive

Sample
(*this sample, see above link)

anyway, let’s try a simple example. the firstHTMLthere is no element to form, with the click of a button, INPUTtry to load only one of the elements.

browser behavior

win mac linux
n7 m1 e6 o7 o8 n7 m1 e5 s1 n7 m1 k3
× ×

*here, such as on the previous page, Safari, Konquerorso i’ve omitted the anti-, this2in one browser, but garbled, treatment similar previous page, or, PHPif treatment such as encoded in the side, garbled to eliminate.

this sample is, radio button selection YES when you click the, the first one reads the text file, written in the fileINPUTelement radio button to display the new and. NO is terminated if.

here, when you press the send button, reception result will be displayed. point we would like you to confirm, read all of these, is that it was done without page transition. that is, only the parts you need when you need, is being loaded in the interactive.

this sample itself, is simple, range of application of this usage may be quite unexpectedly wide.

was usedJavaScript/HTMLside andTEXTsource of the side, are as follows:.

(JavaScriptside) <!– Ajaxsimple library jslb_ajax.js http://jsgt.org/mt/archives/01/ n/a html–> <script languege = “JavaScript” src = “http://jsgt.org/ajax/ref/lib/jslb_ajax036.js” charset = “utf-8″></script> <script language = “JavaScript”> <!– / / the callback function / / first reading DIV1callback function for ( will be executed when the receiving ) function on_loadedToDIV1(oj){ /a string that has been the responseDIVto output document.getElementById(“div1″).innerHTML=decode50915(oj.responseText) } / then it reads DIV2callback function for ( will be executed when the receiving ) function on_loadedToDIV2(oj){ /a string that has been the responseDIVto output document.getElementById(“div2″).innerHTML=decode50915(oj.responseText) } / submitpost-processing DIV3callback function for ( will be executed when the receiving ) function on_loadedToDIV3(oj){ /a string that has been the responseDIVto output document.getElementById(“div3″).innerHTML=decode50915(oj.responseText) } / / for decoding function / function decode50915(text){ /urldecode res = decodeURIComponent(text) /to identify the tag for safetyHTMLactivation res = res.split(‘<input value=”"‘).join(‘<input value=”"‘) res = res.split(‘>’).join(‘>’) return res } / / functions for sending / /DIV1for the transmission function for function getMsgToDIV(oj,callback,url,data){ if(oj.value==’yes’) sendRequest( callback, /DIV1callback function for data, ‘POST’, /data and HTTPmethod url, /URL true,true /asynchronous force load ) } /–> </script> <body> <!– eachDIVand outputs it to –> <div id=”div3″> do you start? <form name=”test” onsubmit=”return false”> <input type=”radio” name=”yesno1″ value=”yes” onclick=”getMsgToDIV(this,on_loadedToDIV1,’test1.txt’)”> YES <input type=”radio” name=”yesno1″ value=”no” onclick=”document.getElementById(‘div3′).innerHTML=’end’” checked> NO <div id=”div1″></div> <div id=”div2″></div> </div> </form>

(textside)
(first read test1.txt) <hr> in, please enter something <input value=”" type=”text” name=”data1″> <br> do you want to continue? <br> <input type=”radio” name=”yesno2″ value=”yes” onclick=”getMsgToDIV(this,on_loadedToDIV2,’test2.txt’)”> YES <input type=”radio” name=”yesno2″ value=”no” onclick=”document.getElementById(‘div3′).innerHTML=’end’” checked> NO (then read test2.txt) <hr> more1place, please enter <input value=”" type=”text” name=”data2″> <br> this is the end of input. <hr> please send <input type=”button” value=”transmission” onclick=”this.value=’yes’;getMsgToDIV(this,on_loadedToDIV3,’./submit.php’, ‘&data1=’+document.getElementById(‘data1′).value +’&data2=’+document.getElementById(‘data2′).value)”>

(PHPside)
(submit.php which receives submit) <? php / reception $data1 = enc ($_POST ['data1']); $data2 = enc ($_POST ['data2']); function such as / decoding enc ($data) {/ data UTF-8 conversion $data = mb_convert_encoding ($data, “UTF-8″); /HTML entity (it makes< for< safety and the like,) $data = htmlspecialchars ($data, 0, “UTF-8″); /URI encoding $data = rawurlencode ($data); return $data; } / output in UTF-8 setting mb_http_output (‘UTF-8′); / output echo (“hr which receives description below<> “); echo (“1: “. $data1. “<br> “); echo (“2: “. $data2. “<br> “); ? >

Interactive form formation

    1: Just one reads the INPUT element
    2: It reads to interactive
    3: Source the library v036 for provisional Ajax

[source] interimAjaxlibrary forv036

this.jslibrary, even without touching the contents, especially, you can use you only need to file a direct copy of.

interimAjaxlibrary support forURLis (Ajax) preliminary versionAjaxlibrary for is. basis of the branch, etc. have been written to the browser, such as reception processing in the library, about the timing of response(about the differences between each browser)survey is based on the results, such as.

(this interimAjaxlibrary for) jslb_ajax036.js