未定义变量:在表单子界面的文本字段中保存用户输入的数据时



当错误报告(E_ALL(时;打开时,我有以下通知的示例

Notice:  Undefined variable: name in /home/user/public_html/directory/subdirectory/test.php on line 111
Notice:  Undefined variable: identity in /home/user/public_html/directory/subdirectory/test.php on line 116

在每个表单字段中,如果我想在用户提交表单后使用会话将用户输入的数据保存在文本字段中。

我当前代码的示例如下:

GetSQLValueString($_POST['name'], "text"),
GetSQLValueString($_POST['identity'], "text")
  <?php  $_SESSION['form'] = $_POST; ?>
  <form action="<?php echo $editFormAction; ?>" method="post" name="userform" id="userform">
  <input name="name" type="text" id="name" value="<php$_SESSION['form']['name'] = '';?><php $_SESSION['form']['name']; ?>" size="25" />
  <input name="identity" type="text" id="identity" value="<php$_SESSION['form']['identity'] = '';?><php $_SESSION['form']['identity']; ?>" size="25" />
and so on.............

关于使用消除未定义索引的建议

<php$_SESSION['form']['name'] = '';?>

作为文本字段的值,如

 <input name="name" type="text" id="name" value="<php$_SESSION['form']['name'] = '';?><php $_SESSION['form']['name']; ?>" size="25" />

上面的代码解析未定义的索引通知,但生成未定义的变量通知。

当错误报告关闭时,该表单运行良好。

你知道在这种情况下如何定义未定义的变量吗?

之所以发出此通知,是因为在发布表单之前,$_SESSION['form']中没有值,但您尝试显示它们。你可以通过查看帖子来解决这个问题如果不是,则用空数组填充CCD_ 2:

if($_POST){
    $_SESSION['form'] = $_POST; 
}else{
    $_SESSION['form'] = array('name'=>'','identity'=>'' //etc);
}
?>

  <form action="<?php echo $editFormAction; ?>" method="post" name="userform" id="userform">
  <input name="name" type="text" id="name" value="<?php echo $_SESSION['form']['name'] ;?>" size="25" />
  <input name="identity" type="text" id="identity" value="<?php echo $_SESSION['form']['identity'];?>" size="25" />

或者,您可以在回显每个变量之前使用isset

还要注意,必须使用echoprint才能实际显示值。

纠正

<input name="name" type="text" id="name" value="<php$_SESSION['form']['name'] = '';?><php $_SESSION['form']['name']; ?>" size="25" />

<input name="name" type="text" id="name" value="<?php echo $_SESSION['form']['name']; ?>" size="25" />

问题是因为没有定义变量,所以有人认为。。。。。!将会话数组定义为空

<?php  
$_SESSION['form'] = array(); //to define null
if(isset($_POST)){
  $_SESSION['form'] = $_POST; 
}

现在你的代码非常小心地像:_

<input name="name" value="<?php echo $_SESSION['form']['name']; ?>"  />

最新更新