...

View Full Version : Is this even possible in javascript



pinkshiro
03-18-2005, 01:57 AM
Hi,

i'm trying to find out if this is possible in javascript: I have a form with default vaules set a combo box. Each value within the combo box sets the recipient e-mail (for the form data) to the corresponding data.

for example, choosing "webmaster" out of the list sets the e-mail in the cgi script to "webmaster@domain.co.nz", choosing "General info" sets it to "info@domain.co.nz" and so on and so on. (note: i have this part down fine)

On another web page, I have profiles of different people, and a link under each profile to the form page. What i want is for the default value in that combo box to be set depending on what link was clicked on.

Is this possible in javascript. If not, what else could it be achieved in. I have next to no experience in JS, so i would not really be able to create this script myself. but if there is some sort of existing script out there, i would love to hear about it! :confused:

please help if you can. thanks a lot in advance

hemebond
03-18-2005, 02:03 AM
Yes. Pass the name along in the URL. Use script to extract the name, then match it to a select option. Set that option as selected.

pinkshiro
03-18-2005, 02:15 AM
thanks so much for the quick reply. much obliged.

would it be possible for someone to flesh out how to achieve this? I'd have no idea how to write the script for your explanation hemebond

hemebond
03-18-2005, 03:16 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>54705</title>
</head>
<body>
<form action="54705" method="get">
<fieldset>
<select id="email" name="email">
<option value="webmaster@domain.co.nz">Webmaster</option>
<option value="info@domain.co.nz">General Info</option>
</select>
</fieldset>
</form>

<ul>
<li><a href="54705?email=webmaster@domain.co.nz">Webmaster</a></li>
<li><a href="54705?email=info@domain.co.nz">General Info</a></li>
</ul>

<script type="text/javascript">
var email = document.location.search.match(/email=(\w*)/i);

if(email)
{
var select = document.getElementById('email');
for(var i = 0; i < select.options.length; i++)
{
if(select.options[i].value.indexOf(email[1]) >= 0)
{
select.selectedIndex = i;
break;
}
}
}
</script>
</body>
</html>

pinkshiro
03-18-2005, 04:19 AM
cool :thumbsup: champion. unfortunately i have no bloody idea of what each part of that script is doing. The links that go to my form page are not on the same page as the form, like in your code. I know that your laughing right now, but I seriously dont know what im doing here so please bear with me.

I have tried to look up on the net on how to fix this up myself, and here's what i've got so far:

pagewithlinks.html:


<a href="contact.html?name=john" class="blue">Contact John</a>

contact.html:


<FORM NAME="staff">
<INPUT TYPE="hidden" NAME="member">
</FORM>

<SCRIPT LANGUAGE="javascript">

var locate = window.location
document.staff.member.value = locate

var text = document.staff.member.value

function delineate(str)
{
theleft = str.indexOf("=") + 1;
return(str.substring(theleft));
}
document.write(delineate(text));

</SCRIPT>

obviously, this writes out "john" onto the page. buts that is where I am stuck. I dont want it to document.write....I need it to be stored somewhere within the page so that in the form <select>, I can call upon that stored info to define which option to show by default.

Plus, if there is no "=" (i.e. if i go straight to contact.html), then it will error, so how would i place some sort of if variable stating that if there is no '=' sign, dont worry?

if you can help me further with this, it would be fantastic. i dont mind if someone wants to clearly explain hemebonds jscript, or help me out with my script.....i dont care as long as i can get this to work!! :eek:

pinkshiro
03-18-2005, 05:44 AM
<FORM NAME="staff">
<INPUT TYPE="hidden" NAME="member">
</FORM>

<SCRIPT LANGUAGE="javascript">

var locate = window.location
document.staff.member.value = locate

var text = document.staff.member.value

function delineate(str)
{
theleft = str.indexOf("=") + 1;
return(str.substring(theleft));
}
document.write(delineate(text));

</SCRIPT>

<form method="POST" action="cgi-bin\bizmail.cgi" onsubmit="return Contact_Validator(this)" language="JavaScript" name="Contact">

<select name="send_to">
<option value="1" selected>General info</option><br>
<option value="4">Webmaster</option><br>
<option value="2">John Gutsell</option><br>
<option value="3">Nick Barrett</option><br>
<option value="5">Mike Walker</option><br>
<option value="7">Martin Cooksen</option><br>
<option value="6">David Walker</option>
</select>

</form>

<SCRIPT LANGUAGE="javascript">
var zork = delineate(text)
var john = 'john';
var nick = 'nick';
var mike = 'mike';
var martin = 'martin';
var david = 'david';
var webmaster = 'webmaster';

document.write("variable j =" + john);
document.write("variable n =" + nick);

if (zork == john){
document.Contact.send_to.value = 2;
}

else if (zork == nick){
document.Contact.send_to.value = 3;
}
else if (zork == mike){
document.Contact.send_to.value = 5;
}
else if (zork == martin){
document.Contact.send_to.value = 7;
}
else if (zork == david){
document.Contact.send_to.value = 6;
}
else if (zork == webmaster){
document.Contact.send_to.value = 4;
}

else {
document.Contact.send_to.value = 1;
}

</SCRIPT>

I have been working on it this afternoon and this is what i have come up to. I think i have answered my own questions through this script, but can someone please tell me if this is valid jscript? it seems to work for me, but if there are any flaws in it, i'd rather it be pointed out now :) cheers



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum