Forum Moderators: open

Message Too Old, No Replies

checking a radio button on a form via a link?

How can I do this?

         

pants1

9:49 am on May 8, 2005 (gmt 0)

10+ Year Member



I have a brokerage type site. If the customer wants to buy, they link off to the product page of my supplier and complete the sales process. The products on the supplier page are selected using radio buttons. I would like to be able to link from a product on my site to their site and have a specific product already selected using the radio buttons on their site. How do I do this?

kaled

10:27 am on May 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) You must pass the information (the required product) to the new page.
2) You must process the information to ensure the radio box is checked.

The best solution to achieve this will depend on what technology you are using. Since you have posed this question in the javascript forum, I'll assume you are using javascript and static html.

1) You must pass the information in the url.
This is done as products.html?product=insurance&priority=cheap

2) You must read the data from the url into the form on the new page.

I wrote the following script a while back to do this.


// Fills a form with parameters passed by url.
// The standard format is //..../page.html?name1=value1&name2=value2
// Parameters that are not recognised are ignored.
// Parameter names must correspond with element names in the form.
// Names are NOT case sensitive.
// Values are not case sensitive but case is preserved for text fields.
// To set or clear a checkbox, use name=1 or name=0 respectively.
// To set a radiogroup value, use name=value where value is the value of the button to be checked.

function loadParam(f,p) // f is a form, p is a string : 'name=value'
{ var pa = p.split('='); if (pa.length < 2) return false;

var n = pa[0].toLowerCase();
var v = pa[1].toLowerCase();
with (f) {
for (var i = 0; i < length; i++) with (elements[i]) {
if (n == name.toLowerCase()) {
var tp = type.toLowerCase();
if (tp == 'select-one') {
for (var j = 0; j < length; j++) if (options[j].text.toLowerCase() == v) {
selectedIndex = j;
break;
}}
else if ((tp == 'radio') && (value.toLowerCase() == v)) checked = true
else if (tp == 'checkbox') checked = ((v!= '') && (v.charAt(0)!= '0'))
else value = pa[1]
}}
}}

function loadParams(f) // f is a form
{ var tmp = unescape(document.location.search); if (tmp.charAt(0) == '?') tmp = tmp.substring(1);

if ((typeof(f) == 'undefined') ¦¦ (tmp == '')) return false;

tmp = tmp.split('&'); for (var i = 0; i < tmp.length; i++) loadParam(f,tmp[i]);
}

loadParams(document.forms[0]);

Place this code in a filed called readparams.js

At the bottom of the page, add <script src="readparams.js" type="text/javascript"></script>

Naturally, there are other solutions.

Kaled.

pants1

10:48 am on May 8, 2005 (gmt 0)

10+ Year Member



Thanks.
I am already bringing across a (text)dealer code for sales tracking using this method. Using the same convention I havent had any luck with the radio buttons.

if this is in the destination form:
<td class="plan_option" align="left" nowrap><input type="radio" name="plan" value="RES1T"> RES 1T</td>

I have tried ...&plan=RES1T no luck

here is the page I am trying to populate:

<Sorry, no personal URLs.
See Terms of Service [webmasterworld.com]>

What do you think?

[edited by: tedster at 8:44 pm (utc) on May 8, 2005]

kaled

1:59 pm on May 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I must have been half asleep - this isn't the javascript forum! *

Ideally, since you are using php, this should be done server-side, but the script above should work unless there is more than one form on the page. However, I haven't tested this with input elements nested in tables.

A couple of other comments
1) You should wrap radio buttons and checkboxes within <label></label> tags so that the user can click the text.
2) When verifying, I prefer to see all incorrect items highlighted rather than each generating an individual alert.

Kaled.

* It is now - I moved the thread.

[edited by: tedster at 11:27 pm (utc) on May 8, 2005]