将表单设置为在提交时重定向到当前页面 (SilverStripe/PHP)
Set form to redirect to current page on submit (SilverStripe/PHP)
我有一个 SilverStripe 站点的注册表单,它处理服务器端的所有内容。最初,它只会出现在主页上,所以我的设置工作正常。但后来需求发生了变化,表单也需要出现在子页面上。无论我为 action
参数设置了什么,除了表单总是提交到主页之外,一切仍然有效。
最初,action
参数是“/home/submit”。我将其更改为接受 returns 当前页面 url 的变量,并通过创建一个名为 Link
的函数(参见下面的代码)向其附加“/submit”。这似乎有效并将正确的 url 放入 action
参数中。
但是当您点击提交按钮时,表单仍会将用户送回主页,这不是我想要的。我希望他们留在表单所在的当前页面(无论是主页还是任何子页面)。
我尝试获取当前 URL 并将其用于提交函数的最后一行,如下所示:
$current = $this->get_current_page()->Link();
return $this->redirect("$current/?redirected=1#signupform");;
但这会将用户发送到不正确的 url:http://my-site.org/sub-page-title /submit
(这是无效的)
这是存储在Page.php中的表单代码:
public function Link($action='') {
$req = Controller::curr()->getRequest();
$req->setURL(parent::Link($action));
$url = $req->getURL(TRUE); // get the url back but with querystr intact.
return $url ;
}
public function getFirstName() {
return Session::get('first_name');
}
public function getLastName() {
return Session::get('last_name');
}
public function getCompanyName() {
return Session::get('company_name');
}
public function getEmail() {
return Session::get('email');
}
public function submit(SS_HTTPRequest $request) {
$firstName = $request->postVar('first_name');
$lastName = $request->postVar('last_name');
$c = $request->postVar('company_name');
$email = $request->postVar('email');
Session::clear('FORM_ERRORS');
$errors = [];
if (empty($email)) {
array_push($errors, "Email is required");
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
array_push($errors, "Email must be in a valid format. Example: example@domain.com");
}
if(empty($firstName)){
array_push($errors, "Please enter a first name");
}
if(empty($lastName)){
array_push($errors, "Please enter a last name");
}
if (empty($errors)) {
Session::clear('first_name');
Session::clear('last_name');
Session::clear('email');
Session::clear('company_name');
$comment = new EmailSignUpSubmission();
$comment->FirstName = $firstName;
$comment->LastName = $lastName;
$comment->CompanyName = $c;
$comment->Email = $email;
$comment->write();
} else {
Session::set($this->formErrorsKey, $errors);
Session::set('first_name', $firstName);
Session::set('last_name', $lastName);
Session::set('company_name', $c);
Session::set('email', $email);
}
return $this->redirect("/?redirected=1#signupform");
}
public function getFormErrors() {
$errors = Session::get($this->formErrorsKey);
if ($errors == null || $errors == "") {
return null;
} else {
$errorList = new ArrayList();
foreach ($errors as $error) {
$e = new ArrayData(['Text' => $error]);
$errorList->add($e);
}
return $errorList;
}
}
public function isRedirect() {
$request = $this->getRequest();
return $request->getVar('redirected') == "1";
}
这里是 HTML 表格本身:
<div class="sign-up" id="signupform">
<div class="form-container">
<% if $isRedirect && not $getFormErrors %>
<div class="row">
<p class="success">
Thank you for your submission. You will hear back from us shortly.
</p>
</div>
<% else %>
<form method="post" action="/$Link/submit">
<h2 class="text-center white subscribe-hdr">Sign up</h2>
<% if $getFormErrors %>
<% loop $getFormErrors %>
<p class="error">$Text</p>
<% end_loop %>
<% end_if %>
<p class="white subscribe-body" style="text-align:center;">Sign up for the latest newsletter.</p>
<div class="form-group">
<input class="form-control" type="text" name="first_name" value="$getFirstName" placeholder="First Name">
</div>
<div class="form-group">
<input class="form-control" type="text" name="last_name" value="$getLastName" placeholder="Last Name">
</div>
<div class="form-group">
<input class="form-control" type="text" name="company_name" value="$getCompanyName" placeholder="Company">
</div>
<div class="form-group">
<input class="form-control" type="email" name="email" value="$getEmail" placeholder="Email">
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit"
style="width:140px;margin-left:auto;margin-right:auto; float: none; text-align: center">Submit
</button>
</div>
</form>
<% end_if %>
</div>
</div>
您始终可以使用隐藏字段:
<input type="hidden" value="{$AbsoluteLink}" name="redirectURL" />
然后在您的提交方法中使用它进行重定向。
扩展表单 class 会容易得多,因为您将当前的 Controller 对象传递给表单。 https://docs.silverstripe.org/en/3/developer_guides/forms/introduction/
派对可能有点晚了,但是这会起到同样的作用,同时不需要表单中的隐藏字段。
return $this->redirect($this->AbsoluteLink().'?redirected=1#signupform"');
我有一个 SilverStripe 站点的注册表单,它处理服务器端的所有内容。最初,它只会出现在主页上,所以我的设置工作正常。但后来需求发生了变化,表单也需要出现在子页面上。无论我为 action
参数设置了什么,除了表单总是提交到主页之外,一切仍然有效。
最初,action
参数是“/home/submit”。我将其更改为接受 returns 当前页面 url 的变量,并通过创建一个名为 Link
的函数(参见下面的代码)向其附加“/submit”。这似乎有效并将正确的 url 放入 action
参数中。
但是当您点击提交按钮时,表单仍会将用户送回主页,这不是我想要的。我希望他们留在表单所在的当前页面(无论是主页还是任何子页面)。
我尝试获取当前 URL 并将其用于提交函数的最后一行,如下所示:
$current = $this->get_current_page()->Link();
return $this->redirect("$current/?redirected=1#signupform");;
但这会将用户发送到不正确的 url:http://my-site.org/sub-page-title /submit
(这是无效的)
这是存储在Page.php中的表单代码:
public function Link($action='') {
$req = Controller::curr()->getRequest();
$req->setURL(parent::Link($action));
$url = $req->getURL(TRUE); // get the url back but with querystr intact.
return $url ;
}
public function getFirstName() {
return Session::get('first_name');
}
public function getLastName() {
return Session::get('last_name');
}
public function getCompanyName() {
return Session::get('company_name');
}
public function getEmail() {
return Session::get('email');
}
public function submit(SS_HTTPRequest $request) {
$firstName = $request->postVar('first_name');
$lastName = $request->postVar('last_name');
$c = $request->postVar('company_name');
$email = $request->postVar('email');
Session::clear('FORM_ERRORS');
$errors = [];
if (empty($email)) {
array_push($errors, "Email is required");
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
array_push($errors, "Email must be in a valid format. Example: example@domain.com");
}
if(empty($firstName)){
array_push($errors, "Please enter a first name");
}
if(empty($lastName)){
array_push($errors, "Please enter a last name");
}
if (empty($errors)) {
Session::clear('first_name');
Session::clear('last_name');
Session::clear('email');
Session::clear('company_name');
$comment = new EmailSignUpSubmission();
$comment->FirstName = $firstName;
$comment->LastName = $lastName;
$comment->CompanyName = $c;
$comment->Email = $email;
$comment->write();
} else {
Session::set($this->formErrorsKey, $errors);
Session::set('first_name', $firstName);
Session::set('last_name', $lastName);
Session::set('company_name', $c);
Session::set('email', $email);
}
return $this->redirect("/?redirected=1#signupform");
}
public function getFormErrors() {
$errors = Session::get($this->formErrorsKey);
if ($errors == null || $errors == "") {
return null;
} else {
$errorList = new ArrayList();
foreach ($errors as $error) {
$e = new ArrayData(['Text' => $error]);
$errorList->add($e);
}
return $errorList;
}
}
public function isRedirect() {
$request = $this->getRequest();
return $request->getVar('redirected') == "1";
}
这里是 HTML 表格本身:
<div class="sign-up" id="signupform">
<div class="form-container">
<% if $isRedirect && not $getFormErrors %>
<div class="row">
<p class="success">
Thank you for your submission. You will hear back from us shortly.
</p>
</div>
<% else %>
<form method="post" action="/$Link/submit">
<h2 class="text-center white subscribe-hdr">Sign up</h2>
<% if $getFormErrors %>
<% loop $getFormErrors %>
<p class="error">$Text</p>
<% end_loop %>
<% end_if %>
<p class="white subscribe-body" style="text-align:center;">Sign up for the latest newsletter.</p>
<div class="form-group">
<input class="form-control" type="text" name="first_name" value="$getFirstName" placeholder="First Name">
</div>
<div class="form-group">
<input class="form-control" type="text" name="last_name" value="$getLastName" placeholder="Last Name">
</div>
<div class="form-group">
<input class="form-control" type="text" name="company_name" value="$getCompanyName" placeholder="Company">
</div>
<div class="form-group">
<input class="form-control" type="email" name="email" value="$getEmail" placeholder="Email">
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit"
style="width:140px;margin-left:auto;margin-right:auto; float: none; text-align: center">Submit
</button>
</div>
</form>
<% end_if %>
</div>
</div>
您始终可以使用隐藏字段:
<input type="hidden" value="{$AbsoluteLink}" name="redirectURL" />
然后在您的提交方法中使用它进行重定向。
扩展表单 class 会容易得多,因为您将当前的 Controller 对象传递给表单。 https://docs.silverstripe.org/en/3/developer_guides/forms/introduction/
派对可能有点晚了,但是这会起到同样的作用,同时不需要表单中的隐藏字段。
return $this->redirect($this->AbsoluteLink().'?redirected=1#signupform"');