笨PHP - 在锚点加载视图(Codeigniter PHP - loading a view a

2019-09-22 18:04发布

I have a form at the bottom of a long page, if a user fills out the form but it doesn't validate the page is reloaded in the typical codeigniter fashion:

$this->load->view('template',$data);

however because the form is way down at the bottom of the page I need the page to load down there like you do with HTML anchors. Does anyone know how to do this in codeigniter?

I can't use the codeigniter

redirect();

function because it loses the object and the validation errors are gone. Other frameworks I've used like Yii you can call the redirect function like:

$this->redirect();

which solves the problem because you keep the object. I've tried using:

$this->index()

within the controller which works fine as a redirect but the validation errors are in another method which is where the current page is loaded from:

$this->item($labs)

but when I use this it get stuck in a loop

Any ideas? I've seen this question a lot on the net but no clear answers. I'm researching using codeigniter "flash data" but think it's a bit overkill.

cheers.

Answer 1:

我不能亲自担保这一点,但根据这个线索 ,如果你追加锚表单的action ,它会工作。

CodeIgniter的帮手:

<?php echo form_open('controller/function#anchor'); ?>

或香草HTML:

<form method='post' action='controller/function#anchor'>

如果你是开放使用Javascript,你可以很容易地检测到$validation_failed变量和适当的滚动。 或者,甚至更好,使用AJAX。

另一种选择是把形式靠近页面顶部?



Answer 2:

好了,就我理解你的问题,这是没有太大关系到后端(笨)。 你想在页面底部的形式是“哪些用户-看到,对网页加载”(因为你提到的锚)。

现在,你可以做的是,您可以设置使用验证错误消息的分隔符:

echo validation_errors('<div id="bottom_form_error">', '</div>');

使用jQuery的ScrollTo ,做:

$( function() { $('#bottom_form_error').ScrollTo(); } );

而且,用户将在页面的底部滚动到错误。 不要忘记,包括jQuery的了。

锚散列片段的点击是不同的-它在滚动∞速度。

我希望这是你想要的。


PS我不理你这条线之下说的话:

有谁知道如何在笨做到这一点?

因为我觉得它不是真正的问题有关。



文章来源: Codeigniter PHP - loading a view at an anchor point