2014-02-15 144 views
0

如何在提交後保留POST輸入文本字段中的值或數據?例如,如果我想向用戶顯示一個錯誤,但在輸入文本框中保留他的書面文本?這是我的javascript:PHP提交後保留價值

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>'; 
} 
+1

可能是有益的,也許使用一些會話變量? –

+0

提交怎麼了? Ajax請求 - 那麼不會更改表單值。頁面刷新 - 使用PHP會話。 –

+0

你可能有一個例子嗎?我幾乎不知道這個:) –

回答

1

這應該在PHP而不是JavaScript中處理。數值在您的問題PHP MySQL Delete function in while loop中從數據庫輸出。現在檢測用戶是否提交了相同字段的值並輸出。

如果你的數據庫列和用戶提交的字段的名稱準確地映射,你可以做這樣的事情:

$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 
}