如何在php中存储哪个div?

时间:2014-11-30 23:59:12

标签: php

我有一堆div在点击时点亮(但只能点击一个div)。我想知道当用户按下"提交"时,点击了哪个div。使用PHP的按钮,虽然我不确定是否可以这样做。

这是我点击的代码:

<script>
   $( function() {
$('.column').click( function() {
$('.column').css('background-color',"");
$(this).css('background-color', '#A3C2C2');
  } );
} );
   </script>

然后我有了div:

<div class="column" onclick="function()" style="background-color: rgb(163, 194, 194);">
<p>Burgers</p>
<div class="avatar">
<img src="img/burgers.ico" alt="" class="img-responsive"></div></div>

<div class="column" onclick="function()" style="background-color: rgb(163, 194, 194);">
<p> Tacoes</p><div class="avatar">
<img src="img/taco.ico" alt="" class="img-responsive"></div></div>

<div class="column" onclick="function()" style="background-color: rgb(163, 194, 194);">
<p> Tacoes</p><div class="avatar">
<img src="img/taco.ico" alt="" class="img-responsive"></div></div>

用户只能点击一个div - 有没有办法使用PHP找出点击了哪个div?我之所以想用php而不是javascript是因为我想存储这个点击的变量来对mysql表进行查询

1 个答案:

答案 0 :(得分:0)

为您的div提供有意义的ID:

<div id="burger" class="column" onclick="function()" style="background-color: rgb(163, 194, 194);">

通过ajax扩展您的脚本:

<script>
$( function() {
   $('.column').click( function() {
     $('.column').css('background-color',"");
     $(this).css('background-color', '#A3C2C2');
        $.ajax({
           url:'somefile.php',
           type:'post',
           data: { clickeddiv : $(this).attr('id') },
           success : function(e) {
                     alert ('saved'); } // or whatever you want as success, maybe here nothing 

   });
  });
});
</script>

并使用以下命令捕获somefile.php中的数据:

$_POST['clickeddiv'] 

并将其保存到表格或处理它,但您需要。