如何从选择框中检索值?

时间:2012-03-08 07:09:32

标签: php html

  

可能重复:
  PHP: “Notice: Undefined variable” and “Notice: Undefined index”

在html代码中:

<select name="123023d">
 <option value="default">Not Share</option>
 <option value="read">Read Only</option>
 <option value="edit">Editable</option>
</select>

在php代码中:

$rights=$_POST['123023d'];

为什么我无法检索此选择框的值?

Notice: Undefined index: 123023d in C:\xampp\htdocs\fyp\list\add.php on line 87

谢谢。

我确信它是在表单中,它是一个post方法。它位于foreach ($result as $set)之后,您可以看到我绘制了一些sql值来生成该选择框,并且选择框的名称是userID

整个部分:

<form id="addlist" method="post" action="add.php" >
<h1>Create your new subscriber list</h1> 
<p>Create a new list before adding subscriber <label class="right"><em class="dot">*</em> indicates required</label></p>

<label><em class="dot">*</em> List name:
<span class="small">Add your list name</span>
</label>
<input id="lname" name="lname" class="required" />

<div class="spacer"></div>

<label>Reminder:
<span class="small">Remind the details of your list</span>
</label>
<textarea id="creminder" name="creminder" cols="52" ></textarea>

<div class="spacer"></div>


<div class="spacer"></div>

<p>Email me when ...</p> 
<label>People subscribe:</label> <input type="checkbox" class="checkbox" name="subscribe" value="1">
<label>People unsubscribe:</label> <input type="checkbox" class="checkbox" name="unsubscribe" value="1">
<div class="spacer"></div>
</div>

</br>

<div id="stylized" class="myform">

<p>Permission Setting ...</p>

<label>Open to other users:</label> <input type="checkbox" class="checkbox" name="public" value="1">
Or
<div class="spacer"></div>

Select the permission for individual user:

<?
$sql =
    "SELECT  UserID,Name,Rights,Position
    FROM     user
    WHERE UserID != ?
    AND Rights != 'Admin'
    ";
$stmt = $conn->prepare($sql);
$stmt->execute(array($_SESSION['username']));

$num_rows= $stmt->rowCount();

if ($num_rows != 0){
$result = $stmt->fetchAll();
?>

<table width="100%" class="display" id="viewSub">
<thead>
<tr>
<th field="col1" width="40%">Name:</th>
<th field="col2" width="40%">Position:</th>
<th field="col2" width="20%">Permission:</th>
</tr>
</thead>
<tbody>
<?
foreach ($result as $set) 
{
    echo "<tr><td>".$set['Name']."</td><td>".$set['Position']."</td><td><select name=".$set['UserID']."><option value='default'>Not Share</option><option value='read'>Read Only</option><option value='edit'>Editable</option></select></td></tr>";
}
?>
</tbody> 
</table>

<?
}
else
echo "There is no another user in this system";
?>


<input class="submit" type="submit" name="submit" value="Submit"/>
<div class="spacer"></div>

</form>

add.php是形式和结果过程

<?
include("../connection/conn.php");
session_start();

if($_SERVER['REQUEST_METHOD'] == "POST"){
print_r($_POST);
exit();
if (!isset($_POST['subscribe']))
$_POST['subscribe']=0;
if (!isset($_POST['unsubscribe']))
$_POST['unsubscribe']=0;
if (!isset($_POST['public']))
$_POST['public']=0;

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);



try {

    $listName = $_POST['lname'];
    $listRemindSub = $_POST['subscribe'];
    $creator = $_SESSION['username'];
    $listRemindUnSub = $_POST['unsubscribe'];
    $isPublic = $_POST['public'];
    $listReminder = $_POST['creminder'];


    $query="INSERT INTO list (ListID,ListName,Creator,IsRemindSub,IsRemindUnSub,IsPublic,CreateDate,Reminder) VALUES ('',?,?,?,?,?,CURDATE(),?)";
    $stmt = $conn->prepare($query);


    $stmt->bindParam(1, $listName , PDO::PARAM_STR);
    $stmt->bindParam(2, $creator, PDO::PARAM_STR);
    $stmt->bindParam(3, $listRemindSub, PDO::PARAM_INT);
    $stmt->bindParam(4, $listRemindUnSub, PDO::PARAM_INT);
    $stmt->bindParam(5, $isPublic, PDO::PARAM_INT);
    $stmt->bindParam(6, $listReminder, PDO::PARAM_STR);

    $stmt->execute();


}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    $conn->rollBack();
    }


