dongli8466 2013-04-18 11:33
浏览 58
已采纳

检查用户在服务器端php上单击的动态按钮(输入类型按钮)

First of all I want to thanks all of you guys for helping every day , answering questions. You have save me a lot of time and I have learn a lot.

I have let's say this buttons on a form.

<button type="button" value="Adventurous" name="adv" id="adv">Adventurous</button>
<button type="button" value="Discovery" name="disc" id="disc">Discovery</button>
<button type="button" calue="Easy-going" name="easy" id="easy">Easy-going</button>

When the user clicks on a button to specify how he feels in our example I have the follow code on jquery to change the state of the button as checked and change the class (to be shown with different colors etc)

$(".tailor_field .tailor_mood_btn button").click(function() {
         var checked = $(this).attr('checked');
         $the_image = $(this).find("img");
        if(checked){
            $(this).removeClass("mood_on");
            $(this).attr('checked', false);
            $the_image.attr("src", templateDir+"/images/btn_on.png");
        }
        else{
            $(this).addClass("mood_on");
            $(this).attr('checked', true);
            $the_image.attr("src", templateDir+"/images/btn_off.png");
        }
    });

So when the user press submit I want to check on server side (php) which buttons the user clicked ?

Second I would like to insert the buttons dynamically. Ie to have a string on my DB, explode it to an array and loop each element to create the buttons etc. How I will know what to check if I don't know how many buttons will be and their names from the beginning ? Maybe with sessions (I would like to avoid it if there is another solution)?

Thanks a lot!

  • 写回答

2条回答 默认 最新

  • dqe9657 2013-04-18 12:35
    关注

    I would do it this way:

    var checkedButtons = $(".tailor_field .tailor_mood_btn button[checked=true]");
    
    $("form").submit(function() {
      var data = [];
      checkedButtons.each(function(index, element) {
        var name = element.attr('name');
        var value = element.val();
        data[name] = value;
      });
    
      $.post('file.php', data);
    });
    

    The data variable is an array with keys as the names of clicked buttons, an values of values of those buttons. Later an ajax request is made to the file that needs this data (file.php) where the data array is available in the superglobal $_POST (ex. $_POST['adv'] = "Adventurous")

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3