设置表单以在提交时重定向到当前页面(SilverStripe / PHP)

时间:2017-06-08 16:59:34

标签: php forms silverstripe

我有一个SilverStripe网站的注册表单,可以处理服务器端的所有内容。最初,它只会在主页上,所以设置我工作得很好。但随后需求发生变化,表格也需要出现在子页面上。除了我为action参数设置的内容外,表格总是提交到主页,一切仍然有效。

最初,action参数为" / home / submit。"我将其更改为接受一个返回当前页面网址的变量并附加" / submit"通过创建一个名为Link的函数来实现它(参见下面的代码)。这似乎有效并将正确的url放入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: 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>

2 个答案:

答案 0 :(得分:1)

您可以随时使用隐藏字段:

<input type="hidden" value="{$AbsoluteLink}" name="redirectURL" />

然后在提交方法中使用它来重定向。

通过扩展Form类可以更容易,因为您将当前的Controller对象传递给表单。 https://docs.silverstripe.org/en/3/developer_guides/forms/introduction/

答案 1 :(得分:0)

参加聚会可能会有点晚,但是这样做不需要表单中的隐藏字段。

return $this->redirect($this->AbsoluteLink().'?redirected=1#signupform"');