Quantcast
Channel: Four Miles to May
Viewing all articles
Browse latest Browse all 10

Mootools, Forms & MediaboxAdvanced

$
0
0

Here’s what I wanted to accomplish tonight:

  1. Submit a form
  2. Process the form data
  3. Display the results in a MediaboxAdvanced window

Getting the script to which the form submits to open in the box was a breeze; it was getting the post data from the form to the script, processing it, and then displaying the results in the box that had me stumped. I came up with a way to get the page to work the way I needed it to, but I’m not entirely happy with the method. If anyone out there has a better idea, please leave a comment!

View the demo

The HTML

As you can see, it consists simply of my form, and a hidden div.

<!--all the usual stuff goes first --
     and then be sure to include Mootools core and MediaboxAdvanced scripts
     and the Mediaboxadvanced css-->
<script type="text/javascript" src="/js/mootools-1.2.1-core-yc.js"></script>
<script type="text/javascript" src="/js/mediaboxadv.js"></script>
<link rel="stylesheet" type="text/css" href="/css/mediaboxadv.css"/>
</head>
<body>
  <form action="/demos/mediaboxadv/submit.php" method="post" name="myform" id="myform">
      <dl>
          <dt>Enter your name:</dt>
          <dd><input type="text" value="" name="name" id="name" /></dd>
          <dt></dt>
          <dd><input type="submit" /></dd>
       </dl>
  </form>
  <div id="mb_inline" style="display:none;"></div>
</body>

The Javascript

<script type="text/javascript">
window.addEvent('domready',function() {
    $('myform').addEvent( 'submit', function(event){
        // Stop the submission of the form by the normal channels
        event.stop();
        // Submit the form using AJAX
        var myHTMLRequest = new Request.HTML(
            {
                url:'/demos/mediaboxadv/submit.php',
                // When the form submission is complete...
                onComplete:function(response) {
                    // Clear anything out of the mb_inline div
                    $('mb_inline').getChildren().destroy();
                    // Dump the response into the div
                    $('mb_inline').adopt(response);
                    // And open the div in the MediaBox
                    Mediabox.open('#mb_inline','Results of Submitted Form','300 200');
                }
            }).post($('myform'));
    });
});
</script>

That’s it. It seems to work. As noted above, please comment below if you know a better method of accomplishing the same thing.

Share/Bookmark

Viewing all articles
Browse latest Browse all 10

Trending Articles