CakePHP,表单助手标记问题与页面化结果

时间:2012-11-28 22:53:37

标签: jquery forms cakephp

我需要的是有人看到为什么CakePHP(Ver 2.2.2)正在做它正在做的事情?好的,我的代码细分如下。 DIV PageList是一个JQuery Accordion,有两个下拉菜单,一个用于管理员添加新用途,另一个用于查看所有用户并进行一些更改。添加用户很好,工作没有任何问题。

我的问题是(或者至少我认为是)与Form Helper一起,我之前在foreach循环之外确实有Form Create调用,并且没有任何问题,我会找到我想改变的用户然后保存,没问题。当我说要对结果进行总结时,我开始遇到问题。当表单创建在foreach之外且在表格代码开始之前时,只有显示的最后结果才能保存所做的更改。我找到了问题的原因,那就是ID字段总是默认为上次显示的结果。所以我将表单创建移动到我的foreach循环中。

这现在有效,但出于某种原因,它立即开始结束,因此我的输入现在位于表单标签之外?我不明白为什么因为我在foreach循环的底部有结束Form Helper end()调用。因此,它应该以自己的形式显示每个用户。

我不确定这是否是一个问题,仍然可以像这样使用它。但是一个问题是我的表单现在在我的表之后开始,它不会与W3C一起验证,这不是一个大问题。旁注,是否可以使用表格呢?我正在列出他们的数据?我正在尝试学习HTML 5规范。但是还没有看到关于表的任何事情......

非常感谢。格伦柯蒂斯

echo '<div id="PageList">';

