Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Set Form Value to Unescaped URL Parameter 2

Status
Not open for further replies.

jay07

Programmer
Dec 15, 2005
19
US
Hi-

I am trying, so far without success, to set a form field value to an unescaped URL parameter. Here is the story. A recipient of email from us click an opt-out button and jumps to an opt out page. The URL of that opt-out page has the recipient's email address appended to the URL as a parameter. But the URL has the "@" symbol in the email encoded into "%40", so I have to unescape it to convert the "%40" back to an "@" symbol. So the URL looks like ...company.com/page/emailoptout.htm?emailname%40yahoo.com

In my header I have script to try to set my "emailoptout" form's "email" field value to the unescaped URL parameter:

<script language="javascript" >
function populate()
{
document.optoutform.email.value=unescape(window.location.search.substring(1));
}
</script>

But upon submission the email address is not arriving with the other form fields' data. Can anyone help?

Thank you

Jay
 
With the information you have provided, I would use the following "checklist" to diagnose the solution (in order).

- Put an alert in the populate function to confirm it is being run.
- Check you are spelling the email field name correctly client-side.
- Check to see if there are any reported javascript errors.
- Set the form field to be visible and confirm that something is being set in there by javascript.
- Change the form to use GET instead of POST and inspect the URL manually looking for the field/value pair.
- Check you are spelling the email field name correctly server-side.
- Inspect the raw headers sent from the client to the server.

If you haven't found the solution at this stage, then the best advise is to post a working URL.

Let us know how you get on!

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Try the following to see how each appears:

Code:
alert(window.location.search);
alert(window.location.search.substring(1));
alert(unescape(window.location.search.substring(1)));

It will at least tell you if the parameter is being passed correctly.

It may well be that you need to use a name/value pair when passing parameters (although I'm really not sure on this one)... so if the above alerts don't work, then try using "?email=emailname%40yahoo.com" instead of just "?emailname%40yahoo.com"

If the alerts do work, then make sure your form name is correct, the element name is correct, and that you don't have more than one form or one element with those names.

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thank you I will try these these to the best of my ability - 1st 4 i can do; i have very limited programming knowledge/skill.... think item #4 does not happen... i will test and confirm.....
 
Hi Thank you - sorry for remedial question: do you mean try adding just one a t a time inside my existing "populate" function or elsewhere?
 
If you mean the alert statements, then yes - right inside your populate function:

Code:
function populate() {
	alert(window.location.search);
	alert(window.location.search.substring(1));
	alert(unescape(window.location.search.substring(1)));
	document.optoutform.email.value = unescape(window.location.search.substring(1));
}

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
OK I tested this and I get no alerts at all. I get sent to the return URL page that is in the form code.....
 
the fn is in the head....so i don't need to call it right????

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="../css/j1.css">
<script language="javascript" >
function populate() {
alert(window.location.search);
alert(window.location.search.substring(1));
alert(unescape(window.location.search.substring(1)));
document.optoutform.email.value = unescape(window.location.search.substring(1));
}
</script>
</head>

<body>
<form name="optoutform" action=" method="POST">
<input type=hidden name="orgid" value="00D00000000hbNd">
<input type=hidden name="retURL" value="<input type="HIDDEN" name="external" id="external" value="0">

<br>
<input type="hidden" name="subject" id="subject" value="Emerging Markets Support Request"><br>
<span class="t1bb">EMERGING MARKETS NEEDS ASSESSMENT PROFILE AND SUPPORT REQUEST</span>
<table border="0" cellpadding="0" cellspacing="0" class="colorborder" width=700>
<tr>
<td>Contact Name:</td>
<td><input name="name" id="name" type="hidden" size=40 maxlength=80 /></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="email" id="email" type="hidden" size=40 maxlength=80 /><script language="javascript" >
<!--
document.write(unescape(window.location.search.substring(1)));
-->
</script>
</td>
</tr>
<tr>
<td>Company: </td>
<td><input name="company" id="company" type="hidden" size=40 maxlength=80 /></td>
</tr>
<tr>
<td>Use this field to offer any addtional description or elaboration that will help PMI's Emerging Markets group to support your efforts with this lender.: </td>
<td><textarea name="description" cols="50" rows="10"></textarea></td>
</tr>
</table>

<input name="submit" type="submit" class="t3bb" value="Submit SFDC Case Record to Emerging Markets">

</form>

</body>
</html>
 
The populate function is never being called. It's being defined, but it's not being called. You will need to call that function... probably using the onload event for the page.
Code:
<body onload="populate()">
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
GLORY friggin HELLELUJAH! U rule! Thankyou! u just ended 48 hours novice brainpain!!!!!!!
 
OK Jeff-

Now that that is working. I would like to if possible have my URL include as parameters 3 items: email, name, company

To do so should my URL read ....page.htm?bobsmith@smith.com&bob smith&smithcompany

or is there another way

and if this is the correct way should my function include something like

document.optoutform.name.value = window.location.search.substring(2);
document.optoutform.company.value = window.location.search.substring(3);

???

Thank you again!

Jay
 
To do this, you'll need to pass parameters as I specified earlier, as name / value pairs, like this:

Code:
?param1=value1&param2=value2&...&paramN=valueN

Then parse the search string. There are many examples here of how to do this... use the search facility to search for those posts.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
OK gents I am getting there.

My URL is:

...intranet.mycompany.com/sales/sfdcforms/emneedsform.htm?x&John%20Smith&Celebrity+Savings+%26+Loan&john7smith%40yahoo.com

My code is driving the form submission contents properly, but I think I must be misusing the document.write to show in the form fields the contact name, company name, and email that the form will submit. The form does not "write" as I'm attempting.

Also, my efforts to "unescape" the "company" name fail in that my alerts show "Celebrity+Savings+&+Loan" instead of "Celebrity Savings & Loan". Why does unescape work to convert the "@" and the "&" but not the "+"s?

Thankyou again and I will keep researchign document.write to try to learn and help myself.

Jay

Following is my code snippet:

<script language="javascript" >
function populate() {
var ar = new Array();
ar = document.location.search.split("&");
var name1=unescape(ar[1]);
var company1=unescape(ar[2]);
var email1=unescape(ar[3]);
alert(name1);
alert(company1);
alert(email1);
document.emneeds.email.value = email1;
}
</script>
</head>

<body onload="populate()">
<form name="emneeds" action=" method="POST">
<input type=hidden name="retURL" value="<input type="HIDDEN" name="external" id="external" value="0">

<br>
<input type="hidden" name="subject" id="subject" value="Emerging Markets Support Request"><br>
<span class="t1bb">EMERGING MARKETS NEEDS ASSESSMENT PROFILE AND SUPPORT REQUEST</span>
<table border="0" cellpadding="0" cellspacing="0" class="colorborder" width=700>
<tr>
<td>Contact Name:</td>
<td><input name="name" id="name" type="hidden" size=40 maxlength=80 /></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="email" id="email" type="hidden" size=40 maxlength=80 /><script language="javascript" >
document.write(email1);
</script>
</td>
</tr>
 
I figured it out. I think. It works now that I replicated the variable declarations in a script inside the body. I am guessing this works because in the body I was trying to write variables that were not yet declared by the function because the function run "onload" so.... well u get the idea. or it was blind luck!
 
The problem I solved was getting the variable to write into the form fields. I still do not know why the Company name even after unescaping shows with the "+" symbols between the words. Help on this would be most appreciated. I gather these behave differently that ascii special characters when it comes to URL encoding???
 
argh. I am getting closer. Apparently I need the "replace" method.

using var company1 = (company1).replace("+",' '); causes the 1st instance only of the plus sign to be replaced with a space; but I need all such instances replaced.

i read in my javascript complete ref book that this should work:

s = s.replace(/\./g,"!"); to replace all period with exclamation points; but when i use

var company1 = (company1).replace(/\+/g,' '); it fails

can anyone advise why my use of the replace method is failing? again, thank you! jay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top