Write XML file on server via JS

1

I have an HTML page in local, with one of its pages that contains a form that has to modify an XML file that is part of the project. The file is part of the page, not the machine that was a client. The XML should be recorded as a file, not as a sample of how it is left.

HTML form:

<form>    
            Titulo: <input type="text" name="titulo" id="titulo"><br />
            Descripcion: <input type="text" name="desc" id="desc"><br />
            Agregar imagen: <select id="seleccion">
                <option onclick='hide()' value="A" selected="selected">No</option>
                <option onclick='unhide()' value="b">S&iacute;</option>
            </select>
            <input type="file" class='hidden' id='imag' accept="image/*"/><br />
            <button type="reset" onclick='hide()'>Reiniciar campos</button> <button type="button" onclick="enviar()">Enviar</button>
        </form>

In sending, I have the following code in JS

function enviar()
{
    var titulo=document.getElementById("titulo").value;
    var desc=document.getElementById("desc").value;
    var e=document.getElementById("seleccion");
    var img;
    if (e.options[e.selectedIndex].value=="b")
        img=recuperar_nombre(); //No va
    else
        img="a";

    if (titulo==null||titulo=="")
        alert("Rellene el título")
    else
        if (desc==null|| desc=="")
            alert("Rellene la descripción")
        else
            if (img=="")
                alert("Desmarque la subida de archivos")
            else
                grabar_xml(titulo,desc,img); //Esto es en lo que pido ayuda. Sería abrir el fichero para escritura, saltarse el elemento raíz, escribir, guardar, y salir, sin mostrar
}

The XML, if needed:

<articulos>
<articulo>
    <article>
        <h2>TEST</h2>
        <p></p>
    </article>
    <img href=''></img>
</articulo>
<articulo>
    <article>
        <h2>prueba</h2>
        <p></p>
    </article>
    <img href=''></img>
</articulo>

    
asked by myenemy 29.03.2017 в 12:24
source

1 answer

1

To make changes on the servidor side, you should first have a servidor responsible for reading and writing in that xml file. The code that I assume is the code that is executed in the cliente and this, in turn, does not have access to that file, because as you say, it is not in the cliente machine.

In this case you should mount a servidor with php for example and create a file that receives the parameters sent from your code JavaScript (title, description, etc) and save them in the file xml . To do this, from your form you must define the type of request that you will make to the server action="/tufichero.php" and% method="POST" for example.

In tufichero.php you should have something like:

<?php 
    if(isset($_POST["titulo"]) && isset($_POST["descripcion"]) .....){
        //escribir en el fichero los parametros de $_POST
    }

And if you just want to write to a file xml from JavaScript you can see a simple example here .

To read a file xml through a request ajax :

   var boton = document.getElementById("tuidboton");
        boton.addEventListener("click", function(){
            if(window.XMLHttpRequest) {
                peticion_http = new XMLHttpRequest();
            }
            else if(window.ActiveXObject) {
                peticion_http = new ActiveXObject("Microsoft.XMLHTTP");
            }

            peticion_http.onreadystatechange = mostrarContenido;
            peticion_http.open('POST', '/tufichero.xml', true);
            peticion_http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            peticion_http.send(null);

            function mostrarContenido() {

                if(peticion_http.readyState == 4) {
                    if(peticion_http.status == 200) { //cuando la petición haya resultado exitosa

                        var documento = peticion_http.responseXML;
                        //este es el dom del documento.. recorre y muestra lo que desees
                    }
                }
            }
        });
    
answered by 29.03.2017 в 13:12