gpt4 book ai didi

javascript - SilverStripe 通过 Ajax 提交 HTML 表单

转载 作者:行者123 更新时间:2023-11-29 16:51:18 25 4
gpt4 key购买 nike

我想通过 Ajax 将数据从简单的 HTML 表单传递到 Controller ,然后处理数据并返回响应。

目前我有以下内容:

主页.ss

<form method="POST" class="form-horizontal submit-form" onsubmit="return checkform(this);">

<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name">Name</label>
<div class="col-md-8">
<input id="name" name="name" type="text" placeholder="insert full Name" class="form-control input-md" required="" />
</div>
</div>

<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="send-btn"></label>
<div class="col-md-8">
<button id="send-btn" name="send-btn" class="btn btn-primary">Submit</button>
</div>
</div>
</form>

JavaScript

$('form.submit-form').submit(function() {
$.ajax({
type: 'POST',
url: 'processForm',
data: $(this).serialize(),
success: function(data) {
alert('data received');
}
});
});

HomePage.php

class HomePage_Controller extends Page_Controller {
public function events() {
$events = CalendarEvent::get();
return $events;
}

public function processForm() {
if (Director::is_ajax()) {
echo 'ajax received';
} else {
//return $this->httpError(404);
return 'not ajax';
}
}
}

在开发人员工具中,我可以看到我得到了带有 404 未找到错误的 xhr processForm。

如何让这个 Ajax 表单与 SilverStripe Controller 一起正常工作?

最佳答案

蜘蛛,

我已经做了类似下面的事情。这是一个快速而肮脏的演示,尚未经过测试,但它可能会让您走上正确的道路。如果您不熟悉表单在 SilverStripe 中的工作方式,请参阅 SilverStripe 中的前端表单类(class)。我发现这些类(class)对我个人很有用,并提供了该类(class)的代码:http://www.silverstripe.org/learn/lessons/introduction-to-frontend-forms?ref=hub

Page.php

<?php

class Page extends SiteTree
{



}

class Page_Controller extends Content_Controller
{

private static $allowed_actions = array(
'MyForm',
);

public function MyForm()
{
Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.min.js');
Requirements::javascript(THIRDPARTY_DIR . '/jquery-validate/jquery.validate.min.js');
Requirements::javascript('/path/to/your/validation/script.js');

$fields = FieldList::create(
TextField::create('name')
->setTitle('Name')
);

$actions = FieldList::create(
FormAction::create('doSubmit')
->setTitle('Submit')
);

$requiredFields = RequiredFields::create(
'name'
);

$form = Form::create($this, 'MyForm', $fields, $actions, $requiredFields);

return $form;
}

public function doSubmit($data, $form)
{
//process $data or create your new object and simpley $form->saveInto($yourObject); then $yourObject->write()
//then deal with ajax stuff
if ($this->request->isAjax()) {
return $this->customise(array(
'YourTemplateVar' => 'Your Value'
))->renderWith('YourIncludeFile');
} else {
//this would be if it wasn't an ajax request, generally a redirect to success/failure page
}
}

}

YourValidationScript.js

(function ($) {
$(function () {
$('#MyForm_Form').validate({
submitHandler: function (form) {
$.ajax({
type: $(form).attr('method'),
url: $(form).attr('action') + "?isAjax=1",
data: $(form).serialize()
})
.done(function (response) {
$('.content').html(response);
})
.fail(function (xhr) {
alert('Error: ' + xhr.responseText);
});
},
rules: {
name: "required"
}
});
})
})(jQuery);

关于javascript - SilverStripe 通过 Ajax 提交 HTML 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35895427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com