Save select data by AJAX

This commit is contained in:
Jakub Vrana 2010-10-17 07:50:40 +02:00
parent 8d4b241156
commit a6bbe1bb3b
3 changed files with 20 additions and 10 deletions

View file

@ -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;
}

View file

@ -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, "<i>...</i>");
$editable = is_utf8($val) && !$long && $rows[$n][$key] == $row[$key] && !$functions[$key];
@ -380,7 +380,7 @@ if (!$columns) {
if (!information_schema(DB)) {
?>
<fieldset><legend><?php echo lang('Edit'); ?></legend><div>
<input type="submit" value="<?php echo lang('Save'); ?>" title="<?php echo lang('Double click on a value to modify it.'); ?>">
<input type="submit" value="<?php echo lang('Save'); ?>" title="<?php echo lang('Double click on a value to modify it.'); ?>" onclick='return !ajaxForm(this.form);'>
<input type="submit" name="edit" value="<?php echo lang('Edit'); ?>">
<input type="submit" name="clone" value="<?php echo lang('Clone'); ?>">
<input type="submit" name="delete" value="<?php echo lang('Delete'); ?>" onclick="return confirm('<?php echo lang('Are you sure?'); ?> (' + (this.form['all'].checked ? <?php echo $found_rows; ?> : formChecked(this, /check/)) + ')');">

View file

@ -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', '<img src="../adminer/static/loader.gif" alt="">');
}, 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('&'));
}
}