Go Back   CodingForums.com > :: Client side development > General web building

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 07:16 AM   PM User | #1
MBI
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
MBI is an unknown quantity at this point
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
MBI is offline   Reply With Quote
Old 10-11-2012, 02:38 PM   PM User | #2
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
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
patryk is offline   Reply With Quote
Users who have thanked patryk for this post:
MBI (10-12-2012)
Old 10-12-2012, 05:37 AM   PM User | #3
MBI
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
MBI is an unknown quantity at this point
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
Click image for larger version

Name:	example.png
Views:	19
Size:	29.4 KB
ID:	11622  
MBI is offline   Reply With Quote
Old 10-13-2012, 03:01 AM   PM User | #4
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
sure. this thing only do focus thing.
are you looking for something to automatically sellect something from drop-down?
patryk is offline   Reply With Quote
Old 10-13-2012, 03:22 AM   PM User | #5
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
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?
patryk is offline   Reply With Quote
Old 10-13-2012, 04:36 AM   PM User | #6
MBI
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
MBI is an unknown quantity at this point
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.
MBI is offline   Reply With Quote
Old 10-13-2012, 02:45 PM   PM User | #7
patryk
Regular Coder

 
patryk's Avatar
 
Join Date: Oct 2012
Location: /dev/couch
Posts: 395
Thanks: 2
Thanked 64 Times in 64 Posts
patryk is on a distinguished road
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
patryk is offline   Reply With Quote
Users who have thanked patryk for this post:
MBI (10-13-2012)
Old 10-13-2012, 11:34 PM   PM User | #8
MBI
New to the CF scene

 
Join Date: Oct 2012
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
MBI is an unknown quantity at this point
Awesome, that got it working!
MBI is offline   Reply With Quote
Reply

Bookmarks

Tags
field, form, url

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:28 PM.


Advertisement
Log in to turn off these ads.