How to pass value of a nachor tag

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuragpj
    New Member
    • Jan 2007
    • 32

    How to pass value of a nachor tag

    I want that when a user click on a link The value of the anchor tag should pass on to yhe next page.
    suppose there is a tag like this:
    <a href="">Click Here</a>
    I want that 'Click Here' should be stored in a variable.
    How can I do this?
  • quill
    New Member
    • Mar 2007
    • 12

    #2
    Originally posted by anuragpj
    I want that when a user click on a link The value of the anchor tag should pass on to yhe next page.
    suppose there is a tag like this:
    <a href="">Click Here</a>
    I want that 'Click Here' should be stored in a variable.
    How can I do this?
    You would do something like this:

    Code:
    <a href="page.php?message=hi">Message</a>
    The text inbetween the <a> tags is not passed, what is passed is after the question mark ("message"), which in this example equals "hi".

    And in page.php you would get the message by

    Code:
    <?php
    echo $_GET['message'];
    ?>

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      The suggestion quill posted is probbly the simplest way, which I use quite frequently myself.

      There are more complex ways, however, using javascript and / or Ajax.

      Just for fun, this a way to complex way to do what you need.
      [PHP]
      <script language="javas cript">

      function send_tag(id)
      {
      // Create form
      var form = document.create Element("form") ;
      form.setAttribu te("action", "#");
      form.setAttribu te("method", "post");

      // Create element
      var elem = document.create Element("input" );
      elem.setAttribu te("type", "hidden");
      elem.setAttribu te("name", id);
      elem.setAttribu te("value", document.getEle mentById(id).in nerHTML);

      // attach the elements
      form.appendChil d(elem);
      document.body.a ppendChild(form );

      // submit
      form.submit();

      // Delete the form
      document.body.r emoveElement(fo rm);
      }

      </script>

      <a id="link" href="#" onClick="javasc ript: send_tag(getAtt ribute('id'));" >I will be passed</a>

      <p>
      <?php

      if(isset($_POST['link']))
      {
      echo "Passed data = ". $_POST['link'];
      }

      ?>
      </p>[/PHP]

      Comment

      • anuragpj
        New Member
        • Jan 2007
        • 32

        #4
        Originally posted by quill
        You would do something like this:

        Code:
        <a href="page.php?message=hi">Message</a>
        The text inbetween the <a> tags is not passed, what is passed is after the question mark ("message"), which in this example equals "hi".

        And in page.php you would get the message by

        Code:
        <?php
        echo $_GET['message'];
        ?>
        It is not working.
        page.php does not print anything.

        Comment

        • wheel54321
          New Member
          • Mar 2007
          • 1

          #5
          What quill meant is:

          Code:
          <!-- save this as valuepage.php -->
          
          <?php
          $value = "This is kool, man";
          ?>
          <a href="page.php?message=<?php echo $value; ?>">Message</a>
          
          <!-- end of valuepage.php -->
          
          <!-- now save this as page.php -->
          
          <?php
          echo $_GET['message'];
          ?>
          <!-- when clicking on Message, you will get a new page.php 
          displaying the $value -->

          Comment

          • srijan2005
            New Member
            • Apr 2007
            • 4

            #6
            Originally posted by anuragpj
            It is not working.
            page.php does not print anything.

            hey anuragpj ....the code is runing properly...plz chek it out...

            Comment

            Working...