简体   繁体   中英

Exporting data from an HTML form into an XML document

Alright here is what I am attempting to do. I have a form written in HTML. It has a few HTML5 elements to it though. What I would like to happen, is when the user fills out all the required fields (validated with PHP and some HTML5) and submits the form, it will export to an existing XML file. The form also uses AJAX to validate as the user fills out the fields, and to validate the form as a whole (once submitted) without reloading my page.

The XML file is just a framework of empty tags set up to receive the information from form submissions, built upon a schema (I made) to restrict it to certain values and what not. I would also need to be able to have a script to validate the new XML values against the schema to make sure all values are valid, and if not return an error message.

The XML Schema validation is as close as possible to the PHP validation with a few character amount limits added in here and there.

If you need me to post the schema for the XML file in order to give a more specific answer then I will upon request.

    <form>
**<!-- First Name -->**
    <fname></fname>

<!-- Last Name -->
    <lname></lname>

<!-- Phone Number -->
    <phone></phone>

<!-- Email -->
    <email></email>

<!-- Website -->
    <website></website>

<!-- Subject -->
    <subject></subject>

<!-- Message -->
    <message></message>

</form>

<form method="post" action="php/validator.php" name="contactform" id="contactform" autocomplete="on"> 

    <fieldset> 

        <legend>Contact Details</legend> 

        <div> 
            <label for="fname" accesskey="F">Your First Name*</label>
            <input name="fname" type="text" id="name" placeholder="Enter your first name" tabindex="1" required /> 
        </div>

        <div> 
            <label for="lname" accesskey="L">Your Last Name*</label>
            <input name="lname" type="text" id="name" placeholder="Enter your last name" tabindex="2" required /> 
        </div> 

        <div> 
            <label for="email" accesskey="E">Email*</label> 
            <input name="email" type="email" id="email" placeholder="myEmail@example.com" pattern="^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$" tabindex="3" required /> 
        </div> 

        <div> 
            <label for="phone" accesskey="P">Phone <small>(optional)</small></label> 
            <input name="phone" type="tel" id="phone" size="12" placeholder="555-555-5555" tabindex="4" /> 
        </div> 

        <div> 
            <label for="website" accesskey="W">Website <small>(optional)</small></label> 
            <input name="website" type="url" id="website" placeholder="www.yourWebDomain.com" tabindex="5" /> 
        </div> 

    </fieldset> 

    <fieldset> 

        <legend>Your Comments</legend> 

        <div> 
            <label for="subject" accesskey="S">Subject*</label> 
            <select name="subject" id="subject" tabindex="6" required="required"> 
                <option value=""></option> 
                <option value="Support">Support</option> 
                <option value="Sale">Sales</option> 
                <option value="Bug">Report a bug</option>
                <option value="Other">Other</option>
            </select>
        </div>
        <div> 
            <label for="comments" accesskey="C">Comments*</label>
            <textarea name="comments" cols="40" rows="3" id="comments" class="comments" placeholder="Enter your comments" spellcheck="true" required tabindex="7"></textarea> 
        </div> 

    </fieldset> 
    <input type="submit" class="submit" id="submit" value="Submit" tabindex="9" />
</form>

After validation use PHP to capture the form fields, wrap them in XML elements, then save it.

eg

<form>
    <fname><?php=$_REQUEST['fname']?></fname>
    <lname><?php=$_REQUEST['lname']?></lname>
    ...
    ..
</form>

No sense in double validation either, with one slightly different than the other. It would be annoying for the user to submit their data successfully but somehow get rejected in the backend without warning. DOMDocument is a little overkill as well.

PHP contains a class called DOMDocument specifically for the purpose of building, altering, importing and exporting XML data. If you are familiar with constructing DOM elements in Javascript, it is the same idea. Create your DOMDocument, then create each element and append those elements to DOMDocument (or that element's parent) after you've made them. After you're done there, use the save function to write it to a file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM