From a6bbe1bb3b760e47c3b0d52162d144cbfb3a1c39 Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Sun, 17 Oct 2010 07:50:40 +0200 Subject: [PATCH] Save select data by AJAX --- adminer/include/functions.inc.php | 2 +- adminer/select.inc.php | 4 ++-- adminer/static/functions.js | 24 +++++++++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index ea81a1c9..d3489447 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -345,7 +345,7 @@ function redirect($location, $message = null) { restart_session(); $_SESSION["messages"][] = $message; } - if (isset($location)) { + if (isset($location) && $_SERVER["HTTP_X_REQUESTED_WITH"] != "XMLHttpRequest") { header("Location: " . ($location != "" ? $location : ".")); exit; } diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 8d118bcd..441681e6 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -333,7 +333,7 @@ if (!$columns) { } } $id = h("val[$unique_idf][" . bracket_escape($key) . "]"); - $value = $_POST["val"][$unique_idf][bracket_escape($key)]; + $value = ($error ? $_POST["val"][$unique_idf][bracket_escape($key)] : null); $h_value = h(isset($value) ? $value : $row[$key]); $long = strpos($val, "..."); $editable = is_utf8($val) && !$long && $rows[$n][$key] == $row[$key] && !$functions[$key]; @@ -380,7 +380,7 @@ if (!$columns) { if (!information_schema(DB)) { ?>
- + diff --git a/adminer/static/functions.js b/adminer/static/functions.js index c1d25c5e..f554758a 100644 --- a/adminer/static/functions.js +++ b/adminer/static/functions.js @@ -136,17 +136,22 @@ var ajaxTimeout; /** Create AJAX request * @param string +* @param string * @return XMLHttpRequest or false in case of an error */ -function ajax(url) { +function ajax(url, data) { var xmlhttp = (window.XMLHttpRequest ? new XMLHttpRequest() : (window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : false)); if (xmlhttp) { var currentState = ++ajaxState; clearTimeout(ajaxTimeout); ajaxTimeout = setTimeout(function () { setHtml('main', ''); - }, 1000); // defer displaying loader - xmlhttp.open('GET', url); + }, 500); // defer displaying loader + var method = (data === undefined ? 'GET' : 'POST'); + xmlhttp.open(method, url); + if (method == 'POST') { + xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + } xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && currentState == ajaxState) { @@ -154,10 +159,11 @@ function ajax(url) { setHtml('main', xmlhttp.responseText); if (window.jush) { jush.highlight_tag('code'); + jush.highlight_tag('pre', 0); } } }; - xmlhttp.send(''); + xmlhttp.send(data); } return xmlhttp; } @@ -170,11 +176,15 @@ function ajaxForm(form) { var params = [ ]; for (var i=0; i < form.elements.length; i++) { var el = form.elements[i]; - if (el.name && (!/checkbox|radio/i.test(el.type) || el.checked)) { - params.push(el.name + '=' + encodeURIComponent(/select/i.test(el.tagName) ? selectValue(el) : el.value)); + if (el.name && (!/checkbox|radio|submit|file/i.test(el.type) || el.checked)) { + params.push(encodeURIComponent(el.name) + '=' + encodeURIComponent(/select/i.test(el.tagName) ? selectValue(el) : el.value)); } } - return ajax((form.action || location.pathname) + '?' + params.join('&')); + if (form.method == 'post') { + return ajax(form.action || location.href, params.join('&')); + } else { + return ajax((form.action || location.pathname) + '?' + params.join('&')); + } }