Passing Multiple Variables in Form

Discussion in 'JavaScript' started by Becca800, Sep 30, 2008.

  1. #1
    In a regular form with text fields, can I pass the imput from the user directly into a hidden textarea from multiple fields intertwined with html? Example:

    <form>
    <input type="text" name="firstname">
    <input type="text" name="lastname">
    <input type="text" name="occupation">
    <input type="hidden" value="some html + firstname + some html + lastname + some html + occupation">
    </form>

    I think this might require some javascript. Is something like this possible?

    The reason why is because I am trying to manipulate a joomla submit content form and this text area where everything will be is posted into the db to create the article. It is the only way I can see doing it, I can't mess with the joomla core stuff. Any help on the form?
     
    Becca800, Sep 30, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    you can. joomla comes with mootools 1.11 js framework... hence you can do this...

    first, assign ids to from and fields:
    <form id="myform" method="get" target="formhandler.php">
    <input type="text" name="firstname" id="firstname" />
    <input type="text" name="lastname" id="lastname" />
    <input type="text" name="occupation" id="occupation" />
    <input type="hidden" name="hiddenField" id="hiddenField" value="" />
    <input type="submit" />
    </form>
    
    
    <script type="text/javascript">
    window.addEvent("domready", function() {
        
    var getValueById = function(what) {
        if (!$(what))
            return false;
        var val = $(what).getProperty("value").clean();
        return (val.length > 0) ? val : false;
    }
    
        $("myform").addEvent("submit", function(e) {
            e = new Event(e).stop();
            
            // you can do checks here like
            if (!getValueById("firstname")) {
                alert("you need to put a first name");
                $("firstname").focus();
                return false;
            }
            
            // you can do checks here like
            if (!getValueById("lastname")) {
                alert("you need to put a sur name");
                $("lastname").focus();
                return false;
            }
            
            // more checks.
            // .. 
            
            // we get this far, set value 
            $("hiddenField").setProperty("value", "somehtml here " + getValueById("firstname") + " more html " + getValueById("lastname") + " more html " + getValueById("occupation"));
            
            // now submit the form. 
            this.submit();
            
        });
    }); // end domready
    
    </script>
    HTML:
     
    dimitar christoff, Oct 3, 2008 IP