dsla94915 2012-02-02 03:55
浏览 53
已采纳

循环中的复选框 - PHP

I'm trying to use checkboxes to add users to a specific group but I'm having trouble figuring exactly how the logic works. The list of users is pulled from a sql statement and is echo'd. I would like to display a checkbox beside each name, and if the box is checked that user will be added to the list using implode so I can store the members in one column.

Here is the output that displays a list containing users, their location, and a checkbox. I'd like to be able to select the checkbox and then add them to the group based on their userID($friendNum).

Update: the checkboxes are working and properly entering into the databse but the inputs in the first form aren't being entered. (title, description, date, location)

  $myUsername = $_SESSION['userid'];       
        if(isset($myUsername)){
            echo '<table><form method="post" action="event.php" name="groupInvite"> 
                <tr><td> Event Title:</td><td> <input type="text" name="eventTitle" /></td></tr>
                <tr><td>Event Description:</td><td> <textarea name="eventDescription" /></textarea></td></tr>
                <tr><td>Event Date: </td><td><input type="text" name="eventDate" /></td></tr>
                <tr><td>Event Location:</td><td><input type="text" name="eventLocation" /></td></tr></table>
                <table><tr><td>Username </td><td> Location</td><td>Invite To Event</td></tr>'; 

        $friends = mysql_query("SELECT userid1 as friendId FROM friends WHERE userid2 = $myUsername AND friendstatus = 1
                                UNION SELECT userid2 as friendId FROM friends WHERE userid1 = $myUsername AND friendstatus = 1");

            while($friend = mysql_fetch_array($friends)){
                $userID = $friend['friendId'];
                $friendNum = mysql_query("select * from users where userid = $userID");
                $friendID = mysql_fetch_array($friendNum);
                $userLocation = mysql_query("select * from userinfo where userid='$userID'");
                $locationResult = mysql_fetch_array($userLocation);
                $locationResultArray = $locationResult['userlocation'];
                $locationExplode = explode("~","$locationResultArray");

        echo '<tr>
                <td><a href="profile.php?userid=' . $friendID['userid'] . '">' . $friendID['username'] .  '</a></td>
                <td>' . $locationExplode[0] . ', ' . $locationExplode[1] . '</td>
                <td><input type="checkbox" name="friendID[]" value='.$friendID['userid'].' /></td></tr>';
                }   
            echo '<tr><td> <input type="submit" name="eventSubmit" value="Create Event" />
            </td></tr>
            </form></table>';     
    }   

      if(isset($_POST['eventSubmit'])){
            $userList = implode("~",$_POST['friendID']);
            $sql = "INSERT INTO events VALUES('$userList',...)";  
             $result = mysql_query($sql);   
            echo "Event Created"; //just used for testing purposes
            }  
    }                       
  • 写回答

2条回答 默认 最新

  • douhuitan5863 2012-02-02 04:08
    关注

    There are many things wrong with this.

    1. All input elements have to be inside the <form>. i.e., open your form before you start outputting the checkboxes. It might work in some browsers regardless, but not all. I ran into a problem like this recently where only the data in the form would be submitted, but this only occured in Firefox.
    2. Set the value of each checkbox to the friend ID; there's no need to store it in a hidden element and then try to match it up later. The name of every textbox should be the same with [] appended to it. e.g., <input type="checkbox" value="428" name="friends[]" />. This will cause PHP to organize the $_POST data into an array for you so that you can easily loop over it.
    3. Don't store all the friend IDs in a single column in the DB. It will make it a nightmare to query later. Use a many-to-many table (Google it if uncertain).
    4. It's not really an error, but your HTML structure could use some more work too. A better design might look like:

    example:

    <ul>
        <li><label><input type="checkbox" value="428" name="friends[]" /> John Smith</label></li>
        <li><label><input type="checkbox" value="235" name="friends[]" /> Jane Doe</label></li>
    </ul>
    

    The labels will make the checkbox easier to click (can click anywhere on the label), and I believe it makes it easier for screen readers too. A <ul> is more appropriate here semantically, because its a list of friends you're trying to output.

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

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值