try {
    $lastID=$conn->lastInsertId();
    $query="INSERT INTO require_attributes (ReqID,ListID,Attribute,Tag) VALUES ('',$lastID,'Email','{email}')";
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $query="INSERT INTO require_attributes (ReqID,ListID,Attribute,Tag) VALUES ('',$lastID,'FirstName','{fname}')";
    $stmt = $conn->prepare($query);
    $stmt->execute();
    $query="INSERT INTO require_attributes (ReqID,ListID,Attribute,Tag) VALUES ('',$lastID,'LastName','{lname}')";
    $stmt = $conn->prepare($query);
    $stmt->execute();
}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    $conn->rollBack();
    } 

try{
$sql = '
SELECT UserID
FROM user
WHERE Rights != ?';
$stmt = $conn->prepare($sql);
$stmt->execute(array('admin'));
$result= $stmt->fetchAll();
}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    }

foreach ($result as $set)
{   
if ($set['UserID']==$_SESSION['username'])
$rights='edit';
else
{$rights=$_POST[$set["UserID"]];
$rights=$_POST['123023d'];}



if ($rights != 'default' || $set['UserID']==$_SESSION['username'] || $_POST['public']==0)
{ 
$user=$set['UserID'];
try {
    $query="INSERT INTO user_list(UserID,ListID,UserRights) VALUES ('$user',$lastID,'$rights')";
    $stmt = $conn->prepare($query);
    $stmt->execute();

}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    $conn->rollBack();
    }  
}
}

$conn = null;

