Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    MBI
    MBI is offline
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Lightbulb Auto-load form field Upon URL Opening

    OK, so I am trying to have a link to the contact page of my website auto-load a certain form field (drop down choice), but I need the form field to be different with each link. So I figure it has to be the URL itself that needs some code attached to the end in order to auto-select the drop down choice.

    My problem is that I don't know exactly what code needs to go in the URL, I figure it's something like #input_field_id.focus(); after the page link. Is it even possible? For example, my form name is "form1" and I want it to auto-select "iPhone Screen Replacement" under the "Enquiry" drop down menu.

    The page of my website can be found here

  • #2
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    put this on page with form:
    Code:
    <script language="javascript">
    function getValue(name){
    	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    	var regexS = "[\\?&]"+name+"=([^&#]*)";
    	var regex = new RegExp( regexS );
    	var results = regex.exec( window.location.href );
    	if( results == null ){
    		return '';
    	}else{
    		return results[1];
    	}
    }
    function updateFocus(){
    	var myId = getValue('focusOn');
    	document.getElementById(myId).focus();
    }
    </script>
    add onload="updateFocus()" to your body tag and call document like that: http://example.com/document.htm?focusOn=someID

  • Users who have thanked patryk for this post:

    MBI (10-12-2012)

  • #3
    MBI
    MBI is offline
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    This helps a lot, however I'm not quite there; I think there may be something else to do so that I can select the specific choice within the "Enquiry" section of my form. I have attached a screen shot of my form code for reference to what I am talking about.. the arrows on it are pointing to my field choices and I need to link them individually to be auto-loaded when the contact page loads from a link; I want to add a specific choice to the end of the document, something like http://example.com/document.htm?focusOn=Enquiry?"Laptop Repair" but I'm not sure the exact code. The code you told me to add to the document page works, but I can only get it working for the text fields, not the drop-down menus.

    Any further help would be appreciated.
    Attached Thumbnails Attached Thumbnails Auto-load form field Upon URL Opening-example.png  

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    sure. this thing only do focus thing.
    are you looking for something to automatically sellect something from drop-down?

  • #5
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    Quote Originally Posted by Jam656es View Post
    I can only get it working for the text fields, not the drop-down menus.
    that's because there's no focus() for drop-downs.
    what exactly it is you want to do?

  • #6
    MBI
    MBI is offline
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    that's because there's no focus() for drop-downs.
    what exactly it is you want to do?
    So there is no focus() for drop-down menus? That is what I want to do, focus on a drop-down selection.

  • #7
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    you need to set 'selected' to true.
    example html:
    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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>javscript select using GET</title>
    </head>
    <script language="javascript">
    function getValue(name){
    	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    	var regexS = "[\\?&]"+name+"=([^&#]*)";
    	var regex = new RegExp( regexS );
    	var results = regex.exec( window.location.href );
    	if(results == null){
    		return '';
    	}else{
    		return results[1];
    	}
    }
    function setSelected(){
    	var id = getValue('selected');
    	if(id != ''){
    		document.getElementById(id).selected = true;
    	}
    }
    </script>
    <body onload="setSelected()">
    <select>
      <option value="1" id="option1">1st option</option>
      <option value="2" id="option2">2nd option</option>
      <option value="3" id="option3">3rd option</option>
      <option value="4" id="option4">4th option</option>
      <option value="5" id="option5">5th option</option>
    </select>
    </body>
    </html>
    you acces it using something like http://example.com/document.htm?selected=option3
    you need to have id in every <option> element

  • Users who have thanked patryk for this post:

    MBI (10-14-2012)

  • #8
    MBI
    MBI is offline
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Awesome, that got it working!


  •  

    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
    •