简体   繁体   English

羊驼表格提交,留在同一页面

[英]Alpaca form submission, stay on same page

I am building a form with PHP and ALPCA (jquery,ajax). 我正在使用PHP和ALPCA(jquery,ajax)构建一个表单。 I am having trouble with file submitting and staying on the same page. 我在文件提交和停留在同一页面时遇到问题。 I have tried the recommended techniques for doing as such with AJAX event.preventDefault(); 我尝试过使用AJAX event.preventDefault();进行的推荐技术event.preventDefault(); , also using hidden frame but with no success. ,也使用隐藏框架,但没有成功。 My question is does the ALPACA package require a different method? 我的问题是ALPACA包需要不同的方法吗?

FORM SECTION 表格部分

    <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 FILE 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);

?> 

Thank You 谢谢

If you want an alpaca form to submit solely trough ajax you can use something along these lines. 如果你想要一个羊驼形式只通过ajax提交你可以使用这些线上的东西。

First, to make the submit button use ajax when clicked set its click property (ie options.form.buttons.submit.click ) to something like: 首先,要使提交按钮在单击时使用ajax,请将其click属性(即options.form.buttons.submit.click )设置为:

function(){
    this.ajaxSubmit().done(function(){
        console.log('Saved');
    }).fail() {
        console.warn('Failed to save');
    });
}

Now, clicking the submit button will save the form with ajax and the user will remain on page. 现在,单击提交按钮将使用ajax保存表单,用户将保留在页面上。 What's left is handling the standard submit. 剩下的就是处理标准提交。 This can be done setting postRender (a root callback, see documentation ) to something along these lines: 这可以通过将postRender (根回调,请参阅文档 )设置为以下内容:

function(control) {
    if (control.form) {
        control.form.registerSubmitHandler(function (e) {
        control.form.getButtonEl('submit').click();
        return false;
    });
}

Hope this still helps. 希望这仍然有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM