The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > Web Programming > DHTML


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 06-15-2007, 06:57 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 442
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
Icon1 How Too Much Can Result In Nothing At All -- And How To Prevent It.

Having a form submit automatically minimizes the end user's number of actions to achieve a desired goal. This is often seen when a user clicks a check box and a custom form relevant to the checked box is displayed; or chooses an option from a drop-down resulting in a page update; or passing a validation check prior to form submission, for example. Anyone who programs for too long in DHTML will become familiar with writing these forms. Let's begin by bringing all this to life in an example form:
Code:
<!DOCTYPE html PUBLIC "--W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Automatic Form Submission</title>
    <script type="text/javascript">
    </script>
  </head>
  <body>
    <?php
      echo "Now Showing Page Number ".max(1,(int)$_POST['page_number']);
    ?>
    <form method="post" action="<?php echo $PHP_SELF;?>">
      <label for="page_number">Page:</label>
        <select name="page_number" id="page_number" onChange="this.form.submit();">
        <?php
        $num_pages = 5;
        for ($counter=1;$counter<=$num_pages;$counter++) {
          $selected = ($counter==(int)$_POST['page_number'])?' selected="selected" ':''; //Using the ternary operator
          echo '<option value="'.$counter.'"'.$selected.'>'.$counter.'</option>';
        }
        ?>
        </select>
        <input type="submit" value="Go" name="submit_button" id="submit_button" />
    </form>
  </body>
</html>
The code above simply echoes out what page it happens to be on and selects the proper entry in the options. It works. You might think we are done, but we haven't overdone it yet. ;-) Take a look at this code:
Code:
<!DOCTYPE html PUBLIC "--W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Automatic Form Submission</title>
    <script type="text/javascript">
    </script>
  </head>
  <body>
    <?php
      echo "Now Showing Page Number ".max(1,(int)$_POST['page_number']);
    ?>
    <form method="post" action="<?php echo $PHP_SELF;?>">
      <label for="page_number">Page:</label>
        <select name="page_number" id="page_number" onChange="this.form.submit();">
        <?php
        $num_pages = 5;
        for ($counter=1;$counter<=$num_pages;$counter++) {
          $selected = ($counter==(int)$_POST['page_number'])?' selected="selected" ':''; //Using the ternary operator
          echo '<option value="'.$counter.'"'.$selected.'>'.$counter.'</option>';
        }
        ?>
        </select>
        <input type="submit" value="Go" name="submit" id="submit" />
    </form>
  </body>
</html>
Do you see the difference? Can you believe that the little difference in the code above is so significant that it causes the automatic submit to stop altogether? Don't believe me? Try them out. I'll wait.

...

Amazing, isn't it? I've spent hours trying to find out what was causing perfectly legitimate code to stop working. Here we have a huge headstart as I've generated the error for you and shown both cases. When you're writing it yourself, you only see the non-functioning case. And searching around with not much for a clue takes a long time. Ok. So, let's get into the reason why it is that when you have a submit button named "submit", that you can't call the submit function of the form..."submit" really is a great word and that sentence is confusing enough to be a great way of pointing out that too much of a good thing can be bad.

When you create a button called "submit", it gets added to the DOM (Document Object Model). That makes form.submit a reference to the button. Now, you're also trying to call a function form.submit(). JavaScript does not automatically separate these two and the result is that you can't access the .submit() function anymore. The solution is easy enough - just rename the submit button so that it does not conflict with the submit function.

Hopefully you too will now be safeguarded against this insipid little submit bug.


Still not sure of the difference? It was in these lines:
Code:
<input type="submit" value="Go" name="submit_button" id="submit_button" />
Code:
<input type="submit" value="Go" name="submit" id="submit" />

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by TeraTask : 12-13-2007 at 11:55 PM. Reason: Corrected grammatical error
Reply With Quote
The Following User Says Thank You to TeraTask For This Useful Post:
HelloWorld (07-25-2007)
  #2 (permalink)  
Old 06-15-2007, 09:22 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
That makes form.submit a reference to the button. Now, you're also trying to call a function form.submit().
are you saying that JavaScript have method called submit() ?? I don't 100% get of what you're saying, because I don't see that there's anything conflict on either code

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 06-15-2007, 11:18 AM
TeraTask's Avatar
TeraTask TeraTask is offline
PT Admin
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 3
Join Date: Jun 2007
Location: Reno, NV
Posts: 442
iTrader: (0)
TeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to beholdTeraTask is a splendid one to behold
You should try running the code (like it suggests ). The form has a method called submit which you are accessing through javascript. When you add a button called submit that makes accessing the submit function through javascript impossible (or very, very difficult).

__________________
Jeremy Miller
Content Farmer - Optimized Automated Blog Posting

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 12:22 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50