我无法获取未知文本输入的值。我目前有一个for循环,它将遍历mysql查询返回的行数组。返回的数据将被迭代并显示在页面上。每行包含自己的表单和按钮。如果单击该按钮,我希望它能够获取阵列中同一行内的值并将它们发送到另一个页面。我的问题是,由于行数未知,我如何才能获得只是单击按钮的行内的值?
编辑:
正如您在图像中看到的那样,手风琴及其内部卡片由循环生成。卡的数量取决于MySQL查询返回的行数。为了验证我的问题,每个“卡”都有输入,其中值也来自MySQL查询,因此,如果用户点击“编辑客户端”,我只想获取其卡内的文本框的值。以下是生成每张卡片的代码:
代码:
$counter = 0;
foreach($clients as $client) {
echo '
<div class="card">
<div class="card-header" role="tab" id="heading'. $counter .'">
<h5 class="mb-0">
<div class="row">
<div class="d-flex align-items-center schedule-flex">
<div class="col-8 col-md-10">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse'. $counter .'" aria-expanded="true" aria-controls="collapse'. $counter .'">
'. $client['fName'] .' '. $client['lName'] .'
</a>
</div>
</div>
</div>
</h5>
</div>
<div id="collapse'. $counter .'" class="collapse" role="tabpanel" aria-labelledby="heading'. $counter .'">
<div class="card-block">
<form action="editClient.php">
<div class="row">
<div class="col-12 col-md-6">
<label for="fname">First Name</label>
<input name="fname" class="form-control" type="text" value="'. $client['fName'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="lname">Last Name</label>
<input name="lname" class="form-control" type="text" value="'. $client['lName'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="address">Address</label>
<input name="address" class="form-control" type="text" value="'. $client['address'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="phone">Phone Number</label>
<input name="phone" class="form-control" type="text" value="'. $client['telephone'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="email">Email Address</label>
<input name="email" class="form-control" type="text" value="'. $client['email'] .'" disabled>
</div>
</div>
<button class="btn btn-lg btn-primary btn-block schedule-submit" type="submit">Edit Client</button>
<br />
</form>
</div>
</div>
</div>
';
$counter++;
答案 0 :(得分:0)
到目前为止,我可以看到我认为您提交表单时遇到问题。根据您的描述和代码,您似乎正在生成多种形式。
method = "post"
,所以我添加了操作。 以下是代码:
$counter = 0;
foreach($clients as $client) {
echo '
<div class="card">
<div class="card-header" role="tab" id="heading'. $counter .'">
<h5 class="mb-0">
<div class="row">
<div class="d-flex align-items-center schedule-flex">
<div class="col-8 col-md-10">
<a data-toggle="collapse" data-parent="#accordion" href="#collapse'. $counter .'" aria-expanded="true" aria-controls="collapse'. $counter .'">
'. $client['fName'] .' '. $client['lName'] .'
</a>
</div>
</div>
</div>
</h5>
</div>
<div id="collapse'. $counter .'" class="collapse" role="tabpanel" aria-labelledby="heading'. $counter .'">
<div class="card-block">
<form action="editClient.php" method="post" id="form'. $counter .'">
<div class="row">
<div class="col-12 col-md-6">
<label for="fname">First Name</label>
<input name="fname" class="form-control" type="text" id="fname'. $counter .'" value="'. $client['fName'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="lname">Last Name</label>
<input name="lname" class="form-control" type="text" id="lname'. $counter .'" value="'. $client['lName'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="address">Address</label>
<input name="address" class="form-control" type="text" id="address'. $counter .'" value="'. $client['address'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="phone">Phone Number</label>
<input name="phone" class="form-control" type="text" id="phone'. $counter .'" value="'. $client['telephone'] .'" disabled>
</div>
<div class="col-12 col-md-6">
<label for="email">Email Address</label>
<input name="email" class="form-control" type="text" id="email'. $counter .'" value="'. $client['email'] .'" disabled>
</div>
</div>
<button class="btn btn-lg btn-primary btn-block schedule-submit edit_client_button" id="'. $counter .'" type="submit">Edit Client</button>
<br />
</form>
</div>
</div>
</div>
';
$counter++;
// Saperatedly add the Script to with your javascript files. I have added here just for example. put it in a right place.
<script type="text/javascript">
$(".edit_client_button").click(function() {
var buttonid = this.id;
$( "#form"+buttonid ).submit();
});
</script>
我在每个输入中添加了id
属性,您希望从中获取点击价值。
更新了用于在按钮单击时获取输入值的脚本。
<script type="text/javascript">
$(".edit_client_button").click(function() {
var buttonid = this.id;
var fname = $('#fname'+buttonid); //Kevin
var lname = $('#lname'+buttonid); //Smith
var address = $('#address'+buttonid); //1212 Test Street
var phone = $('#phone'+buttonid); //8472934569
var email = $('#email'+buttonid); //kevin.smith@gmail.com
// This variabls contail the input value which you want on click.
// Use console.log(); to see the value or simply alert();
// example: console.log(email); or alert(email);
});
</script>
根据你的评论,我没有帮助你。你提到你需要一个值表单输入。 例如:
如果按钮,&#34;编辑客户端&#34;凯文史密斯内部点击,我会 喜欢我的PHP脚本来获取文本框内的值,即 Kevin,Smith,1212 Test Street,2929292929和test.test@gmail.com。
我建议的脚本为您提供表单中的所有值,但是如果您想单独获取该输入值,我会更新答案代码,请检查。