Javascript 使用按钮中的id填写表单PHP
我对任何Javascript、AJAX或jQuery都非常熟悉,因此我很难理解人们在其他问题上提出的详细概念 我有一个脚本,它在一个表中显示数据库中的所有用户。此表还包括一个按钮,该按钮的ID取决于其主键。我不知道当按下按钮时如何选择它的所有记录并将其放置在它正下方的表单中。 admin.php 我试图用一段AJAX代码来显示和隐藏它,希望我能找到ID,但我的上一篇文章很难解释,并给出了与我正在寻找的解决方案无关的各种答案 admin.php accessusers.phpJavascript 使用按钮中的id填写表单PHP,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我对任何Javascript、AJAX或jQuery都非常熟悉,因此我很难理解人们在其他问题上提出的详细概念 我有一个脚本,它在一个表中显示数据库中的所有用户。此表还包括一个按钮,该按钮的ID取决于其主键。我不知道当按下按钮时如何选择它的所有记录并将其放置在它正下方的表单中。 admin.php 我试图用一段AJAX代码来显示和隐藏它,希望我能找到ID,但我的上一篇文章很难解释,并给出了与我正在寻找的解决方案无关的各种答案 admin.php accessusers.php 如果我理解正确,您希
如果我理解正确,您希望在单击按钮时根据accessusers.php中显示的表格单元格值以表单形式设置值吗?如果是这样的话,下面的内容可能会有用——它经过了粗略的测试,但可能存在一些问题。需要注意的一点是表单中的SELECT元素没有name属性-我假设它应该被称为accesslevel-即:
您的问题不清楚-上面两个脚本之间的关系是什么?admin.php上的表单是否将其数据发布到accessusers.php?问题中没有显示javascript代码,但您引用的是javascript/jQuery代码。accessusers.php中的HTML代码没有按钮,只有一些数据和指向每个特定用户的超链接user@RamRaiderhtml有一个表单和一个提交按钮,所以是的,有一个关系。他在理解和实现Ajax方面存在问题。@RamRaider accessuser.php将表输出到admin.php页面,其中包括。这就在显示为admin.php的表单之前。这是测试版本,但现在是。@Xorifelse-表单不会直接发布到accessusers.php,因为该操作是这样的。如果表单发布到该脚本,则必须使用未显示的javascript来完成。@ParagonJenko-您将需要使用javascript根据您按下的超链接/按钮设置各种表单值-使用userid作为参数传递给处理请求的php脚本
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Append User</h3>
</div>
<div class="panel-body">
<form id="loginForm" method="post" class="form-horizontal" action="#">
<div class="form-group">
<label class="col-sm-4 control-label" for="accesslevel">Access Level</label>
<div class="col-sm-5">
<select>
<option value="0">No Admin Access</option>
<option value="1">Admin Access</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="username">Username</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="username" name="username" placeholder="Username"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="password">Password</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="password" name="password" placeholder="Password"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="email">Email</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="email" name="email" placeholder="Email"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="securityanswer">Security Answer</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="securityanswer" name="securityanswer" placeholder="Security Answer"/>
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-4">
<button type="submit" class="btn btn-primary" name="append" value="Append">Append</button>
</div>
</div>
</form>
</div>
</div>
echo "<table class='table text-center'>
<tr>
<th>ID</th>
<th>Access Level</th>
<th>Username</th>
<th>Password</th>
<th>Email</th>
<th>Security Answer</th>
<th>Date Joined</th>
<th>Edit Row</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['usersid'] . "</td>";
echo "<td>" . $row['accesslevel'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['securityanswer'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td><a id='" . $row['userid'] . "'><span class='glyphicon glyphicon-edit'></span></a></td>";
echo "</tr>";
}
echo "</table>";
<script>
/*
Event handler assigned to each button in the table found within `accessusers.php`
*/
function setvalues(e){
/*
column names / form fields - not all get populated such a userid
*/
var columns=['usersid','accesslevel','username','password','email','securityanswer','date'];
var tr=this.parentNode.parentNode;
var col=tr.querySelectorAll('td');
var form=document.getElementById('loginForm');
/* iterate through all table cells found on this row */
for( var n in col ){
if( col[ n ].nodeType==1 ) {
/*
if there is a form element in `admin.php` with this name,
add the value from the table cell that corresponds
*/
if( typeof( columns[ n ] )!='undefined' ){
if( typeof( form[ columns[ n ] ] ) == 'object' ){
var field=form[ columns[ n ] ];
if( field.tagName=='SELECT' ) field.selectedIndex=col[ n ].innerHTML;
else field.value=col[ n ].innerHTML;
}
}
}
}
}
/*
Assign event handlers to the buttons
*/
document.addEventListener('DOMContentLoaded',function(evt){
var table=document.querySelectorAll('table[class="table text-center"]')[0];
/*
below assumes the hyperlinks have been replaced with buttons
*/
var bttns=table.querySelectorAll('input[type="button"]');
if( bttns ){
for( var b in bttns ){
var bttn=bttns[ b ];
if( bttn.nodeType==1 )bttn.onclick=setvalues.bind( bttn );
}
}
},false);
</script>