?>
<div id="stylized" class="myform">
<div style="text-align:center;font-weight:bold;">You have created a list. By default Mail Address, First Name , Last Name is in your list. Add more field if you want. <a href='add.php'>Back</a></div>
<div class="spacer"></div>
</div>
<?
}else{?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<style type="text/css">
      @import "../plugin/easyui/themes/default/easyui.css";
      @import "../plugin/easyui/themes/icon.css";
      @import "../style/form.css";
      @import "../plugin/datatable/media/css/demo_page.css";
      @import "../plugin/datatable/media/css/demo_table.css";
</style>
<script src="../plugin/jquery-1.6.1.min.js"></script>
<script type="text/javascript" src="../plugin/easyui/jquery.easyui.min.js"></script>
<script src="../plugin/jquery.validate.min.js"></script>
<script type="text/javascript" src="../plugin/datatable/media/js/jquery.dataTables.js"></script>
<script src="../plugin/jquery.form.js"></script>


<script>
$(document).ready(function(){
$("#addlist").validate();
});


$(document).ready(function() {
    $('#viewSub').dataTable();
    } );
</script>

</head>
<body>
<div id="stylized" class="myform">
<form id="addlist" method="post" action="add.php" >
<h1>Create your new subscriber list</h1> 
<p>Create a new list before adding subscriber <label class="right"><em class="dot">*</em> indicates required</label></p>

<label><em class="dot">*</em> List name:
<span class="small">Add your list name</span>
</label>
<input id="lname" name="lname" class="required" />

<div class="spacer"></div>

<label>Reminder:
<span class="small">Remind the details of your list</span>
</label>
<textarea id="creminder" name="creminder" cols="52" ></textarea>

<div class="spacer"></div>


<div class="spacer"></div>

<p>Email me when ...</p> 
<label>People subscribe:</label> <input type="checkbox" class="checkbox" name="subscribe" value="1">
<label>People unsubscribe:</label> <input type="checkbox" class="checkbox" name="unsubscribe" value="1">
<div class="spacer"></div>
</div>

</br>

<div id="stylized" class="myform">

<p>Permission Setting ...</p>

<label>Open to other users:</label> <input type="checkbox" class="checkbox" name="public" value="1">
Or
<div class="spacer"></div>

Select the permission for individual user:

<?
$sql =
    "SELECT  UserID,Name,Rights,Position
    FROM     user
    WHERE UserID != ?
    AND Rights != 'Admin'
    ";
$stmt = $conn->prepare($sql);
$stmt->execute(array($_SESSION['username']));

$num_rows= $stmt->rowCount();

if ($num_rows != 0){
$result = $stmt->fetchAll();
?>

<table width="100%" class="display" id="viewSub">
<thead>
<tr>
<th field="col1" width="40%">Name:</th>
<th field="col2" width="40%">Position:</th>
<th field="col2" width="20%">Permission:</th>
</tr>
</thead>
<tbody>
<?
foreach ($result as $set) 
{
    echo "<tr><td>".$set['Name']."</td><td>".$set['Position']."</td><td><select name=".$set['UserID']."><option value='default'>Not Share</option><option value='read'>Read Only</option><option value='edit'>Editable</option></select></td></tr>";
}
?>
</tbody> 
</table>

<?
}
else
echo "There is no another user in this system";
?>


<input class="submit" type="submit" name="submit" value="Submit"/>
<div class="spacer"></div>

</form>
<div class="spacer"></div>
</div>
<br><br><br>
<div id="stylized" class="myform">
<?
try{
$sql = '
    SELECT   *
    FROM     list,user_list
    WHERE    user_list.UserID=?
    AND list.ListID=user_list.ListID
';
$stmt = $conn->prepare($sql);
$stmt->execute(array($_SESSION['username']));
$result= $stmt->fetchAll();
$num_rows= $stmt->rowCount();
}
catch(PDOException $e)
    {
    die ($e->getMessage().'<a href="add.php"> Back</a>'); 
    }
$conn = null;
if ($num_rows == 0) {
    echo '<div style="text-align:center;font-weight:bold;">You have not created any list yet.</div>';}
else {
    echo '<h1>Your Subscriber List</h1> <p>You have created '.$num_rows.' list(s).</p>';
foreach ($result as $set) 
{
echo '<div style="font-weight:bold;">List Name : '.$set['FromName'].'</div><br>';
echo '<div style="font-weight:bold;">Subscriber : </div><br>';
echo '<div style="font-weight:bold;">Create Date : '.$set['CreateDate'].'</div><br>';
echo '<hr>';
}}
    ?>
<div class="spacer"></div>
</div>
</div>
</body>

</html>

<?
}
?>

3 个答案:

答案 0 :(得分:4)

请注意您用于提交表单的方法。有两种一般方式

  1. 获取方法<form method="GET" ... >

    通常使用

    检索
    echo $_GET['123023d'];
    
  2. POST方法<form method="POST" ... > 通常使用

    检索
    echo $_POST['123023d'];
    
  3. 如果没有定义任何方法,默认情况下,它将使用GET方法提交,因此,请使用

    $rights=$_GET['123023d'];
    

    更新

    我发现了您的问题,选择框的标题中没有引号

    <select name=".$set['UserID'].">
    

    将其更改为此。你必须提供引号并将其转义。

    <select name=\"".$set['UserID']."\">
    

    更新2

    归功于@zerkms

    另一个问题是使用数字值而不是按字母顺序字符开始名称。

    <select name="123023d">
    

    确保您不要使用

    之类的数字开头
    <select name="a123023d">
    

答案 1 :(得分:0)

  

如何从选择框中检索值?

它存储在$ _POST ['123023d']或$ _GET ['123023d']变量中取决于使用的方法。

答案 2 :(得分:0)

如果是“发布”请求,请使用:

$rights=$_POST['123023d'];

对于'获取'请求:

$rights=$_GET['123023d'];