duanfenhui5511 2011-11-13 02:05
浏览 18
已采纳

表格提交不起作用

I have a table that prints out all available cameras. It uses a form to change these settings. The problem is that the form only updates the last camera in the entry. In other words if I change the form and hit "Apply" for the last camera in the list it will work. If I change the form for any other camera in this list it changes the one to have the same settings as the last camera in the list. There are no issues with any values as far as I can tell.

Sorry for the long dump here, but without being able to narrow down the problem I thought I should include the bulk of it:

// Dont allow direct linking
defined('_JEXEC') or die('Direct Access to this location is not allowed.');
//get current user
$user =& JFactory::getUser();
// get a reference to the database
$db = &JFactory::getDBO();

$query_camera_name = "SELECT camera_id, camera_name, camera_status, camera_quality, camera_hash, camera_type FROM #__cameras WHERE user_id=".$user->id." AND camera_status!='DELETED'";
$db->setQuery($query_camera_name);
//get number of cameras so we can build the table accordingly
$db->query();
$num_rows = $db->getNumRows();
// We can use array names with loadAssocList.
$result_cameras = $db->loadAssocList();


if (isset($_POST['apply_changes'])) {

    //process changes to camera options     
    $camera_id = $_POST['camera_id'];
    $camera_status = check_input($_POST['camera_status']);
    $camera_name = check_input($_POST['camera_name'], "You entered an empty camera name. Enter another name and apply changes.");
    $camera_quality = check_input($_POST['camera_quality']);

    $query_insert_camera = 'UPDATE `#__cameras` SET `camera_status` ="'.$camera_status.'", `camera_name` ="'.$camera_name.'", `camera_quality` ="'.$camera_quality.'" WHERE `camera_id`='.$camera_id;
    $db->setQuery($query_insert_camera);
    $db->query();
    header("location: " . $_SERVER['REQUEST_URI']);
}

echo "<html>";
echo "<head>";


<link href="dashboard/webcam_widget.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function oncameraSubmit(camera_id)
{
    document.active_cameras.camera_id.value = camera_id;
    return confirm('Apply changes?');
}


</script>
<?php


echo "</head>";
echo "<body>";


if (!isset($result_cameras))
{
   //TODO 
}
else 
{

    if ($num_rows == 0)
    {           
        echo '<b><i><center>You currently have no cameras setup.  Add a Camera below.</center></i></b>';
    }
    else
    {
        ?>
        <form name="active_cameras" action="<?php htmlentities($_SERVER['REQUEST_URI']); ?>" method="POST">
        <input type="hidden" name="camera_id" value="" />
        <table id="webcam-table">
        <thead>
            <tr>
                <th>Camera Type</th>
                <th>Name</th>
                <th>Quality</th>
                <th>Status</th>
                <th>Camera Actions</th>
            </tr>
        </thead>
        <tbody>
<?php
        for($i=0;$i<$num_rows;$i++)
        {

            //camera_status
            if ($result_cameras[$i]["camera_status"] == "ENABLED")
            {
                $enabled_option =  "value='ENABLED' selected='selected'"; 
                $disabled_option = "value='DISABLED'";
            }
            else
            {
                $enabled_option =  "value='ENABLED'";
                $disabled_option = "value='DISABLED' selected='selected'";
            }

            //camera_quality
            if ($result_cameras[$i]["camera_quality"] == "HIGH")
            {
                $high_option =  "value='HIGH' selected='selected'"; 
                $medium_option = "value='MEDIUM'";
                $mobile_option =  "value='MOBILE'";
            }
            else if ($result_cameras[$i]["camera_quality"] == "MEDIUM")
            {
                $high_option =  "value='HIGH'";
                $medium_option = "value='MEDIUM' selected='selected'";
                $mobile_option =  "value='MOBILE'";
            }
            else if ($result_cameras[$i]["camera_quality"] == "MOBILE")
            {
                $high_option =  "value='HIGH'";
                $medium_option = "value='MEDIUM'";
                $mobile_option =  "value='MOBILE' selected='selected'";
            }
            else
            {
                //TODO proper logging
            }

            //camera_type
            if ($result_cameras[$i]["camera_type"] == "WEBCAM")
            {
                $webcam =  "value='WEBCAM' selected='selected'"; 
                $axis = "value='AXIS'";
                $other =  "value='IPCAM'";
            }
            else if ($result_cameras[$i]["camera_type"] == "AXIS")
            {
                $webcam =  "value='WEBCAM'";
                $axis = "value='AXIS' selected='selected'";
                $other =  "value='IPCAM'";
            }
            else if ($result_cameras[$i]["camera_type"] == "IPCAM")
            {
                $webcam =  "value='WEBCAM'";
                $axis = "value='AXIS'";
                $other =  "value='IPCAM' selected='selected'";
            }
            else
            {
                //TODO 
            }

?>

            <tr>
                <td>
                    <select name="camera_type">
                    <option <?php echo $webcam; ?>>Webcam</option>
                    <option <?php echo $axis; ?>>AXIS</option>
                    <option <?php echo $other; ?>>Other</option>
                    </select>
                </td>
                <td>
                <input type="text" size="32" maxlength="64" name="camera_name" value="<?php echo $result_cameras[$i]["camera_name"]; ?>" />
                </td>
                <td>
                    <select name="camera_quality">
                    <option <?php echo $high_option; ?>>High</option>
                    <option <?php echo $medium_option; ?>>Medium</option>
                    <option <?php echo $mobile_option; ?>>Mobile</option>
                    </select>
                </td>
                <td>
                    <select name="camera_status">
                    <option <?php echo $enabled_option; ?>>Enabled</option>
                    <option <?php echo $disabled_option; ?>>Disabled</option>
                    </select>
                </td>
                <td>
                    <input type="submit" name="apply_changes" value="Apply" onClick="javascript:return oncameraSubmit(<?php echo $result_cameras[$i]["camera_id"]; ?>);"/>
                </td>
            </tr>

            <?php
        }
        echo "</tbody>";
        echo "</table>";
        echo "</form>";
    }
}
  • 写回答

2条回答 默认 最新

  • dongxu4580 2011-11-13 02:18
    关注

    It looks like you have multiple HTML elements with the same name. As such, you want to get back an array of values when the form is posted.

    As such, Get $_POST from multiple checkboxes looks like it might be helpful.

    Alternatively, extend oncameraSubmit so that it stores all the data in a hidden input field (not just the id). Then when you update the database, use these hidden fields.

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

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式