Diskuze: Načítání PHP kódu pomocí ajaxu
V předchozím kvízu, Online test znalostí JavaScript, jsme si ověřili nabyté zkušenosti z kurzu.
Zobrazeno 4 zpráv z 4.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
V předchozím kvízu, Online test znalostí JavaScript, jsme si ověřili nabyté zkušenosti z kurzu.
Podívej se třeba sem: http://api.jquery.com/jquery.ajax/, nejvíc tě asi budou zajímat příklady.
Pro našeptávač používám toto:
function showResult(str) {
if (str.length==0) {
document.getElementById("naseptavac").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("naseptavac").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","https://katalog.mat-internet.eu/naseptavac.php?q="+str,true);
xmlhttp.send();
}
Nešlo by něco podobného?
Snad je to kompletni. Kopiroval jsem jen tu funkcni cast. Musi se to spoustet pres server, ja tam mam localhost.
sender_receive.php
<?php
$text = isset($_POST['text']) ? $_POST['text'] : '';
echo 'Poslal jsi text="'.$text.'"';
?>
sender_send.htm
<form action="" onsubmit="buttSend(); return false;">
<input type="submit">
</form>
<hr>
<div id="info_container"></div>
<hr>
<div id="log_container"></div>
<script src="func-inc.js"></script>
<script src="sender.js"></script>
<script>
var SENDER = new classSender({log:SCREEN.log});
function buttSend()
{
var url, arg, cb;
cb = function(str) {$('info_container').innerHTML = str;};
arg = 'text=Ahoj';
url = 'http://localhost/www/zas3-pokus/sender_receive.php';
SENDER.func.sendPost(url, arg, cb);
}
</script>
sender.js
function classSender(opt)
{
var root = this;
this.req = null;
//this.opt = {};
this.func = {};
this.func.log = null;
this.func.cbRequestReceive = function()
{
root.func.log('cbRequest: Request reading...');
var str = root.req.responseText; // pro text/csv
if (!str)
{
root.func.log('cbRequest: Chyba, prazdna odpoved.');
return;
}
root.func.log('cbRequest: File writing end.');
root.func.cbEnd(str);
}
this.func.cbRequestError = function ()
{
var status_text = '';
//...
SCREEN.func.message('cbRequestError: code=' + root.req.status + '. ' + status_text, 'error');
}
this.func.sendPost = function(path, arg, func_CB)
{
root.func.log('strToUrl: File writing start...');
root.func.cbEnd = isFunction(func_CB) ? func_CB : opt.cbEnd;
root.req = new XMLHttpRequest();
root.req.responseType = 'text'; // pro posilani text/csv
root.req.onload = root.func.cbRequestReceive;
root.req.onerror = root.func.cbRequestError;
root.req.error_path = path;
root.req.open('POST', path, true);
root.req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
root.req.send(arg);
}
this.func.init = function(opt)
{
if (!isObject(opt)) {return;}
root.func.log = isFunction(opt.log) ? function (str) {opt.log('classSender.func.' + str);} : null;
root.func.cbRequest = isFunction(opt.cbRequest) ? opt.cbRequest : root.func.cbRequest;
root.func.cbRequestError = isFunction(opt.cbRequestError) ? opt.cbRequestError : root.func.cbRequestError;
root.func.cbEnd = isFunction(opt.cbEnd) ? opt.cbEnd : null;
}
this.func.init(opt);
}
func-inc.js
function isObject(item) {return typeof(item)=='object' && item!=null;}
function isFunction(item) {return typeof(item)=='function' && item!=null;}
function $(id,win) {var win = win || window; return win.document.getElementById(id);}
function classScreen()
{
var root = this;
this.func = {};
this.log = function (str)
{
if (!$('log_container'))
{alert('log_container err');}
// $('log_container').innerHTML += str; // asc
$('log_container').innerHTML = str + '<br>' + $('log_container').innerHTML; // desc
}
this.func.log = function(str)
{
root.log(str);
}
}
var SCREEN = new classScreen();
Zobrazeno 4 zpráv z 4.