www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript Function for PHP Looped Elements

  1. #1
    Join Date
    Oct 2012
    Posts
    3

    Javascript Function for PHP Looped Elements

    Hi all,
    I've got a script that I found on these forums that gives a text-box to a set of radio options. It's essentially an 'other___' box.

    It works great, however, my php contains a loop where the strip of radio options would appear multiple times on one page. Not knowing too much javascript, I was hoping someone could help me generalize this JS so it can exist in multiple instances on the page.

    JS
    Code:
            function displayTextBox()
            {
                var objElement = document.getElementById('otherTextBox');
    			otherTextBox.style.display = 'block';
    			otherTextBox.style.visibility = 'visible';
            }
    		
    		function hideTextBox()
    		{
    			var objElement = document.getElementById('otherTextBox');
    			otherTextBox.style.display = 'none';
    			otherTextBox.style.visibility = 'hidden';
    		}
            
    		function validate()
    		{
    			var arrElements = document.getElementsByName('choice');
    			var objElement;
    			var boolContinue = false;
    			var objOtherText;
    			
    			for(var i=0, _length=arrElements.length; i<_length; i++)
    			{
    				objElement = arrElements[i];
    				
    				if(objElement.checked)
    				{
    					if(objElement.id == 'choice5')
    					{
    						objOtherText = document.getElementById('othertext');
    						
    						if(strTrim(objOtherText.value).length>0)
    						{
    							boolContinue = true;
    							break;
    						}
    					}
    					else
    					{
    						boolContinue = true;
    						break;
    					}
    				}
    			}
    			
    			if(boolContinue)
    			{
    				alert('Continue, user completed the information.')
    			}
    			else
    			{
    				alert('Ask user to complete the data.')
    			}
    		}
    		
    		/**
    		 * Removes all white space characters from the string.
    		 * 
    		 * @param: {String} String to trim.
    		 * 
    		 * @return {String} Trimed string.
    		*/
    		function strTrim(strTrim) 
    		{
    			return strTrim.replace(/^\s+|\s+$/g, '');
    		}
    The HTML that is being looped in PHP
    HTML Code:
    <form name='' class='form' action='js/submit.php' method='post'>
    <label for='choice1'>Keep:</label>
    <input type='radio' name='dec[]' id='choice1' value='keep' onclick='hideTextBox()'>
    <label for='choice2'>Donate:</label>
    <input type='radio' name='dec[]' id='choice2' value='donate' onclick='hideTextBox()'>
    <label for='choice3'>Sell:</label>
    <input type='radio' name='dec[]' id='choice3' value='sell' onclick='hideTextBox()'>
    <label for='choice4'>Trash:</label>
    <input type='radio' name='dec[]' id='choice4' value='trash' onclick='hideTextBox()'>
    <label for='choice5'>Give To:</label>
    <input type='radio' name='dec[]' id='choice5' value="" onclick='displayTextBox()' />
    <div id='otherTextBox' style='display:none;visibility:hidden;'>
    <textarea id='othertext' cols='15' rows='1' style='resize:none;' name='dec[]'></textarea>
    </div>
    <input type='hidden' name='id' value='<?= $row['Id'] ?>' /><br />
    <input type='submit' value='Submit' onclick='validate()'>
    </form>

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,644
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    
            function displayTextBox(id)
            {
               var objElement = document.getElementById(id);
    			objElement.style.display = 'block';
    			objElement.style.visibility = 'visible';
            }
    
    		function hideTextBox(id)
    		{
    			var objElement = document.getElementById(id);
    			objElement.style.display = 'none';
    			objElement.style.visibility = 'hidden';
    		}
    
    		function validate()
    		{
    			var arrElements = document.getElementsByName('choice');
    			var objElement;
    			var boolContinue = false;
    			var objOtherText;
    
    			for(var i=0, _length=arrElements.length; i<_length; i++)
    			{
    				objElement = arrElements[i];
    
    				if(objElement.checked)
    				{
    					if(objElement.id == 'choice5')
    					{
    						objOtherText = document.getElementById('othertext');
    
    						if(strTrim(objOtherText.value).length>0)
    						{
    							boolContinue = true;
    							break;
    						}
    					}
    					else
    					{
    						boolContinue = true;
    						break;
    					}
    				}
    			}
    
    			if(boolContinue)
    			{
    				alert('Continue, user completed the information.')
    			}
    			else
    			{
    				alert('Ask user to complete the data.')
    			}
    		}
    
    		/**
    		 * Removes all white space characters from the string.
    		 *
    		 * @param: {String} String to trim.
    		 *
    		 * @return {String} Trimed string.
    		*/
    		function strTrim(strTrim)
    		{
    			return strTrim.replace(/^\s+|\s+$/g, '');
    		}/*]]>*/
    </script></head>
    
    <body>
    <form name='' class='form' action='js/submit.php' method='post'>
    <label for='choice1'>Keep:</label>
    <input type='radio' name='dec[]' id='choice1' value='keep' onclick='hideTextBox("otherTextBox")'>
    <label for='choice2'>Donate:</label>
    <input type='radio' name='dec[]' id='choice2' value='donate' onclick='hideTextBox("otherTextBox")'>
    <label for='choice3'>Sell:</label>
    <input type='radio' name='dec[]' id='choice3' value='sell' onclick='hideTextBox("otherTextBox")'>
    <label for='choice4'>Trash:</label>
    <input type='radio' name='dec[]' id='choice4' value='trash' onclick='hideTextBox("otherTextBox")'>
    <label for='choice5'>Give To:</label>
    <input type='radio' name='dec[]' id='choice5' value="" onclick='displayTextBox("otherTextBox")' />
    <div id='otherTextBox' style='display:none;visibility:hidden;'>
     <textarea id='othertext' cols='15' rows='1' style='resize:none;' name='dec[]'>1</textarea>
    </div>
    <br />
    <br />
    <label for='choice1'>Keep:</label>
    <input type='radio' name='dec1[]' id='choice1' value='keep' onclick='hideTextBox("otherTextBox1")'>
    <label for='choice2'>Donate:</label>
    <input type='radio' name='dec1[]' id='choice2' value='donate' onclick='hideTextBox("otherTextBox1")'>
    <label for='choice3'>Sell:</label>
    <input type='radio' name='dec1[]' id='choice3' value='sell' onclick='hideTextBox("otherTextBox1")'>
    <label for='choice4'>Trash:</label>
    <input type='radio' name='dec1[]' id='choice4' value='trash' onclick='hideTextBox("otherTextBox1")'>
    <label for='choice5'>Give To:</label>
    <input type='radio' name='dec1[]' id='choice5' value="" onclick='displayTextBox("otherTextBox1")' />
    <div id='otherTextBox1' style='display:none;visibility:hidden;' >
    <textarea id='othertext1' cols='15' rows='1' style='resize:none;' name='dec1[]'>2</textarea>
    </div>
    <input type='hidden' name='id' value='tom' /><br />
    <input type='submit' value='Submit' onclick='validate()'>
    </form>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Oct 2012
    Posts
    3
    Thanks for the reply.

    Is there not a way to have to javascript seek out the correct text box by using 'THIS' or some variation?
    I worry that giving every instance a unique name would quickly become problematic when it comes time to passing the form to the database.

    Thoughts?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles