www.webdeveloper.com
Results 1 to 9 of 9

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

  1. #1
    Join Date
    Jun 2009
    Posts
    26

    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?

  2. #2
    Join Date
    Jun 2009
    Posts
    111
    Quote Originally Posted by speed1972 View Post
    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?
    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

    PHP Code:
    <input type="text" name="trade" value="<?php echo addslashes($_GET['trade']); ?>">
    Make sure you keep the addslashes() or someone could inject content into your page.

  3. #3
    Join Date
    Mar 2009
    Posts
    25

    Arrow Code of both pages

    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>

  4. #4
    Join Date
    Jun 2009
    Posts
    111
    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.

  5. #5
    Join Date
    Jun 2009
    Posts
    26
    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.

  6. #6
    Join Date
    Jun 2009
    Posts
    111
    Quote Originally Posted by speed1972 View Post
    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.
    Looks like your server doesn't have PHP available. Try this instead...

    Put this in the <head>
    Code:
    <script type="text/javascript">
    function get(variable) {
    	var query = window.location.search.substring(1);
    	var vars = query.split("&");
    	for (var i=0;i<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'));
    }
    }
    </script>
    (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:
    Code:
    <input type="text" name="trade" id="trade">
    And rename the page to .html instead of .php.
    Last edited by Four Staples; 06-06-2009 at 02:51 PM.

  7. #7
    Join Date
    Jun 2009
    Posts
    26
    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>

  8. #8
    Join Date
    Jun 2009
    Posts
    111
    Just put it anywhere in between <head> and </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" />
    <script type="text/javascript">
    function get(variable) {
    	var query = window.location.search.substring(1);
    	var vars = query.split("&");
    	for (var i=0;i<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'));
    }
    }
    </script>
    </head>

  9. #9
    Join Date
    Jun 2009
    Posts
    26
    Thanks for all the help, managed to get it working exactly as you said.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles