php代码从列表框中获取所选索引ID并在另一页上获取id

时间:2014-12-01 16:08:13

标签: php

表名---新闻

我创建了两个页面 1)sethomepage.php 2)exec_setpage.php

我创建了一个列表框,并在sethomepage.php上的数据库中填写

现在我需要将所选值的id从listbox传递给exec_setpage.php

下面是我的代码

PLZ建议我如何在另一页上获取选定的价值指数和GET。

bcoz它显示错误未定义的索引ID

sethomepage.php代码

<select id="news" name="news" size="20" style="width:400px; height:370px; border:1px solid #BBBBBB; float:left; overflow:auto"> 
        <?php                  
        foreach($news as $load_news)
        {                       
        ?>      
        <option value="<?php echo $load_news["id"]; ?>"><?php echo $load_news["headline"]; ?></option>
        <?php }?>       
        </select>

<td><a href="exec_setpage.php"></a></td>

exec_setpage.php代码

<?php
require_once('includes/config.php');
if(!isset($_SESSION['Auth']['id']))
{
    header("Location: index.php");
    exit;
}
$errors = array();
$Admin = new admins;
if(isset($_POST['Add']))
    {   
        $id = $_GET['id'];
        $sql = "SELECT id,status FROM news WHERE id=".mysql_real_escape_string($_GET['id']);
        $result = mysql_query($sql);
        $row = mysql_fetch_assoc($result);
        mysql_query("Update news SET status='1' WHERE id=".mysql_real_escape_string($_GET['id']));
        if(mysql_affected_rows() > 0)
        {

            $_SESSION['message'] = "News Added Successfully";
            header("Location:sethomepage.php");
            exit;
        }
        else
            $_SESSION['message'] = "News Already Added Top List.";
            header("Location:sethomepage.php");
            exit;
    }
?>

2 个答案:

答案 0 :(得分:0)

sethomepage.php代码

<form action="exec_setpage.php" name="sel_news" method="post">
<select>
...
</select>

<input type="submit" name="submit">

exec_setpage.php

$id = $_POST['sel_news'];        //Get the value of selected option

答案 1 :(得分:0)

<form action="exec_setpage.php" name="sel_news" method="post">
<select id="news" name="news" size="20" style="width:400px; height:370px; border:1px solid  BBBBBB; float:left; overflow:auto"> 
    <?php                  
    foreach($news as $load_news)
    {                       
    ?>      
    <option value="<?php echo $load_news["id"]; ?>"><?php echo $load_news["headline"]; ?></option>
    <?php }?>       
    </select>

<input type="submit" name="submit">


 $id = $_POST['news'];