0

Using Javascript I have a plain text where the data inside on it is being shown in my form tags, everything ok and it's working but now I need to change the values inside my form tags code and now it has to take effect on my plain text as well.

Here's my plain text :

Interface: test, IP: 192.168.1.1, Mask : test, Gateway : test, DNS 1: test, DNS 2: test, Broadcast: test

Here's my div:

<div class="col-md-12">
            <div class="form-panel">
            <h4><i class="fa fa-angle-right"></i> Formulario </h4>
            <hr />
            <form method="post">
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">Interfaces:</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>      
                </div>  
                 <div class="form-group ">
                    <label class="col-sm-3 col-sm-3 control-label">IP: </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control"
                    </div>
                </div>
                 <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">Mask : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label"> Gateway : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">DNS 1 : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">DNS 2 : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 col-sm-3 control-label">Broadcast : </label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" 
                    </div>

                </div>
                <div id="hiddenFileLoad" style="display:none;";> 
                </div>
                <br><br>
                <div class="form-group">
                    <button type="submit" name="Save" class="btn btn-success btn-sm" " title="Save"><i class="fa fa-save"></i> Save</button>
                </div>
            </form>
        </div>

Here's my jquery working code:

$(document).ready(function(){

    $("#hiddenFileLoad").load("myfile.txt", function(){



        var loadedText = $("#hiddenFileLoad").text();
        console.log("loadedText:\n\n"+loadedText);


        var loadedTextSplitted = loadedText.split(",");


        for (i=0;i<loadedTextSplitted.length;i++){
            temp = loadedTextSplitted[i].split(": ");
            loadedTextSplitted[i] = temp[1];
        }


        $(".form-panel").find("input").each(function(index){
            $(this).val( loadedTextSplitted[index] );
        });
    }); 
});

The point is once I hit "save" buttom it has to take effect in code and plain text as well..

Here's my output of my code until now: enter image description here

elvispt
  • 4,832
  • 7
  • 27
  • 35
  • I've seen this code somewhere... To write to a file, you can't do it with a client-side language like jQuery. You have to submit the infos to a PHP script, a server-side language. Here is some reading for you : http://www.w3schools.com/php/php_file_create.asp – Louys Patrice Bessette Oct 02 '16 at 15:18
  • Similar question which has been resolved on stackoverflow: http://stackoverflow.com/questions/16055391/writing-data-to-a-local-text-file-with-javascript – Gaurav Gupta Oct 02 '16 at 15:36

1 Answers1

0

If i got your question correctly then you have scenario where your plaintext is composed like this:

[ValueName]: [Value],
[ValueName]: [Value],
[ValueName]: [Value]

I have created small js fiddle, that might help you understand how to create your plaintext dynamically from your form. The idea is to get all form groups iterate through them and then create file row. Then join all rows together.

UPDATE:

Oh, now i get it. As @LouysPatriceBessette pointed in comments you have to save files on server. You can create your file content on client-side (if its not a big file) but then you have to post it to server and process it there. But that's another question.

DolphinSX
  • 91
  • 5