www.webdeveloper.com
Results 1 to 3 of 3

Thread: Javascript Function for PHP Looped Elements

Hybrid View

  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,709
    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

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.13324 seconds
  • Memory Usage 2,965KB
  • Queries Executed 13 (?)
More Information
Template Usage (31):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (2)bbcode_code
  • (1)bbcode_html
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./includes/functions_threadedmode.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids_threaded
  • showthread_threaded_construct_link
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates