我正在用PHP和ALPCA(jquery,ajax)构建表单.我在文件提交和停留在同一页面上遇到麻烦.我已经尝试过使用AJAX event.preventDefault();这样的推荐技术,也使用隐藏框架但没有成功.我的问题是,ALPACA软件包需要其他方法吗?
表格部分
<script type="text/javascript">
$(document).ready(function() {
$("#form").alpaca({
"schema": {
"title":"User Feedback",
"description":"What do you think about this Speaker?",
"type":"object",
"properties": {
"email": {
"type":"string",
"title":"Email",
"required":false
},
"feedback": {
"type":"string",
"title":"Feedback"
},
"ranking": {
"type":"string",
"title":"Ranking",
"enum":['It is Great', 'Not so cool', 'Very poor quality', 'I think there may be a Privacy or Copyright issue'],
"required":false
}
}
},
"options": {
"form":{
"attributes":{
"action":"FORM.php",
"method":"post"
"target":"hiddenFrame"
},
"buttons":{
"submit":{}
}
},
"helper": "What do you think about this Speaker?",
"fields": {
"email": {
"size": 20,
"placeholder": "email not nessasary"
},
"feedback" : {
"type": "textarea",
"name": "your_feedback",
"rows": 4,
"cols": 40,
"helper": ""
},
"ranking": {
"type": "select",
"helper": "",
"optionLabels": ["It is Great", "Not so cool", "Very poor quality", "I think there may be a Privacy or Copyright issue"]
}
}
},
e.preventDefault();
});
});
</script>
PHP文件
<?php
$file = "people.txt";
$feedback = $_REQUEST['feedback'];
$ranking = $_REQUEST['ranking'];
$email= $_REQUEST['email'];
$string = file_get_contents("/tmp/live-info");
$json = str_replace('***(', $callback, $string);
$json = rtrim($json, ')');
$json_a = json_decode($json, true);
$current_name = $json_a['current'][name];
$current_name .= "|$email|$ranking";
$feedback .= "|$current_name" .PHP_EOL;
file_put_contents($file, $feedback, FILE_APPEND | LOCK_EX);
?>
谢谢
解决方法:
如果您希望羊驼毛形式仅提交低谷ajax,则可以使用以下方式.
首先,要使提交按钮在单击时使用ajax,请将其click属性(即options.form.buttons.submit.click)设置为类似以下内容:
function(){
this.ajaxSubmit().done(function(){
console.log('Saved');
}).fail() {
console.warn('Failed to save');
});
}
现在,单击提交按钮将使用ajax保存表单,并且用户将保留在页面上.剩下的就是处理标准提交.可以通过将以下行设置为postRender(根回调,请参见documentation)来完成:
function(control) {
if (control.form) {
control.form.registerSubmitHandler(function (e) {
control.form.getButtonEl('submit').click();
return false;
});
}
希望这对您有所帮助.
原文地址:https://codeday.me/bug/20191120/2044432.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。