如何从HTML表中获取数据并将其放在PHP文本字段中
how to get data from html table and put it in php text fields?
这是我创建的表:
<table class="table table-striped table-bordered" id="example">
<thead>
<?php
foreach ($fields as $field_name => $field_display){
echo "<th>".$field_display."</th>";
}
?>
</thead>
<tbody>
<?php
foreach ($users as $row){
echo "<tr>";
foreach($fields as $field_name => $field_display){
if($field_name == 'username'){
echo "<td><a href='#' onclick='"edituserdata('".$row->username."')'">".$row->$field_name."</a></td>";
}else{
echo "<td>".$row->$field_name."</td>";
}
}
echo "</tr>";
}
?>
</tbody>
</table>
这是当我点击一行时出现的引导模式:
<!-- Modal -->
<div id="myModal1" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Update you're data</h3>
</div>
<div class="modal-body">
<?php echo form_open('main/update'); ?>
<?php
$name = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name')
);
?>
<div class="control-group">
<label class = "control-label" for="name">Name:</label>
<div>
<input type="text" name="name" placeholder="Name" id="name" value="">
<?php echo form_error('name', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Username:</label>
<div>
<input type="text" name="username" placeholder="Username" id="username" value="">
<?php echo form_error('username', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Password: </label>
<div>
<input type="password" name="password" placeholder="Password" id="password" value="">
<?php echo form_error('password', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Retype password: </label>
<div>
<input type="password" name="password1" placeholder="Retype password" id="password1" value="">
<?php echo form_error('password1', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Email address: </label>
<div>
<input type="email" name="email" placeholder="Email" id="email" value="">
<?php echo form_error('email', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Phone number: </label>
<div>
<input type="tel" name="tel" placeholder="Phone number" id="phone_number" value="">
<?php echo form_error('tel', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Description: </label>
<div>
<input type="text" placeholder="Description" name="descr" id="description" value="">
<?php echo form_error('descr', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
</div>
<div class="modal-footer">
<div class="container-fluid">
<div class="row-fluid">
<div class="span5" id="message_add_user_id">
</div>
<div class="span7">
<button id="add_user_submit" type="button" class="btn btn-success">Update <i class="icon-white icon-check"></i> </button>
<button class="btn" onClick="document.location.reload(true)" data-dismiss="modal" >Close</button>
</div>
</div>
</div>
<?php echo form_close();?>
,下面是将要出现模态的函数:
function edituserdata(username){
$('#myModal1').modal('show');
}
,当我点击一行,我想有该用户的数据在引导模式的文本字段。我该怎么做呢?
您可能希望像这样通过ajax完成所有操作:
table html page
<table class="table table-striped table-bordered" id="example">
<thead>
<?php
foreach ($fields as $field_name => $field_display){
echo "<th>$field_display</th>";
}
?>
</thead>
<tbody>
<?php
foreach ($users as $row){
echo "<tr>";
foreach($fields as $field_name => $field_display){
if($field_name == 'username'){
echo "<td><a href='#' onclick='"edituserdata('".$row->username."')'">".$row->$field_name."</a></td>";
} else {
echo "<td>".$row->$field_name."</td>";
}
}
echo "</tr>";
}
?>
</tbody>
</table>
<!-- Modal -->
<div id="myModal1" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Update you're data</h3>
</div>
<div class="modal-body">
<!-- NOTE THE EMPTY MODAL -->
</div>
<div class="modal-footer">
<div class="container-fluid">
<div class="row-fluid">
<div class="span5" id="message_add_user_id">
</div>
<div class="span7">
<button id="add_user_submit" type="button" class="btn btn-success">Update <i class="icon-white icon-check"></i> </button>
<button class="btn" onClick="document.location.reload(true)" data-dismiss="modal" >Close</button>
</div>
</div>
</div>
</div>
</div>
Ajax页面<?php echo form_open('main/update'); ?>
<?php
// TODO get username from $_GET['username'] and then retrieve all user information from db
// TODO echo user data into form fields
$name = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name')
);
?>
<div class="control-group">
<label class = "control-label" for="name">Name:</label>
<div>
<input type="text" name="name" placeholder="Name" id="name" value="">
<?php echo form_error('name', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Username:</label>
<div>
<input type="text" name="username" placeholder="Username" id="username" value="">
<?php echo form_error('username', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Password: </label>
<div>
<input type="password" name="password" placeholder="Password" id="password" value="">
<?php echo form_error('password', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Retype password: </label>
<div>
<input type="password" name="password1" placeholder="Retype password" id="password1" value="">
<?php echo form_error('password1', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Email address: </label>
<div>
<input type="email" name="email" placeholder="Email" id="email" value="">
<?php echo form_error('email', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Phone number: </label>
<div>
<input type="tel" name="tel" placeholder="Phone number" id="phone_number" value="">
<?php echo form_error('tel', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<div class="control-group">
<label class = "control-label" for="name">Description: </label>
<div>
<input type="text" placeholder="Description" name="descr" id="description" value="">
<?php echo form_error('descr', '<span class="alert-error help-inline">', '</span>')?>
</div>
</div>
<?php echo form_close();?>
Javascript在table page
<script>
function edituserdata(username) {
$.get('url-to-ajax-page', {username: username}, function(resp) {
$('#myModal1 .modal-body').html(resp);
$('#myModal1').modal('show');
});
}
</script>
我们在这里做的是创建一个包含用户名的表。当您单击用户名时,它会对页面进行ajax调用,该页面使用用户数据填充表单数据,并在模态中显示。
这应该可以让你开始了。至于提交表单,我就没时间写了但你要做的是,当他们点击提交按钮时,有另一个ajax查询它会将数据发送到相同的ajax url。当数据返回时,如果发布的数据中有错误,则用ajax响应替换模型体的内容,否则关闭窗口。
相关文章:
- 而循环只设置php中输入字段中的第一个值
- 将phonegap图像发布到php/mysql-图像字段从发布请求中消失
- 带有条件字段的PHP表单
- 在第一页加载时隐藏字段,而不是在php发布之后
- 如何防止丢失PHP表单中输入的输入字段
- Javascript/ajax 不向 php 发送空字段
- 将一个html表转换为json对象并将其发送到php页面,该表有一个输入字段
- 获取所有输入字段并保留文本字段中的值,即使在使用 javascript 和 php 提交按钮后也是如此
- 使用jquery UI next按钮提交输入字段值,php将返回结果
- Javascript,PHP,保存字段值
- 使用DB值动态添加字段到Form,php
- 动态添加输入字段,但字段是由外部PHP函数生成的
- 自动完成PHP表单选择字段中的用户姓氏
- AJAX/JS:抓取多个输入字段而不刷新或点击按钮,php会回显该值
- 在php中选择使用onclick without按钮的字段
- "如何使用jQuery's form.serialize但排除空字段"不起作用(使用php)
- PHP通过AJAX更新字段值
- 将使用 json 预填充 php 字段
- 更新PHP 字段与GMaps标记位置
- 将php字段添加到电子邮件中