$OutputHolder = '<h3 class="Background-Titles">Add User</h3>';
$OutputHolder .= '<div class="Background-Content" id="AdminAddUser">';
$OutputHolder .=  $this->Form->create('User', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminadd/', 'inputDefaults' => array('div' => false) ) ); 
$OutputHolder .=  $this->Form->input('username');
$OutputHolder .=  $this->Form->input('password');
$OutputHolder .=  $this->Form->input('firstname');
$OutputHolder .=  $this->Form->input('surname');
$OutputHolder .=  $this->Form->input('email');
$OutputHolder .= 'Select Role For User <br />';
$OutputHolder .=  $this->Form->radio('role_id', $opt_role, $attributes);
$OutputHolder .= '<br />Select Active State For User <br />';
$OutputHolder .=  $this->Form->radio('active', $opt_active, $attributes);
$OutputHolder .=  $this->Form->end('Submit'); 
$OutputHolder .= '</div>'; //End for AddUser
echo $OutputHolder;

$OutputViewUsers = '<h3 class="Background-Titles">View All Users</h3>';
$OutputViewUsers .= '<div class="Background-Content" id="AdminViewUsers">';

$OutputViewUsers .=  '<table border="0">';
$OutputViewUsers .=  '<tr><td>Username</td><td>First Name</td><td>Surname</td><td>E-mail</td><td>Stutus</td><td>Level</td><td>Save</td></tr>';
foreach($users as $key => $value) {
    $OutputViewUsers .= $this->Form->create('UserEdit', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminedit/', 'inputDefaults' => array('div' => false) ) );
    $BaseLink = $users[$key]['User']; 
    $OutputViewUsers .= "<td class=\"AdminViewUsersUN\">" .($BaseLink  ['username']) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('fname', array('default' => $BaseLink  ['firstname'], 'label' => false,  'class' => 'AdminViewUsersFN') ) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('sname', array('default' => $BaseLink  ['surname'], 'label' => false,  'class' => 'AdminViewUsersSN') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('email', array('default' => $BaseLink  ['email'], 'label' => false,  'class' => 'AdminViewUsersEM') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('active', $ActOpts, array('empty' => false, 'default' => $BaseLink  ['active'], 'class' => 'AdminViewUsersAS') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('role', $RoleOpts, array('empty' => false, 'default' => $users[$key]['Role']['id'],  'class' => 'AdminViewUsersUL') ). "</td>";
    $OutputViewUsers .= $this->Form->hidden('id', array('default' => $BaseLink  ['id'], ) );
    $OutputViewUsers .= "<td>" . $this->Form->submit('Save', array('div' => false, 'class' => 'AdminViewUsersSB') ). "</td></tr>"; 
    //$test = array('label' => 'Save', 'div' => array('class' => 'AdminViewUsersSB'));
    $OutputViewUsers .=  $this->Form->end(); 

} //End of foreach loop
$OutputViewUsers .= '</table>';

$OutputViewUsers .= $this->Paginator->numbers(array('first' => 2));
$OutputViewUsers .= '</div>'; //End for AllUsers 
echo $OutputViewUsers;

echo '</div>'; //End for PageList

更新:::

这就是它现在的样子!但现在,这更多或更多,以免我以前如何拥有它。我添加了缺失的标签。但现在这只能让最后的结果奏效!我每页分页5个结果,页面上的前四个结果不起作用,因为它们有错误的ID号,每次按下任何“保存”,只提供最后一个结果的id。

我不确定你的意思是“输入输入字段的索引”,但是“给出提交元素包含相关记录的相应索引的唯一名称”,据我所知,我正在这样做,提供ID的隐藏字段。

我对我的看法是,我必须将整个表单放在我的循环中,以便每个返回的结果都是以自己的形式存在,那么应该没有错误!但是我不能把我的表的开头放在循环中,或者每个结果都有一个标题!我想我应该可以解决这个问题!

非常感谢您的帮助!

$OutputViewUsers = '<h3 class="Background-Titles">View All Users</h3>';
$OutputViewUsers .= '<div class="Background-Content" id="AdminViewUsers">';
$OutputViewUsers .= $this->Form->create('UserEdit', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminedit/', 'inputDefaults' => array('div' => false) ) );
$OutputViewUsers .=  '<table border="0">';
$OutputViewUsers .=  '<tr><td>Username</td><td>First Name</td><td>Surname</td><td>E-mail</td><td>Stutus</td><td>Level</td><td>Save</td></tr>';
foreach($users as $key => $value) {
    $BaseLink = $users[$key]['User']; 
    $OutputViewUsers .= "<tr><td class=\"AdminViewUsersUN\">" .($BaseLink  ['username']) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('fname', array('default' => $BaseLink  ['firstname'], 'label' => false,  'class' => 'AdminViewUsersFN') ) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('sname', array('default' => $BaseLink  ['surname'], 'label' => false,  'class' => 'AdminViewUsersSN') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('email', array('default' => $BaseLink  ['email'], 'label' => false,  'class' => 'AdminViewUsersEM') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('active', $ActOpts, array('empty' => false, 'default' => $BaseLink  ['active'], 'class' => 'AdminViewUsersAS') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('role', $RoleOpts, array('empty' => false, 'default' => $users[$key]['Role']['id'],  'class' => 'AdminViewUsersUL') ). "</td>";
    $OutputViewUsers .= $this->Form->hidden('id', array('default' => $BaseLink  ['id'], ) );
    $OutputViewUsers .= "<td>" . $this->Form->submit('Save', array('div' => false, 'class' => 'AdminViewUsersSB') ). "</td></tr>"; 
} //End of foreach loop
$OutputViewUsers .= '</table>';
$OutputViewUsers .=  $this->Form->end(); 
$OutputViewUsers .= $this->Paginator->numbers(array('first' => 2));
$OutputViewUsers .= '</div>'; //End for AllUsers 
echo $OutputViewUsers;

另一个更新::

这现在似乎有效,每个结果都有自己的表单元素和所有输入或现在在表单标记内。但是,每个结果现在都有自己的标题,因为我现在在表单中有表,但现在在我的foreach循环中,所以每次循环时,它会回显/打印结果顶部的标题。我正在研究这个问题。任何帮助仍然是最受欢迎的!!

foreach($users as $key => $value) {
    $OutputViewUsers .= $this->Form->create('UserEdit', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminedit/', 'inputDefaults' => array('div' => false) ) );
    $OutputViewUsers .=  '<table border="0">';
    $OutputViewUsers .=  '<tr><td>Username</td><td>First Name</td><td>Surname</td><td>E-mail</td><td>Stutus</td><td>Level</td><td>Save</td></tr>';
    $BaseLink = $users[$key]['User']; 
    $OutputViewUsers .= "<tr><td class=\"AdminViewUsersUN\">" .($BaseLink  ['username']) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('fname', array('default' => $BaseLink  ['firstname'], 'label' => false,  'class' => 'AdminViewUsersFN') ) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('sname', array('default' => $BaseLink  ['surname'], 'label' => false,  'class' => 'AdminViewUsersSN') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('email', array('default' => $BaseLink  ['email'], 'label' => false,  'class' => 'AdminViewUsersEM') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('active', $ActOpts, array('empty' => false, 'default' => $BaseLink  ['active'], 'class' => 'AdminViewUsersAS') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('role', $RoleOpts, array('empty' => false, 'default' => $users[$key]['Role']['id'],  'class' => 'AdminViewUsersUL') ). "</td>";
    $OutputViewUsers .= $this->Form->hidden('id', array('default' => $BaseLink  ['id'], ) );
    $OutputViewUsers .= "<td>" . $this->Form->submit('Save', array('div' => false, 'class' => 'AdminViewUsersSB') ). "</td></tr>"; 
    //$test = array('label' => 'Save', 'div' => array('class' => 'AdminViewUsersSB'));
    $OutputViewUsers .= '</table>';
    $OutputViewUsers .=  $this->Form->end(); 

} //End of foreach loop

2 个答案:

答案 0 :(得分:1)

您只是生成无效的HTML。您在循环中缺少一个开始tr标记,但即使它存在,它仍然无效,因为tr is only allowed to hold td or th elements,因此浏览器可能会在表格之外呈现表单。

另见Form inside a table

一种解决方法是将整个表包装在表单中,为输入索引输入字段,为submit元素提供包含相关记录的相应索引的唯一名称,并在服务器端使用此名称来确定哪个记录到保存。或者将提交的数据与数据库进行比较,并仅保存更改内容的记录。

PS。是的,表格很好,只要它们用于它们的目的,用于描述表格数据。

答案 1 :(得分:0)

我已将上述标记作为答案,因为它让我走上正轨,非常感谢!

我现在在循环中有表单,表单里面有表格。现在,这意味着每个结果都有自己的表单标记。

使用标题我刚刚使用CSS和span标记来排列它们!

非常感谢格伦。

这是我的最终代码,以防任何其他人发现任何用途。

$OutputViewUsers = '<h3 class="Background-Titles">View All Users</h3>';
$OutputViewUsers .= '<div class="Background-Content" id="AdminViewUsers">';
$OutputViewUsers .=  '<div class="AdminViewUsersListHeaders">
    <span class="AdminViewUsersUNHeader">Username</span>
    <span class="AdminViewUsersFNHeader">First Name</span>
    <span class="AdminViewUsersSNHeader">Surname</span>
    <span class="AdminViewUsersEMHeader">E-mail</span>
    <span class="AdminViewUsersASHeader">Stutus</span>
    <span class="AdminViewUsersULHeader">Level</span>
    <span class="AdminViewUsersSAHeader">Save</span></div>';


foreach($users as $key => $value) {
    $OutputViewUsers .= $this->Form->create('UserEdit', array('url'=>'http://glenns-showcase.net/projects/cake/DrWho/adminedit/', 'inputDefaults' => array('div' => false) ) );
    $OutputViewUsers .=  '<table border="0">';
    //$OutputViewUsers .=  '<tr><td>Username</td><td>First Name</td><td>Surname</td><td>E-mail</td><td>Stutus</td><td>Level</td><td>Save</td></tr>';
    $BaseLink = $users[$key]['User']; 
    $OutputViewUsers .= "<tr><td class=\"AdminViewUsersUN\">" .($BaseLink  ['username']) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('fname', array('default' => $BaseLink  ['firstname'], 'label' => false,  'class' => 'AdminViewUsersFN') ) . "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('sname', array('default' => $BaseLink  ['surname'], 'label' => false,  'class' => 'AdminViewUsersSN') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->input('email', array('default' => $BaseLink  ['email'], 'label' => false,  'class' => 'AdminViewUsersEM') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('active', $ActOpts, array('empty' => false, 'default' => $BaseLink  ['active'], 'class' => 'AdminViewUsersAS') ). "</td>";
    $OutputViewUsers .= "<td>" . $this->Form->select('role', $RoleOpts, array('empty' => false, 'default' => $users[$key]['Role']['id'],  'class' => 'AdminViewUsersUL') ). "</td>";
    $OutputViewUsers .= $this->Form->hidden('id', array('default' => $BaseLink  ['id'], ) );
    $OutputViewUsers .= "<td>" . $this->Form->submit('Save', array('div' => false, 'class' => 'AdminViewUsersSB') ). "</td></tr>"; 
    //$test = array('label' => 'Save', 'div' => array('class' => 'AdminViewUsersSB'));
    $OutputViewUsers .= '</table>';
    $OutputViewUsers .=  $this->Form->end(); 

} //End of foreach loop


$OutputViewUsers .= $this->Paginator->numbers(array('first' => 2));
$OutputViewUsers .= '</div>'; //End for AllUsers 
echo $OutputViewUsers;