...

View Full Version : Auto-load form field Upon URL Opening



MBI
10-11-2012, 07:16 AM
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 (http://www.inventionrepair.com/contact.html)

patryk
10-11-2012, 02:38 PM
put this on page with form:


<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

MBI
10-12-2012, 05:37 AM
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.

patryk
10-13-2012, 03:01 AM
sure. this thing only do focus thing.
are you looking for something to automatically sellect something from drop-down?

patryk
10-13-2012, 03:22 AM
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?

MBI
10-13-2012, 04:36 AM
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.

patryk
10-13-2012, 02:45 PM
you need to set 'selected' to true.
example html:


<!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

MBI
10-13-2012, 11:34 PM
Awesome, that got it working! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum