Remove quotes from boolean when form is submitted












0















I have a simple form.



It POSTs to the server (node js / express), and has a checkbox with a 'true' value when checked.



<form id="demo_form" action="/submit_demo" method="POST">
<input type="checkbox" id="demo_input" name="demo_input" value=true />
</form>


When I receive the data on the server, my values are wrapped in quotes - eg. "true".



This causes issues when submitting data to my api which strictly accepts booleans as true (no quotes).



I can easily manually remove the quotes from any data before submitting to my api BUT it gets a bit painful when dealing with a lot of fields.



Is there a simple solution to sending a boolean value from a form to a server without it being a string?



Any input much appreciated :)










share|improve this question

























  • Probable copy of stackoverflow.com/questions/263965/…

    – Shubham Tiwari
    Nov 22 '18 at 12:15











  • POST/GET parameters do not have any “type”, the values are always send as strings.

    – misorude
    Nov 22 '18 at 12:15






  • 1





    That's not how checkboxes work. If checked, they are in the submitted data as a regular key-value pair, the key being the name. Unchecked, not at all. So, the Boolean is in the presence/absence, never in the value.

    – connexo
    Nov 22 '18 at 12:31


















0















I have a simple form.



It POSTs to the server (node js / express), and has a checkbox with a 'true' value when checked.



<form id="demo_form" action="/submit_demo" method="POST">
<input type="checkbox" id="demo_input" name="demo_input" value=true />
</form>


When I receive the data on the server, my values are wrapped in quotes - eg. "true".



This causes issues when submitting data to my api which strictly accepts booleans as true (no quotes).



I can easily manually remove the quotes from any data before submitting to my api BUT it gets a bit painful when dealing with a lot of fields.



Is there a simple solution to sending a boolean value from a form to a server without it being a string?



Any input much appreciated :)










share|improve this question

























  • Probable copy of stackoverflow.com/questions/263965/…

    – Shubham Tiwari
    Nov 22 '18 at 12:15











  • POST/GET parameters do not have any “type”, the values are always send as strings.

    – misorude
    Nov 22 '18 at 12:15






  • 1





    That's not how checkboxes work. If checked, they are in the submitted data as a regular key-value pair, the key being the name. Unchecked, not at all. So, the Boolean is in the presence/absence, never in the value.

    – connexo
    Nov 22 '18 at 12:31
















0












0








0








I have a simple form.



It POSTs to the server (node js / express), and has a checkbox with a 'true' value when checked.



<form id="demo_form" action="/submit_demo" method="POST">
<input type="checkbox" id="demo_input" name="demo_input" value=true />
</form>


When I receive the data on the server, my values are wrapped in quotes - eg. "true".



This causes issues when submitting data to my api which strictly accepts booleans as true (no quotes).



I can easily manually remove the quotes from any data before submitting to my api BUT it gets a bit painful when dealing with a lot of fields.



Is there a simple solution to sending a boolean value from a form to a server without it being a string?



Any input much appreciated :)










share|improve this question
















I have a simple form.



It POSTs to the server (node js / express), and has a checkbox with a 'true' value when checked.



<form id="demo_form" action="/submit_demo" method="POST">
<input type="checkbox" id="demo_input" name="demo_input" value=true />
</form>


When I receive the data on the server, my values are wrapped in quotes - eg. "true".



This causes issues when submitting data to my api which strictly accepts booleans as true (no quotes).



I can easily manually remove the quotes from any data before submitting to my api BUT it gets a bit painful when dealing with a lot of fields.



Is there a simple solution to sending a boolean value from a form to a server without it being a string?



Any input much appreciated :)







javascript node.js forms express boolean






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 18:16







ronnie burns

















asked Nov 22 '18 at 12:10









ronnie burnsronnie burns

921210




921210













  • Probable copy of stackoverflow.com/questions/263965/…

    – Shubham Tiwari
    Nov 22 '18 at 12:15











  • POST/GET parameters do not have any “type”, the values are always send as strings.

    – misorude
    Nov 22 '18 at 12:15






  • 1





    That's not how checkboxes work. If checked, they are in the submitted data as a regular key-value pair, the key being the name. Unchecked, not at all. So, the Boolean is in the presence/absence, never in the value.

    – connexo
    Nov 22 '18 at 12:31





















  • Probable copy of stackoverflow.com/questions/263965/…

    – Shubham Tiwari
    Nov 22 '18 at 12:15











  • POST/GET parameters do not have any “type”, the values are always send as strings.

    – misorude
    Nov 22 '18 at 12:15






  • 1





    That's not how checkboxes work. If checked, they are in the submitted data as a regular key-value pair, the key being the name. Unchecked, not at all. So, the Boolean is in the presence/absence, never in the value.

    – connexo
    Nov 22 '18 at 12:31



















