dongqishun6409 2014-04-30 11:35
浏览 51
已采纳

未定义的变量:在表单提交的文本字段中保存用户输入的数据

When error_reporting(E_ALL); is turned on I'm having the following example of notice

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

in every form field if I want to save user inputted data in text fields using session after the form is submitted by the users.

Example of my current code is as following:

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.............

I gout suggestion about eliminating undefined index using

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

as value of text field like,

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

The above code resolves undefined index notice but produces undefined variable notice.

The form works well when error reporting is off.

Any idea, how to define the undefined variable in this case?

  • 写回答

3条回答 默认 最新

  • dongwenyou4298 2014-04-30 11:50
    关注

    This notice is raised because before the form is posted, there are no values in $_SESSION['form'] but you try to display them. You can fix this by checking for a post and filling $_SESSION['form'] with an empty array if not:

    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" />
    

    Alternately you could use isset before echoing each variable.

    Also note that you must use echo or print to actually display the values.

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图