Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts

    submit form & js changed code

    If I insert html via javascript, into innerHTML of form tag, like:

    Code:
    <textarea name = 'foo' ...
    Should be that foo readable on server upon submit ?

    It loooks like it is not.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,437
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Should be, but inserting form fields using innerHTML isn't a good idea.

    You should use document.createElement("textarea") instead.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by BubikolRamios View Post
    If I insert html via javascript, into innerHTML of form tag, like:

    Code:
    <textarea name = 'foo' ...
    Should be that foo readable on server upon submit ?

    It loooks like it is not.
    Works for me. See https://patrick.dark.name/web.dev/cf...rea.element.1/ for a live example, or see the code posted below.

    In testing, I did notice that in_array("textarea", $_POST) doesn't work with textarea element values even though it works fine for input element values. That might lead one to believe that the value isn't being submitted. One has to use isset($_POST["textarea"]) to determine whether the POST value exists. I still have no clue as to why this differs between textarea and input elements.

    PHP:
    PHP Code:
    <?php
        header
    ("content-type: application/xhtml+xml; charset=utf-8");
        echo 
    "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
        echo 
    "<?xml-stylesheet type=\"application/xml\" href=\"../style.sheets/boilerplate.xslt\"?>\r\n";
    ?>
    <html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:p="https://patrick.dark.name/">
        <head>
            <title>Demo for CodingForums.com Thread 300475: submit form &amp; js changed code</title>
            <p:title>Demo for CodingForums.com Thread 300475: <cite>submit form &amp; <abbr title="JavaScript">js</abbr> changed code</cite></p:title>
            <script>
                // This script was validated at http://jshint.com/ using the following settings:
                /* jshint curly: true, eqeqeq: true, forin: true, immed: true, latedef: true, noarg: true, noempty: true, nonew: true, plusplus: true, quotmark: double, undef: true, unused: strict, strict: true, trailing: true */
                (function () {
                    "use strict";
                    var namespaces = {
                        xhtml: "http://www.w3.org/1999/xhtml"
                    };
                    function initialize_script() {
                        var form = document.getElementById("form");
                        var text_area = document.createElementNS(namespaces.xhtml, "textarea");
                        text_area.setAttribute("name", "textarea");
                        text_area.appendChild(document.createTextNode("This is a dynamically‐generated textarea element."));
                        form.insertBefore(text_area, form.firstChild);
                    }
                    document.addEventListener("DOMContentLoaded", initialize_script);
                })();
            </script>
        </head>
        <body>
            <section id="description">
                <h1>Demo Description</h1>
                <p>This demo demonstrates that the values of <code>textarea</code> elements created via JavaScript can be successfully transferred to a server for use by <abbr title="PHP: Hypertext Preprocessor">PHP</abbr>.</p>
                <p>Upon clicking the <q class="button">Submit</q> button, the user should see a paragraph indicating that the value was successfully transferred as well as the contents of the value itself.</p>
            </section>
            <section id="demo">
                <h1>Demo</h1>
                <form id="form" method="post" action="../textarea.element.1/">
                    <button>Submit</button>
                </form>
                <?php
                    
    if (isset($_POST["textarea"])) {
                        echo 
    "<p>The <code>textarea</code> element’s value (<q>" $_POST["textarea"] . "</q>) was successfully processed by the server.</p>";
                    }
                
    ?>
            </section>
        </body>
    </html>
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • #4
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,963
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Ok, thanks to both, had another issue. Form tag encapsuled smaller part of html than I thought it does.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •