TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-09-2007, 01:13 AM   #1 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default Javascript/Prototype Situation

Hey,

I've made a form, theirs a link that when clicked on, it adds another input box below the other.

javascript Code:
function addChoice() {
    new Insertion.After('choice', '<span style="display: block; padding-bottom: 5px"><label for="poll_choice">Option</label><input name="poll_choice[]" type="text" size="25" /></span>');
  }
I want it so that after ten inputs have been added, you can't add any more. Anyone any ideas on how this could be achieved?
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Old 12-09-2007, 01:18 AM   #2 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Add a counter?

So each time they click the link, the counter increments.
bdm is offline  
Reply With Quote
Old 12-09-2007, 01:55 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

As bdm said.

javascript Code:
var iCount = 0; // Set to 1 if there is already a span by default.

function addChoice()
{
    if(iCount >= 10)
    {
        return;
    }

    new Insertion.After('choice', '<span style="display: block; padding-bottom: 5px"><label for="poll_choice">Option</label><input name="poll_choice[]" type="text" size="25" /></span>');
    iCount++;
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
CMellor (12-09-2007)
Old 12-09-2007, 02:01 AM   #4 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

I was going to code it out but I was in the middle of playing CoD4.

I guess also if you want an alternative. You could find everything with the name of 'poll_choice', and count how many their are. Although the first method is much simpler.
bdm is offline  
Reply With Quote
Old 12-09-2007, 03:04 AM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I wrote a slightly fuller example, more to keep me occupied than anything else, which might be of use. Instead of hard-coding the HTML to be repeated, I told prototype to use what's already in the HTML -- that might be super useful, or superfluous, for you.

The essential function makes use of some of Prototypes great features:
JavaScript Code:
function addChoice()
{
    var choice = $('choice');
   
    // Prevent more than x copies
    if (choice.select('input[name="poll_choice[]"]').size() >= 10)
    {
        return;
    }
   
    // Make a copy of the pre-existing HTML that we want to repeat
    var clone = choice.select('span').first().cloneNode(true);
   
    // Clear any text boxes
    clone.select('input[type=text]').invoke('clear');
   
    // Insert the copy into the page
    choice.insert({'bottom': clone});
}

A full test page
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Cloning elements with Prototype</title>
	<script type="text/javascript" src="prototype.js"></script>
	<script type="text/javascript">
		function addChoice()
		{
			var choice = $('choice');
			
			// Prevent more than x copies
			if (choice.select('input[name="poll_choice[]"]').size() >= 10)
			{
				return;
			}
			
			// Make a copy of the pre-existing HTML that we want to repeat
			var clone = choice.select('span').first().cloneNode(true);
			
			// Clear any text boxes
			clone.select('input[type=text]').invoke('clear');
			
			// Insert the copy into the page
			choice.insert({'bottom': clone});
		}
	</script>
</head>
<body>
	<p id="choice">
		<span style="display: block; padding-bottom: 5px">
			<label for="poll_choice">Option</label>
			<input name="poll_choice[]" type="text" size="25" />
		</span>
	</p>
	<p>
		<button onclick="addChoice()">Add Choice</button>
	</p>
</body>
</html>

If you really want to keep the HTML hard-coded then a viable option would be:
JavaScript Code:
function addChoice()
{
    var choice = $('choice');
   
    if (choice.select('input[name="poll_choice[]"]').size() >= 10)
    {
        return;
    }
   
    choice.insert('<span style="display: block; padding-bottom: 5px"><label for="poll_choice">Option</label><input name="poll_choice[]" type="text" size="25" /></span>');
}
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
CMellor (12-09-2007)
Old 12-09-2007, 03:34 AM   #6 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Ah, so I guess my suggested alternative wasn't such a bad idea.

And good call on the clone method.
bdm is offline  
Reply With Quote
Old 12-09-2007, 05:41 PM   #7 (permalink)
The Acquainted
Upcoming Programmer 
 
CMellor's Avatar
 
Join Date: Sep 2007
Location: Leeds, UK
Posts: 141
Thanks: 6
CMellor is on a distinguished road
Default

Thanks to all who responded :) and thanks for the help, I appreciate it a lot! :D
__________________
Not quite a n00b...
CMellor is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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. The time now is 06:37 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design