SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member twinbee's Avatar
    Join Date
    Aug 2006
    Location
    England
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing variables to the HTML with javascript?

    Is there any simple way of passing HTML variables from one HTML page to another HTML page without using PHP or messy URL extensions?

    I presume some kind of Javascript will be needed.
    ---
    skytopia.com

  2. #2
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML does not support variables as such, they are a programming construct and HTML is a markup language, not a programming language. If you mean HTML properties, they are specific to a page, though CSS can allow a style sheet to be shared between multiple pages, giving similar elements similar display properties across pages. JavaScript can pass actual variables (like: x = 3, then passing the x variable) between pages using cookies. Additionally, some back door techniques exist, like using the window.name property to pass strings.

    To learn more try the W3C's W3 Schools at http://www.w3schools.com/

  3. #3
    SitePoint Member twinbee's Avatar
    Join Date
    Aug 2006
    Location
    England
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I wasn't too keen on cookies, because of their relative complexity and that not all people enable them.

    I was hoping to pass the 'html variables' (or whatever you want to call them) using hidden form fields:
    <input type="hidden" name="myvar" value="34">

    Unfortunately, I don't think you can submit a form to another HTML page, as I get an error. This is a shame - why can't they make the HTML standard so that it does?

    Perhaps you can tell me more about the back door techniques you spoke of (give me the shortest/simplest/scalable way).
    ---
    skytopia.com

  4. #4
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forms require processing at the server. With server-side scripts you can pass values to the new pages that result from the server scripts, so, in reality what you ask for as an HTML standard already exists; it just requires logic at the server to build the HTML page. Again, HTML has nothing to do with logic or processing, it is simply a markup language, nothing more and, in fact, that is quite intentional.

    To use JavaScript to modify the properties of your HTML pages you will have to do a lot more than just pass variables between pages; you will have to manipulate the DOM to effect such modifications. Passing the variables is the easy part. Then you must parse the variables, extract object references from the page, and modify the objects' properties. There is, unfortunately, no shortcut here. You need to learn JavaScript, the Document Object Model, and Cascading Style Sheets before you can reasonably proceed. Again, I refer you to the W3Schools tutorials.

  5. #5
    SitePoint Member twinbee's Avatar
    Join Date
    Aug 2006
    Location
    England
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, in that case, I think I'll stick to using PHP.

    Unfortunately, I have a problem though. The variables pass on one site I manage, but not on another. Both use exactly the same code for the HTML and PHP page.

    The PHP page which will be on the receiving end of the form just contains a test print.

    Code:
    <h2>
    	<?php
    		echo("Passed variable is: ".$myVar);
    	?>
    </h2>
    The HTML page contains the main form:

    Code:
    <script type="text/javascript">
    	function val(t) {
    		document.myForm.myVar.value = t
    		myForm.submit();
    	}
    </script>
    
    <form name="myForm" action="test2.php" method="POST">
    	<input type="hidden" name="myVar">
    	Pass variable - <a href="test2.php" onclick="val('12345'); return false">Click me</a>
    </form>
    Now on one server, if I click the hyperlink, I get the expected:
    "Passed variable is: 12345"

    But on the other server, I only get:
    "Passed variable is: "

    What's going on? Is there a small change I can do to make it work on both?

    I can give you the URLs as long as it isn't against the TOS.
    Last edited by twinbee; Aug 16, 2006 at 16:18.
    ---
    skytopia.com

  6. #6
    SitePoint Member twinbee's Avatar
    Join Date
    Aug 2006
    Location
    England
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I've just realised that I I have to use the special $_POST['myVar'] command instead. This may probably be because the PHP version is earlier.
    ---
    skytopia.com


Bookmarks

Posting Permissions

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