SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Using an a href with php code

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using an a href with php code

    Code:
    <?php
        $id = 5;
        session_start();
        function fn1(){
    		//code
    		return "http://www.philosophaie.heliohost.org/Julian_Date.php?"&data=$id";
        }
        session_write_close();
    ?>
    
    
    <a href = "<?php echo fn1(); ?>">Text</a>
    The a href does not work and I keep getting:

    Code:
    %34http://www.philosophaie.heliohost.org/Julian_Date.php?"&data=5
    on the link mouseover. How do I remove the %34 to execute the link?

  2. #2
    SitePoint Mentor bronze trophy
    chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,057
    Mentioned
    9 Post(s)
    Tagged
    1 Thread(s)
    The end of your string is the problem, see the below which I've highlighted in red to show the problem.

    Code:
    Julian_Date.php?"&data=$id";
    There is no reason to have the double quotes there as well as the ampersand after the double quote as your creating an improperly formatted URL, if you need to at any point use symbols and such in a URL you need to use the urlencode() function.
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2010
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was really:

    return "http://www.philosophaie.heliohost.org/Julian_Date.php?sec=$urladd"."&data=$id";
    and I still got the %34 error.

  4. #4
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    243
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    You have to pass the $id to the function, because is defined outside the function. Try this code:
    Code:
    <?php
    $id = 5;
    session_start();
    function fn1($id){
      //code
      return "http://www.philosophaie.heliohost.org/Julian_Date.php?&data=$id";
    }
    session_write_close();
    ?>
    
    <a href = "<?php echo fn1($id); ?>">Text</a>
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

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
  •