9

I'm coding a newsletter and the guy that asked me to do it wants a link in it...all perfect no problems...

Now the problem is that when you click on this link it goes on a page with fields and the guy asked me if it's possible to automatically fill up one of the fields.

The page is a subscription page for some services and this guy wants me to fill up the referral field automatically when you land on the page with his email. Is it possible?

Thanks heaps

T.Rob
  • 31,522
  • 9
  • 59
  • 103
manujj88
  • 199
  • 2
  • 3
  • 7

3 Answers3

4

Encountered this issue, and the following Javascript code did the trick.

Using @T.Rob query string parameters.

HTML CODE:

<input type="text" name="fillfield" id="fillfield" style="width: 350px;" class="input" value=""/>

JAVASCRIPT CODE:

window.onload=function(){ 

 function querySt(ji) {

 hu = window.location.search.substring(1); 
 gy = hu.split("&");

for (i=0;i<gy.length;i++) { 
ft = gy[i].split("="); 
if (ft[0] == ji) { 
 return ft[1]; 
 } 
 } 
 } 
 var fieldName = querySt("fieldName");


 if( fieldName==null){ 
 }else{ 
 document.getElementById('fillfield').value = fieldName; 
 } 
 } 

When you visit http://www.example.com?fieldName=fieldValue it would automatically fill in fieldValue to the fillfield input.

Community
  • 1
  • 1
Julius
  • 387
  • 6
  • 15
  • How to have first and last name in the field with space between them in the URL I tried + and other solution but did not work or should I edit JavaScript code and How can I do that ? – Falcon_S Jan 26 '22 at 11:14
3

One way to do this is to place the field name and value in the query string. The "query string" is the part of a URL which contains field/value pairs. It is separated from the page address by a ? followed by field=value pairs separated by & characters. For example,

http://www.example.com

...would become...

http://www.example.com?fieldName=fieldValue

In order for this to work, the page must parse the field as part of the HTTP GET request and then return the value as a pre-filled field. If the form is properly validated at the server side then it usually already has this capability of parsing fields and retaining their values across multiple submissions of the same form. This is because doing so allows the page to be redrawn with an error message if some fields are blank or have invalid values.

This is very easy to test - just find the field name in the page source and extend the URL you are currently using as shown above. If it works you are done. If not, you may need to get the code on the server side updated to accept the field as part of the query string.

Other ways to accomplish the same thing are more dependent on the server-side code. For example, many sites embed the associate referral ID in the URL such as:

http://www.example.com/123456/

In this case, server-side code interprets the directory path as a field and parses it accordingly. Because this requires very specific server-side code to support it, the first option is probably your best bet.

T.Rob
  • 31,522
  • 9
  • 59
  • 103
  • I tried already and thanks above all for time you took to try to fix my problem....but doesn't work.... :( any other solution? – manujj88 Nov 30 '11 at 03:58
  • 1
    There's no guaranteed way to do it from the client side. Any link becomes an HTTP(s) GET request and all you have to work with are the protocol, domain, port, path and query string. It is up to the server and/or application how to interpret the parts that get passed to it (i.e. path and query string). Either the support is there for you to use or it isn't. You can't force it to happen from the client side. – T.Rob Nov 30 '11 at 04:35
2

Using php

<?

$passthis = "See you on the other side";

echo '<form action="whereyouwantittogo.php" target="_blank" method="post">'.
'<input type="text" name="passthis1" value="'.
$passthis .' " /> '.
'<button type="Submit" value="Submit" >Submit</button>'.
'</form>';

?>

The script for the page you would like to pass the info to:

<?

$thispassed = $_POST['passthis1'];

echo '<textarea>'. $thispassed .'</textarea>';
echo $thispassed;

?>

Use this two codes on seperate pages with the latter at whereyouwantittogo.php and you should be in business.