www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Display URL of previous page in html form

  1. #1
    Join Date
    Feb 2011
    Posts
    24

    Display URL of previous page in html form

    Hello
    I am a pesky beginner. I create websites using Lauyan WYSIWYG software but sometimes I need to use more complex functions. For the moment I've managed by copying bits of scripts and adaping them but this time I've run into a brick wall. And I am sure it should be easy and all my tries have probably failed for syntax error or a missing var or something.
    So I really would appreciate somebody to take me slowly through this one.

    I am making a site for a travel agency. On each page (over 150) there should be a contact form which should include the tour name or code the person is interested in.

    Rather than creating (and maintaining) 150 contact forms what I would like to do ideally is : keep the 150 pages but add a button to a common "Booking & Enquiries" form. So far that's OK. But I would like the form to have a hidden field with a variable value. This value could just be the URL of the sending page. I can display the value of the current URL with window.location and put it in a var.

    Now I just need how to know how to put it in the wg-formfield
    HTML Code:
    <td align="left" valign="top"><input class="wg-formfield" type="text" name="nameoftour" value="" maxlength="100" style="width:300px;">
    in the value part.

    I've tried creating a javascript displaying the value of the current URL :
    HTML Code:
    <script type="text/javascript">
    var nameoftour = window.location.href;
    document . write (nameoftour);
    </script>
    but I want :
    a) to pass it on to the next page
    b) to display as value in the form either as hidden or as read-only

    Can anybody help please ?
    Thank you very much in advance for your help.

  2. #2
    Join Date
    May 2010
    Posts
    213
    so not for html forum. more like php or asp.

    anyways, using php, after submitting your first form, in your second page you could write that value in a hidden field by writing:
    <input type="hidden" name="nameoftour" value="<?=$_POST['nameoftour'];?>"/>
    Last edited by 3Nex; 02-06-2011 at 10:12 AM.

  3. #3
    Join Date
    Feb 2011
    Posts
    24

    Display URL of previous page in html form

    Thank you 3Nex and sorry for boring you with my intro.

    For the moment everything is in html except the php form for sending to email.

    So it means I have to change my html form to a php one ?

  4. #4
    Join Date
    May 2010
    Posts
    213
    Quote Originally Posted by Optimistic View Post
    sorry for boring you with my intro
    How the hell did you get to read that, i edited that out later (you got it on your email i suppose)


    Quote Originally Posted by Optimistic View Post
    For the moment everything is in html except the php form for sending to email.

    So it means I have to change my html form to a php one ?
    I don't get what do you mean to do with your form if you're not gonna use some server-side programming? I mean, if you already are using forms to do anything, you obviously do own either a PHP or ASP server (PHP, according to your previous post) so the solution i proposed should work just fine in your case.

    Unless i got your question wrong because i'm too lazy to read into your whole process of growing up as a child and getting to one day writing an html form that doesn't work. But as far as i can see, you already got the url in the field of your form and you want to put it in another field on the next form. Therefore, reading it with PHP is in fact the only solution.

  5. #5
    Join Date
    Feb 2011
    Posts
    24

    Display URL of previous page in html form

    Unless i got your question wrong because i'm too lazy to read into your whole process of growing up as a child and getting to one day writing an html form that doesn't work
    Thanks a lot for your welcome to the forum.

  6. #6
    Join Date
    May 2010
    Posts
    213
    I'm usually a very nice really

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    It makes most sense to go ahead and make the form into a server-side page. Then you can check for any passed variables (I'd recommend GET rather than POST so the links will work even if not posted to from a previous form).

  8. #8
    Join Date
    Feb 2011
    Posts
    24
    Quote Originally Posted by 3Nex View Post
    I'm usually a very nice really
    Prove it :
    How do I write a variable value in my html page ?

    I have tried :
    value=nameoftour
    valeue='nameoftour'
    value=&nameoftour
    value=document.write(nameoftour)
    value="<?=$_POST['nameoftour'];?>"
    value=objForm.nameoftour.value
    ....

    What is the correct syntax please.
    Thank you.

  9. #9
    Join Date
    May 2010
    Posts
    213
    Quote Originally Posted by Optimistic View Post
    How do I write a variable value in my html page ?
    Noob :P

    <?php and ?> brackets are used for writing PHP code in between.
    <?php echo $variable;?> is a PHP code for writing a variable called "variable".
    The same thing is done by writing <?=$variable;?>, so use whichever suits you more.

    $_POST is an array of variables that are submitted in a form from the page before (assuming that the form has method="post")
    So, if you had an input field whose name was name="nameoftour", the value that user entered in that field will be accesible in $_POST['nameoftour'].

    Finally, you will write that value out by using <?php echo $_POST['nameoftour'];?> or <?=$_POST['nameoftour'];?>. Naturally, if you wanted (which i suppose you did) to make that into a hidden field again, you will write <input type="hidden" name="name" value="<?=$_POST['nameoftour'];?>"/>

    A demo of all i just said can be tried out using the following code on your PHP server: http://pastebin.com/f2JEq1kR
    That same code is available here if you wanna try it out right away: http://turcin.net/php/posted_variable.php

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    There is one caveat when using the shorthand version:
    PHP Code:
    <?=$variable;?>
    This shorthand is disabled by default on PHP installs and thus may or may not be enabled on any particular host. It is considered best practice to use the full statement:
    PHP Code:
    <?php echo $variable;?>
    This is often the cause of "when I moved to a new host my site stopped working..." posts on here.

  11. #11
    Join Date
    Feb 2011
    Posts
    24

    Display URL of previous page in html contact form

    Thank you 3Nex for your time and clear explanations.


    I will be able to use this example for other things.

    Unfortunately in this case I don't have a user input field on the previous (calling) page - it's just a button "Booking & Equiries"
    HTML Code:
    <a href="../contact/index.html"><img style="border:0;" src="bv000004.lnkbut.png" alt="Contact" title="Contact"></a>
    So I don't have an post array of variables to read on the next (called) page.

    But thank you anyway.

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Optimistic View Post
    Thank you 3Nex for your time and clear explanations.


    I will be able to use this example for other things.

    Unfortunately in this case I don't have a user input field on the previous (calling) page - it's just a button "Booking & Equiries"
    HTML Code:
    <a href="../contact/index.html"><img style="border:0;" src="bv000004.lnkbut.png" alt="Contact" title="Contact"></a>
    So I don't have an post array of variables to read on the next (called) page.

    But thank you anyway.
    In this case a GET variable would work plenty sufficiently.


    Example:
    Code:
    <a href="contact.php?type=version1">Version 1</a>

    contact.php
    PHP Code:
    $contactType= (isset($_GET['type']))?$_GET['type']:'version1';

    switch(
    $contactType){
       case 
    'version2':
          
    //Stuff for 'version2' goes here
       
    break;
       case 
    'version3':
          
    //stuff for 'version3' goes here
       
    break;
       case 
    'version1':
       default:
          
    //This where you do stuff specific to "version1" which is also the default
       
    break;


  13. #13
    Join Date
    Jan 2008
    Location
    Florida
    Posts
    1,227
    Why bother with changing all 150 of your forms? Why not just get the referring URL server-side on the page that processes the form?

    If PHP then:

    $ref = getenv("HTTP_REFERER");

  14. #14
    Join Date
    Feb 2011
    Posts
    24

    Display URL of previous page in html form

    Quote Originally Posted by 3Nex View Post
    Noob :P
    BTW - Thanks for the comment. I don't think I can be a real noob because apparently they don't have a sense of humour.

  15. #15
    Join Date
    Feb 2011
    Posts
    24
    Quote Originally Posted by yamaharuss View Post
    Why bother with changing all 150 of your forms? Why not just get the referring URL server-side on the page that processes the form?

    If PHP then:

    $ref = getenv("HTTP_REFERER");
    Thanks for this useful tip. Yes, you did understand what I want to do - not change and maintain 150 pages but centralise all changes to one place.

    I'll try this out and let you know.
    Thank you for your help.

    The Noob
    Last edited by Optimistic; 02-07-2011 at 02:36 PM.

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