0

How can I keep the value or data in the POST input textfield after submit? For example, if I want to show the user an error, but keeps his written text inside the input textfield? This is my 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>';
}
9
  • 1
    Might be beneficial to use a few session variables maybe? Commented Feb 15, 2014 at 20:16
  • What's happening on submit? Ajax-request - then no form values will be changed. Page refresh - use php sessions. Commented Feb 15, 2014 at 20:17
  • You've got an example maybe? I'm barely new to this :) Commented Feb 15, 2014 at 20:19
  • AJAXify the form, that way the form stays populated anyway - it's great! :D Commented Feb 15, 2014 at 20:19
  • @NiettheDarkAbsol how can I do that and still keep the same function :)? Commented Feb 15, 2014 at 20:22

1 Answer 1

1

This should be handled in PHP instead of JavaScript. Values are output from the database in your question PHP MySQL Delete function in while loop. Now detect if the user has submitted a value for the same field and output that instead.

If the names of your database columns and user submitted fields map exactly you could do something like this:

$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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.