0

I have two xml files, Drinks.xml and Desserts.xml, I plan to keep adding items in it in future.

<?xml version="1.0" encoding="UTF-8" ?>
<DrinksMenu>
    <drink value = "1">Lemonade</drink>
    <drink value = "2">Milk Shake</drink>
</DrinksMenu>

<?xml version="1.0" encoding="UTF-8" ?>
<DessertsMenu>
    <dessert value = "1">Apple Pie</dessert>
    <dessert value = "2">Caramel Custard</dessert>
</DessertsMenu>

I create a custom form in a wordpress page called Registration using the following HTML code:

<title>Using jQuery and XML to populate a drop-down box demo</title>
</head>
<body>
    <form class="form-horizontal">
        <fieldset>

            <!-- Form Name -->
            <legend>
                Form Name
            </legend>

            <!-- Text input-->
            <div class="form-group">
                <label class="col-md-4 control-label" for="textinput">Food Title</label>
                <div class="col-md-8">
                    <input id="textinput" name="textinput" type="text" placeholder="e.q. A Novel Sensation at a restaurant ..." class="form-control input-md">

                </div>
            </div>

            <!-- Select Basic -->
            <div class="form-group">
                <label class="col-md-4 control-label" for="selectbasic">Select Type</label>
                <div class="col-md-4">
                    <select id="selectbasic" name="selectbasic" class="form-control">
                        <option value="1">Drinks</option>
                        <option value="2">Desserts</option>
                    </select>
                </div>
            </div>

            <div id="page-wrap">
                <label class="col-md-4 control-label" for="selectbasic">Select Your Option</label>
                <select id="paperSelect">
                    <option value="1"></option>
                </select>
            </div>

        </fieldset>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                var urlPaper;
                var elementTag;
                urlPaper = "Drinks.xml";;
                elementTag = 'drink';

                $.ajax({
                    // updatePublicationFields();
                    type : "GET",
                    url : urlPaper,
                    dataType : "xml",
                    success : function(xml) {
                        var select = $('#paperSelect');
                        select.empty();

                        var optionsHtml = new Array();
                        $(elementTag, xml).each(function() {
                            var value = $(this).attr('value');
                            var label = $(this).text();
                            optionsHtml.push("<option class='ddindent' value='" + value + "'>" + label + "</option>");
                        });
                        optionsHtml = optionsHtml.join('');
                        select.append(optionsHtml);
                    } //sucess close
                });

                $("#selectbasic").change(function() {
                    var urlPaper;
                    var elementTag;

                    if ($("#selectbasic").val() == 1) {
                        urlPaper = "Drinks.xml";
                        elementTag = 'drink';
                    } else {
                        urlPaper = "Desserts.xml";
                        elementTag = 'dessert';
                    }

                    $.ajax({

                        type : "GET",
                        url : urlPaper,
                        dataType : "xml",
                        success : function(xml) {
                            var select = $('#paperSelect');
                            select.empty();
                            var optionsHtml = new Array();
                            $(elementTag, xml).each(function() {
                                var value = $(this).attr('value');
                                var label = $(this).text();
                                optionsHtml.push("<option class='ddindent' value='" + value + "'>" + label + "</option>");
                            });
                            optionsHtml = optionsHtml.join('');
                            select.append(optionsHtml);
                        } //success close
                    });

                });

            });
        </script>

</body>

The above code works perfectly if I create a project outside wordpress and run it on my localhost. But for wordpress there is some problem. Now I have two questions,

  1. How do I make it work in wordpress ? I mean where do I place those xml files so wordpress finds and loads it ? I was unable to make it work in wordpress.
  2. I have used some repeated blocks of code since I couldn't make it work otherwise. How can I with the use of functions optimize it ?

Can someone knowledgeable help me please ? I am still dabbling with web technologies.

Syed Alam Abbas
  • 521
  • 6
  • 21
  • you may need to place `.xml` file in `wp-content/themes/yourTheme` folder , open your `page-registration` or `template-registration`. place the code and do not forget to use `get_header()` and `get_footer()` top and bottom of file. – Noman Jan 08 '16 at 21:45
  • Hi Noman, Thanks for your prompt response. Could you please elaborate ? Then post your comment as an answer ? I am not well versed with the setup , if you be kind to give me the pointers I will be able to do this. Thanks in advance. – Syed Alam Abbas Jan 08 '16 at 21:53
  • I followed the settings : 1. I copied the .xmls to wp-content/themes/yourTheme. 2. I included the get header and get footer. But still it doesn't work. I must be missing some major settings ? – Syed Alam Abbas Jan 09 '16 at 02:25
  • Check [this](http://stackoverflow.com/a/22437983/1287812) and [this](http://stackoverflow.com/a/13614297/1287812) with special attention to `wp_localize_script` – brasofilo Jan 10 '16 at 08:10

0 Answers0