Php 将元素ID传递到数组中以进行后期表单处理
假设我有一系列用php循环创建的图像->Php 将元素ID传递到数组中以进行后期表单处理,php,javascript,jquery,Php,Javascript,Jquery,假设我有一系列用php循环创建的图像-> <img src="http://..._1.jpg" class="profile-thumb" id="12345678"> <img src="http://..._2.jpg" class="profile-thumb" id="12345677"> <img src="http://..._3.jpg" class="profile-thumb" id="12345676"> 因此,当您单击图像时,它会将“
<img src="http://..._1.jpg" class="profile-thumb" id="12345678">
<img src="http://..._2.jpg" class="profile-thumb" id="12345677">
<img src="http://..._3.jpg" class="profile-thumb" id="12345676">
因此,当您单击图像时,它会将“.active”类添加到图像元素->
<img src="http://..._3.jpg" class="profile-thumb active" id="12345676">
然后如何使用foreach循环读取POST数组
解决方案:
<button id="share" class="off">
<span>Sharer</span>
</button>
<div id="sharer">
<!-- Array will echo here -->
</div>
$("#share").click(function () {
//Create var array
var list = $('.active-profile').map(function(){return this.id;}).toArray();
$.post('sharer.php', {ids:list}, function(data) {
$('#sharer').html(data);
});
});
HTML:
<button id="share" class="off">
<span>Sharer</span>
</button>
<div id="sharer">
<!-- Array will echo here -->
</div>
$("#share").click(function () {
//Create var array
var list = $('.active-profile').map(function(){return this.id;}).toArray();
$.post('sharer.php', {ids:list}, function(data) {
$('#sharer').html(data);
});
});
PHP(sharr.PHP)
javascript数组(列表)通过jquery POST发送,并在#sharr div中回送
$('.profile-thumb.active').map(function(){return this.id}).toArray();
此外,如果hasClass removeClass else addClass,您可以简单地使用:
$(".profile-thumb").click(function () {
$(this).toggleClass("active");
});
演示:要将值数组发送到php,需要使用稍微不同的语法。对于每个id,您可以创建一个类似的url
http://test.com/?img_id[]=1&img_id[]=2&img_id[]=3
这将让您在php中以数组的形式获取值img_id
$_POST['img_id'] // array of values.
foreach ($_POST['img_id'] as $id) {
print $id;
}
// 1
// 2
// 3
为您的帮助干杯,我已经编辑了这个问题,以获取有关读取数组的更多信息。@reekogi使用(
array\u of_id
,如我的答案/提琴中所定义的)。在服务器端,循环浏览$\u POST['id']
array。我看不到你在其中定义了“id:array of id”,你指的是什么?@reekogi数组是在我回答的第一行定义的。在我的小提琴中,数组被称为list
。
$_POST['img_id'] // array of values.
foreach ($_POST['img_id'] as $id) {
print $id;
}
// 1
// 2
// 3