Probable copy of stackoverflow.com/questions/263965/…

– Shubham Tiwari
Nov 22 '18 at 12:15





Probable copy of stackoverflow.com/questions/263965/…

– Shubham Tiwari
Nov 22 '18 at 12:15













POST/GET parameters do not have any “type”, the values are always send as strings.

– misorude
Nov 22 '18 at 12:15





POST/GET parameters do not have any “type”, the values are always send as strings.

– misorude
Nov 22 '18 at 12:15




1




1





That's not how checkboxes work. If checked, they are in the submitted data as a regular key-value pair, the key being the name. Unchecked, not at all. So, the Boolean is in the presence/absence, never in the value.

– connexo
Nov 22 '18 at 12:31







That's not how checkboxes work. If checked, they are in the submitted data as a regular key-value pair, the key being the name. Unchecked, not at all. So, the Boolean is in the presence/absence, never in the value.

– connexo
Nov 22 '18 at 12:31














1 Answer
1






active

oldest

votes


















0














If a form is POSTed and you did not check a checkbox, it will not be included in the payload. So on the server side it should be easy to identify if something was checked or not.



See this codepen for reference: https://codepen.io/anon/pen/KrowNJ



<form method="POST">
<div>
<input type="text" name="message" placeholder="Enter your message...">
</div>
<div>
<input type="checkbox" id="subscribeNews" name="subscribe">
<label for="subscribeNews">Subscribe to newsletter?</label>
</div>
<div>
<button type="submit">Subscribe</button>
</div>
</form>


Hope this helps!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430730%2fremove-quotes-from-boolean-when-form-is-submitted%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    If a form is POSTed and you did not check a checkbox, it will not be included in the payload. So on the server side it should be easy to identify if something was checked or not.



    See this codepen for reference: https://codepen.io/anon/pen/KrowNJ



    <form method="POST">
    <div>
    <input type="text" name="message" placeholder="Enter your message...">
    </div>
    <div>
    <input type="checkbox" id="subscribeNews" name="subscribe">
    <label for="subscribeNews">Subscribe to newsletter?</label>
    </div>
    <div>
    <button type="submit">Subscribe</button>
    </div>
    </form>


    Hope this helps!






    share|improve this answer




























      0














      If a form is POSTed and you did not check a checkbox, it will not be included in the payload. So on the server side it should be easy to identify if something was checked or not.



      See this codepen for reference: https://codepen.io/anon/pen/KrowNJ



      <form method="POST">
      <div>
      <input type="text" name="message" placeholder="Enter your message...">
      </div>
      <div>
      <input type="checkbox" id="subscribeNews" name="subscribe">
      <label for="subscribeNews">Subscribe to newsletter?</label>
      </div>
      <div>
      <button type="submit">Subscribe</button>
      </div>
      </form>


      Hope this helps!






      share|improve this answer


























        0












        0








        0







        If a form is POSTed and you did not check a checkbox, it will not be included in the payload. So on the server side it should be easy to identify if something was checked or not.



        See this codepen for reference: https://codepen.io/anon/pen/KrowNJ



        <form method="POST">
        <div>
        <input type="text" name="message" placeholder="Enter your message...">
        </div>
        <div>
        <input type="checkbox" id="subscribeNews" name="subscribe">
        <label for="subscribeNews">Subscribe to newsletter?</label>
        </div>
        <div>
        <button type="submit">Subscribe</button>
        </div>
        </form>


        Hope this helps!






        share|improve this answer













        If a form is POSTed and you did not check a checkbox, it will not be included in the payload. So on the server side it should be easy to identify if something was checked or not.



        See this codepen for reference: https://codepen.io/anon/pen/KrowNJ



        <form method="POST">
        <div>
        <input type="text" name="message" placeholder="Enter your message...">
        </div>
        <div>
        <input type="checkbox" id="subscribeNews" name="subscribe">
        <label for="subscribeNews">Subscribe to newsletter?</label>
        </div>
        <div>
        <button type="submit">Subscribe</button>
        </div>
        </form>


        Hope this helps!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 12:24









        chrisg86chrisg86

        8,06921025




        8,06921025






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430730%2fremove-quotes-from-boolean-when-form-is-submitted%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin