将数组从数组下拉更改为复选框

时间:2011-01-07 09:00:52

标签: php arrays checkbox drop-down-menu

我想从下拉菜单更改为复选框,我想更改它,因为我想首先选择数组中的列表可以通过复选框存储到数据库之前选择,所以下拉脚本如下< / p>

<?php
session_start();
define('DEFAULT_SOURCE','Site_A'); 
define('DEFAULT_VALUE',100);
define('DEFAULT_STC','BGS');
include('class/stockconvert_class.php');
$st = new st_exchange_conv(DEFAULT_SOURCE);
if(isset($_GET['reset'])) {
    unset($_SESSION['selected']);
    header("Location: ".basename($_SERVER['PHP_SELF']));
    exit();
}
?>
<form action="do.php" method="post">
<label for="amount">Amount:</label>
<input type="input" name="amount" id="amount" value="1">
<select name="from">
<?php
$stocks = $st->stocks();
asort($stocks);
foreach($stocks as $key=>$stock)
{
    if((isset($_SESSION['selected']) && strcmp($_SESSION['selected'],$key) == 0) || (!isset($_SESSION['selected']) && strcmp(DEFAULT_STC,$key) == 0))
    {
    ?>
    <option value="<?php echo $key; ?>" selected="selected"><?php echo $stock; ?></option>
    <?php
    }
    else
    {
    ?>
    <option value="<?php echo $key; ?>"><?php echo $stock; ?></option>
    <?php   
    }
}
?>
</select>
<input type="submit" name="submit" value="Convert">
</form>

然后我将其更改为如下复选框

<?php
session_start();
define('DEFAULT_SOURCE','Site_A'); 
define('DEFAULT_VALUE',100);
define('DEFAULT_STC','BGS');
include('class/stockconvert_class.php');
$st = new st_exchange_conv(DEFAULT_SOURCE);
if(isset($_GET['reset'])) {
    unset($_SESSION['selected']);
    header("Location: ".basename($_SERVER['PHP_SELF']));
    exit();
}
?>
<form action="do.php" method="post">
<label for="amount">Amount:</label>
<input type="input" name="amount" id="amount" value="1"><input type="submit" name="submit" value="Convert">
<?php
$stocks = $st->stocks();
asort($stocks);
foreach($stocks as $key=>$stock)
{
    if((isset($_SESSION['selected']) && strcmp($_SESSION['selected'],$key) == 0) || (!isset($_SESSION['selected']) && strcmp(DEFAULT_STC,$key) == 0))
    {
    ?>
    <br><input type="checkbox" id="scb1" name="from[]" value="<?php echo $key; ?>" checked="checked"><?php echo $stock; ?>
    <?php
    }
    else
    {
    ?>
    <br><input type="checkbox" id="scb1" name="from[]" value="<?php echo $key; ?>"><?php echo $stock; ?>
    <?php
    }
}
?>
</form>

但不起作用,我需要显示其他相关的代码吗?

谢谢,如果有人帮助,并赞赏它

更新: 好的帖子第一个显然不太明显,所以我会添加错误的问题

错误是 致命错误:在第21行的C:\ xampp \ htdocs \ test \ do.php中调用未定义的方法st_exchange_conv :: convert()

第21行是$st->convert($from,$key,$date);

session_start();
if(isset($_POST['submit']))
{
    include('class/stockconvert_class.php');
    $st = new st_exchange_conv(DEFAULT_SOURCE);
    $from = mysql_real_escape_string(stripslashes($_POST['from']));
    $value = floatval($_POST['amount']);
    $date = date('Y-m-d H:i:s');
    $_SESSION['selected'] = $from;
    $stocks = $st->stocks();
    asort($stocks);

    foreach($stocks as $key=>$stock)
    {
        $st->convert($from,$key,$date);
        $stc_price = $st->price($value);
        $stock = mysql_real_escape_string(stripslashes($stock));
        $count = "SELECT * FROM oc_stock WHERE stock = '$key'";
        $result = mysql_query($count) or die(mysql_error());
        $sql = '';
        if(mysql_num_rows($result) == 1)
        {
            $sql = "UPDATE oc_stock SET stock_title = '$stock', stc_val = '$stc_price', date_updated = '$date' WHERE stock = '$key'";
        }
        else
        {       
        $sql = "INSERT INTO oc_stock(stock_id,stock_title,stock,decimal_place,stc_val,date_updated) VALUES ('','$stock','$key','2',$stc_price,'$date')";
        }   
        $result = mysql_query($sql) or die(mysql_error().'<br />'.$sql);
    }
    header("Location: index.php");
    exit();
}

为什么我要将其从下拉列表更改为复选框? 因为有了复选框列表,我将能够选择我检查的是数据库的入口,那对我来说似乎并不简单,我正在寻找一些帮助&lt;非常感谢你的伴侣。

1 个答案:

答案 0 :(得分:0)

您尚未删除开头<select>代码。

但您删除了<submit>按钮。

您已将name从“从”更改为“从[]”。

编辑:添加后:

使用下拉列表,您只能为from选择一个值。现在您将其更改为复选框,因此可以选择多个条目。这导致在from[]中的脚本中收到数组do.php。您的函数无法以任何方式处理数组或多个选择。

您必须重新设计do.php,将表单更改回下拉列表或改为使用比率按钮。