简体   繁体   English

php保存会话(窗体)不起作用

[英]php save session (form) doesn't work

I'm making a website with a form and want to save the inputted data after e new page with the errors was opened. 我正在制作一个带有表单的网站,并希望在打开出现错误的新页面后保存输入的数据。 On the new page you then have a link to the previous one so you can edit it. 然后,在新页面上,您会找到上一页的链接,以便可以对其进行编辑。 The problem is the editing... I want to save the data using a session but apparently it doesn't work. 问题是编辑...我想使用会话保存数据,但显然不起作用。

I'll post the code partially so you guys don't get bombed. 我将部分地发布代码,以免你们被炸毁。 If you need more just ask ;) 如果您需要更多,请询问;)

In the HTML ocntact form: 以HTML ocntact形式:

 <?php
session_start();
if (isset($_SESSION['your_form']) && !empty($_SESSION['your_form'])) {
    $form_data = $_SESSION['your_form'];
    unset($_SESSION['your_form']);
}
?>
<form id="contactForm" class="doorlopendetekst" name="htmlform" method="post" action="PHP/html_form_send.php">
    <table width="80%">
        <tr>
             <td valign="top" id="td">
              <label for="first_name">Voornaam</label>
             </td>
             <td valign="top">
              <input  type="text" id="roundedborder" name="first_name" placeholder="Voornaam" maxlength="50" size="30">
             </td>
        </tr>

In the PHP file to send everything (completely at the top to avoid the 'already sent error'): 在PHP文件中发送所有内容(完全在顶部,以避免出现“已发送错误”):

<?php
session_start();
//assign all posted values to a session
if (!empty($_POST)) {
    foreach($_POST as $key => $value) {
        $_SESSION['your_form'][$key] = $value;
    }
} 
?>
<html><body>
<?php 

I don't get any errors. 我没有任何错误。 It just doesn't save the data. 它只是不保存数据。 Can anyone tell me what's wrong? 谁能告诉我怎么了?

thx 谢谢

You need to assign the value attribute to the input field . 您需要将value属性分配给input field

For instance: 例如:

 <input  type="text" value="<?php echo $form_data['first_name'];?>"
 id="roundedborder" name="first_name" placeholder="Voornaam" maxlength="50" size="30">

EDIT 1: (@Terry Harvey 's suggestions) 编辑1:(@Terry Harvey的建议)

 <input  type="text" value="
 <?php (isset($form_data['first_name'])) ? echo $form_data['first_name'] : echo "" ;?>
 "id="roundedborder" name="first_name" placeholder="Voornaam" maxlength="50" size="30">

看起来不错,但您需要将以前的值放入表单的html部分:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM