/    Sign up×
Community /Pin to ProfileBookmark

How do i link a field in a form to previous page?

Is there a way so that when someone clicks on the ‘book me an alarm specialist’ link it will automatically fill in the trade section of the form on the page it links to. I have about 20 pages linking to the form page all coming from a different trade.

I have added this bit of code to the initial page :

<a href=”http://www.elitetradesmen.com/searchtrades.php?trade=Alarm%20Specialist”>Book me an alarm specialist</a>

and this bit to the form page:

<a href=”http://www.elitetradesmen.com/searchtrades.php?trade=Alarm%20Specialist”>Book me an alarm specialist</a>

I’ve clearly done something wrong as it should come up with alarm specialist in the form box but its showing some of the code. What have i done wrong?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@Four_StaplesJun 06.2009 — Is there a way so that when someone clicks on the 'book me an alarm specialist' link it will automatically fill in the trade section of the form on the page it links to. I have about 20 pages linking to the form page all coming from a different trade.

I have added this bit of code to the initial page :

<a href="http://www.elitetradesmen.com/searchtrades.php?trade=Alarm%20Specialist">Book me an alarm specialist</a>

and this bit to the form page:

<a href="http://www.elitetradesmen.com/searchtrades.php?trade=Alarm%20Specialist">Book me an alarm specialist</a>

I've clearly done something wrong as it should come up with alarm specialist in the form box but its showing some of the code. What have i done wrong?[/QUOTE]


Putting the variable in the URL like that only makes it available as a query string variable, it won't fill in any forms.

Where you have your input box, put this

[code=php]<input type="text" name="trade" value="<?php echo addslashes($_GET['trade']); ?>">[/code]

Make sure you keep the addslashes() or someone could inject content into your page.
Copy linkTweet thisAlerts:
@meralJun 06.2009 — Here is the complete code for your problem



index.php [where the link of 'Book me an alarm specialist']
-------------------------------------------------------



<html>

<body>

<p><a href="http://www.elitetradesmen.com/searchtrades.php?trade=Alarm&#37;20Specialist">Book me an alarm specialist</a></p>

</body>

</html>


searchtrades.php [where the link redirect after clicking]
------------------------------------------------------------


<?php

$tradeval = addslashes($_REQUEST["trade"]);
?>

<html>

<body>

<form action="" method="post">

<input type="text" name="trade" id="trade" value="<?=$tradeval?>" />

<input type="submit" name="submit" value="submit" />

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@Four_StaplesJun 06.2009 — Note that for Meral's to work, your server will need to have PHP shortt ags enabled. If not, use my PHP from the other post.
Copy linkTweet thisAlerts:
@speed1972authorJun 06.2009 — Sorry not sure where i paste Merals code within my code,

I've put

<input type="text" name="trade" value="<?php echo addslashes($_GET['trade']); ?>">

into my code and i'm still having the same problem ?

http://www.elitetradesmen.com/alarmspecialists.html

then click on the book alarm specialist link, doesn't fill in the correct info in the trade field of the form.
Copy linkTweet thisAlerts:
@Four_StaplesJun 06.2009 — Sorry not sure where i paste Merals code within my code,

I've put

<input type="text" name="trade" value="<?php echo addslashes($_GET['trade']); ?>">

into my code and i'm still having the same problem ?

http://www.elitetradesmen.com/alarmspecialists.html

then click on the book alarm specialist link, doesn't fill in the correct info in the trade field of the form.[/QUOTE]


Looks like your server doesn't have PHP available. Try this instead...

Put this in the <head>&lt;script type="text/javascript"&gt;
function get(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&amp;");
for (var i=0;i&lt;vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
window.onload = function() {
var trade = document.getElementById('trade');
if(trade.value == '') {
trade.value = unescape(get('trade'));
}
}
&lt;/script&gt;
(got the function it from http://snipplr.com/view/623/get-querystring-variable/ )

And give the trade input an ID of 'trade', and remove the value:&lt;input type="text" name="trade" id="trade"&gt;

And rename the page to .html instead of .php.
Copy linkTweet thisAlerts:
@speed1972authorJun 06.2009 — Ok not sure where to paste your code, here is my head code:

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

<title>Elite Tradesmen of Southport &amp; surrounding areas</title>
<link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />


</head>
Copy linkTweet thisAlerts:
@Four_StaplesJun 06.2009 — Just put it anywhere in between <head> and </head>:&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;

&lt;title&gt;Elite Tradesmen of Southport &amp;amp; surrounding areas&lt;/title&gt;
&lt;link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" /&gt;
&lt;script type="text/javascript"&gt;
function get(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&amp;");
for (var i=0;i&lt;vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return pair[1];
}
}
}
window.onload = function() {
var trade = document.getElementById('trade');
if(trade.value == '') {
trade.value = unescape(get('trade'));
}
}
&lt;/script&gt;
&lt;/head&gt;
Copy linkTweet thisAlerts:
@speed1972authorJun 06.2009 — Thanks for all the help, managed to get it working exactly as you said.
×

Success!

Help @speed1972 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...