2013-04-18 11:33
浏览 46


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");
            $(this).attr('checked', false);
            $the_image.attr("src", templateDir+"/images/btn_on.png");
            $(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!

图片转代码服务由CSDN问答提供 功能建议

首先,我要感谢你们所有人每天都在帮助,回答问题。 你节省了我很多时间,而且我已经学到了很多东西。


   &lt; button type =“button”value =“Adventurous”name =“adv”id =“adv”&gt; Adventurous&lt; / button&gt; 
&lt; button type =“button”value =“Discovery”name =“disc”id  =“disc”&gt;发现&lt; / button&gt; 
&lt; button type =“button”calue =“Easy-going”name =“easy”id =“easy”&gt; Easy-going&lt; / button&gt; 

当用户点击按钮指定他在我们的示例中的感觉时,我在jquery上有以下代码,将按钮的状态更改为已选中并更改类(更改为 用不同的颜色等显示)

  $(“。tailor_field .tailor_mood_btn button”)。click(function(){
 var checked = $(this).attr(  'checked'); 
 $ the_image = $(this).find(“img”); 
 $(this)。  attr('checked',false); 
 $ the_image.attr(“src”,templateDir +“/ images / btn_on.png”); 
 else {
 $ the_image.attr(“src”,templateDir +“/ images  /btn_off.png");

所以当用户按提交时我想检查服务器端(php)哪些按钮 用户点击了吗?

其次我想动态插入按钮。 即我的数据库上有一个字符串,将它爆炸成一个数组并循环每个元素以创建按钮等。我怎么知道如果我不知道从一开始会有多少个按钮和它们的名字? 也许有会话(如果还有其他解决方案,我想避免使用它)?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dqe9657
    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")

    点赞 评论
  • douyi2798
    douyi2798 2013-04-18 12:48

    I think the best answer is to just use a list of checkboxes. It is what they were made for.

    If you want to use the special buttons though, here is one way using the hidden filed as the input, rather than the actual button. I don't know if the element "button" has a checked attibute.

    I just used a simple little javascript to change the values of the input, you can adapt your current function. Just notice that I gave the clickable buttons a new id, appending "_button" to the name from your database.

    $string = "Adventerous,Discovery,Easy-Going"; /* this is from your database */
    $delimiter = ",";
    $button_values = explode($delimiter, $string);
    <script type="text/javascript" >
    function checkbutton(id) {
             var val = document.getElementById(id).value;
             if (val == 0) 
                val = 1;
                val = 0;
             document.getElementById(id).value = val;
             alert("Clicked: " + id);
    <form action="form-catch.php" method="post">
    foreach($button_values as $value) {
        echo '<button type="button" 
                    onClick="checkbutton(\''.$value.'\')">'.$value .'</button>
        <input type="hidden" name="'.$value.'" id="'. $value .'" value="0" ><br>';
    <input type="submit" value="submit">

    Then on the server, the form will send the button names from the string and the 0 or 1 if that button was checked.

    /* quick form checker to show what was clicked in the other form */
    foreach($_POST as $key => $val)
        echo "<br>    $key : $val";   

    Even if you do not use the exact code, I hope the concept will get you in the right direction.

    1) Send the data as a form input.

    2) Loop through whatever was sent over and check for set values.

    点赞 评论