PHP Javascript在提交后保留值

PHP Javascript在提交后保留值,javascript,php,jquery,Javascript,Php,Jquery,提交后,如何在POST input文本字段中保留值或数据?例如,如果我想向用户显示一个错误,但将其写入的文本保留在输入文本字段中?这是我的javascript: function myFunction() { var table = document.getElementById("produkter_rows"); var row = table.insertRow(-1); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1

提交后,如何在POST input文本字段中保留值或数据?例如,如果我想向用户显示一个错误,但将其写入的文本保留在输入文本字段中?这是我的javascript:

function myFunction()
{
var table = document.getElementById("produkter_rows");
var row = table.insertRow(-1);
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
var cell4 = row.insertCell(3);
var cell5 = row.insertCell(4);
cell1.innerHTML = '<td><input style="width:450px;" class="text-input" type="text" name="beskrivelse_ny[]" value=""></td>';
cell2.innerHTML = '<td><input style="width:60px;" class="text-input" type="text" name="enhed_ny[]" value=""></td>';
cell3.innerHTML = '<td><input style="width:30px;" class="text-input" type="text" name="stk_ny[]" value=""></td>';
cell4.innerHTML = '<td><input style="width:205px;" class="text-input" type="text" name="vejl_eks_moms_ny[]" value=""></td>';
cell5.innerHTML = '<td><a href="#" onclick="removeRow(this)" id="addNew" title="Slet produkt"><img src="images/icons/slet.gif" width="16" alt="Slet" /></a></td>';
}
函数myFunction()
{
var table=document.getElementById(“produkter_行”);
var行=table.insertRow(-1);
var cell1=行插入单元格(0);
var cell2=行插入单元格(1);
var cell3=行插入单元格(2);
var cell4=行插入单元格(3);
var cell5=行插入单元格(4);
cell1.innerHTML='';
cell2.innerHTML='';
cell3.innerHTML='';
cell4.innerHTML='';
cell5.innerHTML='';
}

这应该用PHP而不是JavaScript来处理。值是从问题中的数据库输出的。现在检测用户是否提交了同一字段的值,并输出该值

如果数据库列和用户提交字段的名称完全对应,则可以执行以下操作:

$data = isset($_POST['data']) ? (array)$_POST['data'] : array();

while($mat = $materialer_query->fetch_object()) {
    // true when the user has submitted data for the current object
    if (array_key_exists($mat->id, $data)) {
        foreach ($data as $k => $v) {
            if (property_exists($mat, $k)) {
                // replace the database value with that submitted by the user
                $mat->$k = $v;
            }
        }
    }

    // ... the rest of your code to output the table row
}

使用一些会话变量可能会有好处?提交时发生了什么?Ajax请求-然后不会更改任何表单值。页面刷新-使用php会话。你可能有一个例子?我对这一点并不陌生:)对表单进行AJAXify,这样表单就可以保持填充状态-太棒了!:D@NiettheDarkAbsol我如何才能做到这一点,并仍然保持相同的功能:)?