我有一個SilverStripe網站的註冊表格,它處理服務器端的所有內容。最初,它只會在主頁上,所以我的設置工作得很好。但隨後需求發生變化,並且表單也需要在子頁面上顯示。無論我爲action
參數設置了什麼,一切仍然有效,除非表單始終提交到主頁。設置表單重定向到提交當前頁面(SilverStripe/PHP)
最初,action
參數是「/ home/submit」。我將它改爲接受一個返回當前頁面url的變量,並通過創建一個名爲Link
(參見下面的代碼)的函數向其添加「/ submit」。這似乎工作,並把正確的網址到action
參數。
但是當你點擊提交按鈕時,表單仍然會將用戶發回主頁,這不是我想要的。我希望他們留在表單所在的當前頁面上(無論是主頁還是任何子頁面)。
我想獲取當前URL,並使用它提交功能,像這樣的最後一行:
$current = $this->get_current_page()->Link();
return $this->redirect("$current/?redirected=1#signupform");;
但是,這會將用戶帶到一個不正確的網址: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: [email protected]");
}
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>
我走了隱藏的領域的方法。我同意SS表單API會更容易,但這是一個匆忙的項目。將來,我打算更多地使用表單API。 –