Tagged: ,

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #1464632

    Hi team,
    I’m adding a code block to a page containing a fetch to a .json file. Where should I place that file? I’ve tried both the root directory and also a new folder in the uploads directory.

    The code is in private. Thanks for your help.

    #1464649

    Hey Julie,

    Thank you for the inquiry.

    Are you trying to add JavaScript code to a Code Block element? What should happen after fetching the content of the JSON file? You might need to place the JSON file in the wp-content directory or your child theme folder. Please provide the complete code so that we can review it. However, please note that this level of customization is beyond the scope of our support. You may need to hire a freelance developer or reach out to our partner, Codeable.

    If you have any other questions or need further assistance, please don’t hesitate to let us know.

    Best regards,
    Ismael

    #1464687

    Hi Ismael,
    The code works well outside of Enfold. (see private) It works inside of Enfold as well but it can’t find the JSON file where I placed it. That’s why I’m asking where to put it. See private for all. I gave you login credentials as well above.
    Thanks

    #1464741

    Hi,

    Thank you for the info.

    We adjusted the script slightly and removed a few unnecessary elements, including some styles. You can place the zipcodes.json file in the wp-content > uploads folder and use the absolute URL (https://dev.site.com/wp-content/uploads/zipcodes.json) in the fetch function.

    <script>
    var gform;gform||(document.addEventListener("gform_main_scripts_loaded",function(){gform.scriptsLoaded=!0}),window.addEventListener("DOMContentLoaded",function(){gform.domLoaded=!0}),gform={domLoaded:!1,scriptsLoaded:!1,initializeOnLoaded:function(o){gform.domLoaded&&gform.scriptsLoaded?o():!gform.domLoaded&&gform.scriptsLoaded?window.addEventListener("DOMContentLoaded",o):document.addEventListener("gform_main_scripts_loaded",o)},hooks:{action:{},filter:{}},addAction:function(o,n,r,t){gform.addHook("action",o,n,r,t)},addFilter:function(o,n,r,t){gform.addHook("filter",o,n,r,t)},doAction:function(o){gform.doHook("action",o,arguments)},applyFilters:function(o){return gform.doHook("filter",o,arguments)},removeAction:function(o,n){gform.removeHook("action",o,n)},removeFilter:function(o,n,r){gform.removeHook("filter",o,n,r)},addHook:function(o,n,r,t,i){null==gform.hooks[o][n]&&(gform.hooks[o][n]=[]);var e=gform.hooks[o][n];null==i&&(i=n+"_"+e.length),gform.hooks[o][n].push({tag:i,callable:r,priority:t=null==t?10:t})},doHook:function(n,o,r){var t;if(r=Array.prototype.slice.call(r,1),null!=gform.hooks[n][o]&&((o=gform.hooks[n][o]).sort(function(o,n){return o.priority-n.priority}),o.forEach(function(o){"function"!=typeof(t=o.callable)&&(t=window[t]),"action"==n?t.apply(null,r):r[0]=t.apply(null,r)})),"filter"==n)return r[0]},removeHook:function(o,n,t,i){var r;null!=gform.hooks[o][n]&&(r=(r=gform.hooks[o][n]).filter(function(o,n,r){return!!(null!=i&&i!=o.tag||null!=t&&t!=o.priority)}),gform.hooks[o][n]=r)}});
    </script>
    <div class="container">
    <h3>Zipcode Redirect</h3>
    <form id="zipcodeForm">
            <input type="text" id="zipcodeInput" placeholder="Enter zipcode" required>
            <button type="submit">Go</button>
        </form>
    <p id="errorMessage" class="error"></p>
    
    </div>
    <script>
    document.addEventListener('DOMContentLoaded', () => {
        const form = document.getElementById('zipcodeForm');
        const input = document.getElementById('zipcodeInput');
        const errorMessage = document.getElementById('errorMessage');
    
        fetch('https://welstestdev.wpenginepowered.com/wp-content/uploads/zipcodes.json')
            .then(response => response.json())
            .then(zipcodeData => {
                form.addEventListener('submit', (e) => {
                    e.preventDefault();
                    const zipcode = input.value.trim();
    
                    if (zipcodeData[zipcode]) {
                        window.location.href = zipcodeData[zipcode];
                    } else {
                        errorMessage.textContent = 'Zipcode not found. Please try again.';
                    }
                });
            })
            .catch(error => {
                console.error('Error fetching zipcode data:', error);
                errorMessage.textContent = 'An error occurred. Please try again later.';
            });
    });
    </script>
    

    Best regards,
    Ismael

    #1464742

    Hi,

    UPDATE: Please make sure to move the Code Block element inside a Color Section element. Let us know of the result.

    Best regards,
    Ismael

    #1464766
    This reply has been marked as private.
    #1464834

    Hi,
    As I understand your test site outer box shadow works good:
    Screen Shot 2024 08 17 at 12.54.28 PM
    but your live site is not:
    Screen Shot 2024 08 17 at 12.55.52 PM
    I first note that in your code block you have HEAD & BODY tags please don’t add these, I also note that your page is not published, the WordPress preview will not always display the page correctly, I also see that your HTML in the code block adds css for “.container” & “body” that targets the page and not just the code block element.
    So I remove the HEAD & BODY tags, change “.container” to “.zip-container” and publish, and now it is mostly correct:
    Screen Shot 2024 08 17 at 1.27.39 PM
    the corrected code:

    <script>
    var gform;gform||(document.addEventListener("gform_main_scripts_loaded",function(){gform.scriptsLoaded=!0}),window.addEventListener("DOMContentLoaded",function(){gform.domLoaded=!0}),gform={domLoaded:!1,scriptsLoaded:!1,initializeOnLoaded:function(o){gform.domLoaded&&gform.scriptsLoaded?o():!gform.domLoaded&&gform.scriptsLoaded?window.addEventListener("DOMContentLoaded",o):document.addEventListener("gform_main_scripts_loaded",o)},hooks:{action:{},filter:{}},addAction:function(o,n,r,t){gform.addHook("action",o,n,r,t)},addFilter:function(o,n,r,t){gform.addHook("filter",o,n,r,t)},doAction:function(o){gform.doHook("action",o,arguments)},applyFilters:function(o){return gform.doHook("filter",o,arguments)},removeAction:function(o,n){gform.removeHook("action",o,n)},removeFilter:function(o,n,r){gform.removeHook("filter",o,n,r)},addHook:function(o,n,r,t,i){null==gform.hooks[o][n]&&(gform.hooks[o][n]=[]);var e=gform.hooks[o][n];null==i&&(i=n+"_"+e.length),gform.hooks[o][n].push({tag:i,callable:r,priority:t=null==t?10:t})},doHook:function(n,o,r){var t;if(r=Array.prototype.slice.call(r,1),null!=gform.hooks[n][o]&&((o=gform.hooks[n][o]).sort(function(o,n){return o.priority-n.priority}),o.forEach(function(o){"function"!=typeof(t=o.callable)&&(t=window[t]),"action"==n?t.apply(null,r):r[0]=t.apply(null,r)})),"filter"==n)return r[0]},removeHook:function(o,n,t,i){var r;null!=gform.hooks[o][n]&&(r=(r=gform.hooks[o][n]).filter(function(o,n,r){return!!(null!=i&&i!=o.tag||null!=t&&t!=o.priority)}),gform.hooks[o][n]=r)}});
    
    document.addEventListener('DOMContentLoaded', () => {
        const form = document.getElementById('zipcodeForm');
        const input = document.getElementById('zipcodeInput');
        const errorMessage = document.getElementById('errorMessage');
    
        fetch('https://welsbpo.net/wp-content/uploads/zipcodes.json')
            .then(response => response.json())
            .then(zipcodeData => {
                form.addEventListener('submit', (e) => {
                    e.preventDefault();
                    const zipcode = input.value.trim();
    
                    if (zipcodeData[zipcode]) {
                        window.location.href = zipcodeData[zipcode];
                    } else {
                        errorMessage.textContent = 'Zipcode not found. Please try again.';
                    }
                });
            })
            .catch(error => {
                console.error('Error fetching zipcode data:', error);
                errorMessage.textContent = 'An error occurred. Please try again later.';
            });
    });
    </script>
        <style>
            body {
                font-family: Arial, sans-serif;
                display: flex;
                justify-content: center;
                align-items: center;
                margin: 0;
                background-color: #f0f0f0;
            }
    
            .zip-container {
                background-color: white;
                padding: 2rem !important;
                border-radius: 8px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                text-align: center;
            }
    
            h3 {
                margin-bottom: 1rem;
            }
    
            form {
                display: flex;
                justify-content: center;
                margin-bottom: 1rem;
            }
    
            input {
                padding: 0.5rem;
                font-size: 1rem;
                border: 1px solid #ccc;
                border-radius: 4px 0 0 4px;
            }
    
            button {
                padding: 0.5rem 1rem;
                font-size: 1rem;
                background-color: #007bff;
                color: white;
                border: none;
                border-radius: 0 4px 4px 0;
                cursor: pointer;
            }
    
            button:hover {
                background-color: #0056b3;
            }
    
            .error {
                color: red;
            }
        </style>
        <div class="zip-container">
            <h3>Zipcode Redirect</h3>
            <form id="zipcodeForm">
                <input type="text" id="zipcodeInput" placeholder="Enter zipcode" required>
                <button type="submit">Go</button>
            </form>
            <p id="errorMessage" class="error"></p>
        </div>

    Best regards,
    Mike

    #1464907

    Thank you Mike and Ismael for your help with this. It works well and looks good now too! You guys are the best.
    Have a great week!

    #1464912

    Hi,
    Glad we were able to help, if you have any further questions please create a new thread and we will gladly try to help you. Thank you for using Enfold.

    Best regards,
    Mike

Viewing 9 posts - 1 through 9 (of 9 total)
  • The topic ‘Fetch to JSON field path’ is closed to new replies.