Saving dynamically added strings in PHP and JavaScript

I have a form with dynamically added rows. It works. After filling in the form fields and pressing the submit button "Check correctness", the check is performed in advance. If the data is wrong, it needs to be correct. If the data is correct, we get a new "generate pdf" submit button for the generated PDFs. After the data confirming user input must remain visible. He also works in my form.

The problem is when I add multiple lines. After filling in and clicking on the "Check correctness" button, the newly added lines disappear with its data. Does anyone know how I can keep these lines after validation?

This is my HTML for dynamically added fields:

    $oForm->formHTML('<div class="add_btn_block" style="margin-left:200px;"><div class="row_add_btn add_row">title</div></div>');
       $oForm->formHTML(addRows2());

   function addRows(){
        $personsArray = $_POST['persons'];
        $html = '
        <table id="template2" style="display:none; ">
            <tr id="row_{0}">
                <td><input type="text" name="persons[]"></td>
                <td><img src="/../_img/row_del.png" id="delete_{0}" alt="usun"></td>
            </tr>
        </table>
        <table id="list2">
            <thead >
                <tr>
                    <th></th>
                    <th></th>
                </tr>
            </thead>

            <tbody>
               <tr>
                    <td><input type="text" name="persons[]" value="'.$personsArray[1].'"></td>
                    <td></td>
               </tr>

                <tr>
                    <td colspan="2" id="app_here2"></td>
                </tr>
            </tbody>
        </table>';
            return $html;
    }

      

And for that, it's JavaScript code.

<script>
        $(document).ready(function(){
            var i = 2;
            var rowTemplate2 = jQuery.format($("#template2").html());

            function addRow(){
                var ii = i++;
                $("#app_here2").append(rowTemplate2(ii));
                $("#delete_" + ii).click(function(){
                    $("#row_" + ii).remove();
                });
            }
            $(".add_row").click(addRow);
        });
</script>

      

This is some PHP:

if($_POST["ACT"] == NULL){
    $oForm->formInput(array(
        'type'=>'hidden',
        'name'=>'ACT',
    ));

    $oForm->formInput(array(
        'id' =>'counter',
        'type'=>'hidden',
        'name'=>'counter',
        'value'=>0));

    $oForm->formSubmit(array(
        'name' => 'sprawdzPoprawnosc',
        'value' => 'check correctness'
    ));
}

      

There are several screens:

  • Its only line: 1 enter image description here

2. After adding two lines. I want to still have a form like this after refreshing the page (3 lines and data in it): enter image description here

3. How it looks now, after refreshing the page: enter image description here

Can you help me?

+3


source to share


1 answer


I agree with Marc B. You will need to save your changes to the form before it returns to the page from the server. One option is to save it to a temporary file or database. I think it will work, but it might be overkill. I would try to store it in the $ _SESSION variable, so when the server submits a new form, it can get it from that particular $ _SESSION variable, if it exists.



0


source







All Articles