Commit 0309529d authored by Richard Mansfield's avatar Richard Mansfield
Browse files

ajax form submitting function now created in form.php

parent 8dfa0773
......@@ -38,7 +38,10 @@ asort($pageoptions);
$f = array(
'name' => 'editsitepage',
'method' => 'post',
'onsubmit' => 'return submitForm(\'editsitepage\',\'savesitepage.json.php\',contentSaved);',
'ajaxpost' => true,
'ajaxsuccessfunction' => 'contentSaved',
'ajaxformhandler' => 'savesitepage.json.php',
//'onsubmit' => 'return submitForm(\'editsitepage\',\'savesitepage.json.php\',contentSaved);',
'action' => '',
'elements' => array(
'pagename' => array(
......
......@@ -37,36 +37,3 @@ function displayMessage(m, /* optional */ elemid) {
});
}
// The javascript form validating function should be available from
// the server as formname_validate().
// Gets form elements, submits them to a url via post, and waits for a
// JSON response containing the result of the submission.
function submitForm(formid,url,callback) {
if (typeof(tinyMCE) != 'undefined') {
tinyMCE.triggerSave();
}
if (!eval(formid + '_validate()')) {
return false;
}
var formelements = getElementsByTagAndClassName(null,formid,formid);
var data = {};
for (var i = 0; i < formelements.length; i++) {
data[formelements[i].name] = formelements[i].value;
}
var req = getXMLHttpRequest();
req.open('POST',url);
req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
var d = sendXMLHttpRequest(req,queryString(data));
d.addCallback(function (result) {
var data = evalJSONRequest(result);
var type = data.success ? 'infomsg' : 'errmsg';
eval(formid + '_message(\'' + data.message + '\',\'' + type + '\')');
callback();
});
d.addErrback(function() {
eval(formid + '_message(\'' + get_string('unknownerror') + '\',\'' + 'errmsg\')');
});
eval(formid + '_message(\'' + get_string('processingform') + '\',\'' + 'infomsg\')');
return false;
}
......@@ -145,6 +145,13 @@ class Form {
*/
private $onsubmit = '';
/**
* Ajax forms specify these
*/
private $ajaxpost = false;
private $ajaxsuccessfunction = '';
private $ajaxformhandler = '';
/**
* The tab index for this particular form.
*
......@@ -218,6 +225,9 @@ class Form {
'method' => 'post',
'action' => '',
'onsubmit' => '',
'ajaxpost' => false,
'ajaxformhandler' => '',
'ajaxsuccessfunction' => '',
'submit' => true,
'elements' => array()
);
......@@ -233,6 +243,15 @@ class Form {
$this->submit = $data['submit'];
$this->onsubmit = $data['onsubmit'];
if ($data['ajaxpost']) {
$this->ajaxformhandler = $data['ajaxformhandler'];
$this->ajaxpost = $data['ajaxpost'] && !empty($this->ajaxformhandler);
$this->ajaxsuccessfunction = $data['ajaxsuccessfunction'];
if ($this->ajaxpost) {
$this->onsubmit = 'return ' . $this->name . '_submit();';
}
}
if (isset($data['renderer'])) {
$this->renderer = $data['renderer'];
}
......@@ -423,10 +442,15 @@ class Form {
}
$result .= "</form>\n";
$js_validator = '<script language="javascript" type="text/javascript">' . "\n"
. $this->validate_js() . "</script>\n";
if ($this->ajaxpost) {
$inlinejs = '<script language="javascript" type="text/javascript">';
$inlinejs .= $this->validate_js();
$inlinejs .= $this->submit_js();
$inlinejs .= "</script>\n";
return $result . $inlinejs;
}
return $result . $js_validator;
return $result;
}
/**
......@@ -600,6 +624,41 @@ class Form {
return $result;
}
/**
* Returns a js function to submit an ajax form
*/
private function submit_js() {
$result = 'function ' . $this->name . "_submit(){\n";
// eventually we should check input types for wysiwyg before doing this:
$result .= "if (typeof(tinyMCE) != 'undefined') { tinyMCE.triggerSave(); }\n";
$result .= 'if (!' . $this->name . "_validate()) { return false; }\n";
$result .= "var data = {};\n";
foreach ($this->get_elements() as $element) {
$result .= "data['" . $element['name'] . "'] = $('" . $element['name'] . "').value;\n";
}
// This does a post. Gets are much simpler in mochikit and we could check whether
// there are any big fields (wysiwyg,textarea) and do a get here if there aren't any.
$result .= 'var req = getXMLHttpRequest();';
$result .= "req.open('POST','" . $this->ajaxformhandler . "');\n";
$result .= "req.setRequestHeader('Content-type','application/x-www-form-urlencoded');\n";
$result .= "var d = sendXMLHttpRequest(req,queryString(data));\n";
$result .= 'd.addCallback(function (result) {';
$result .= 'var data = evalJSONRequest(result);';
$result .= "var type = data.success ? 'infomsg' : 'errmsg';\n";
$result .= $this->name . "_message(data.message,type);\n";
if (!empty($this->successcallback)) {
$result .= $this->successcallback . "();\n";
}
$result .= "});\n";
$result .= 'd.addErrback(function() {';
$result .= $this->name . "_message('" . get_string('unknownerror') . "','errmsg');";
$result .= "});\n";
$result .= $this->name . "_message('" . get_string('processingform') . "','infomsg');\n";
$result .= "return false;\n";
$result .= '}';
return $result;
}
/**
* Returns whether a field has an error marked on it.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment