• PHP Help General Help
  • [RESOLVED] How to add a variable to a anchor tag that changes depending on which webpage I am on

I have a fundamental problem that takes a bit of explaining -

This is the code in my navbar which is stored in a file called squarenav.php

<a href="index.php?myvarstf=myvarstf&title=MHAP+-+Forward+This+Page&pagevar=  variable will go here ">
   <div class="square square3"><div class="icontext">Forward This Page</div>
   </div>
   </a> 

It is included in my root document that loads in web page content depending on which navbar button is clicked

include("plug-in/squarenav.php"); 


if (isset($_GET["myvar"])) { $name1 = $_GET["myvar"]; }

if (isset($_GET["myvarstf"])) { $namestf = $_GET["myvarstf"]; }

if (isset($_GET["myvar2"])) { $name2 = $_GET["myvar2"]; }


if (isset($name1)) {

	include("plug-in/home.php");
}
elseif(isset($namestf)){

   include("plug-in/sendtofriend.php");	

    }
   elseif(isset($name2)){		

	include("plug-in/aboutadvoc.php");

} else{
	include("plug-in/home.php");
}

one of the "web content" pages called (send web page to a friend) allows the user to
send the url of the page they are currently on to a friend -

sendtofriend.php

<?php if (isset($_GET['title'])) { ?>
  <h1>Just Fill In a Few Details</h1>
  <div class="center_form">
  <form name="form1" method="post" action="">
<p>
  <label for="friend">Your friend's name:</label>
  <input type="text" name="friend" id="friend">
</p>
<p>
  <label for="to">Your friend's email address:</label>
  <input type="text" name="to" id="to">
</p>
<p>
  <label for="you">Your name:</label>
  <input type="text" name="you" id="you">
</p>
<p>
  <label for="sender">Your email address:</label>
  <input type="text" name="sender" id="sender">
</p>
<p>
  <label for="comments">Comments:</label>
  <textarea name="comments" id="comments"></textarea>
</p>
<p>
  <input type="submit" name="send" id="send" value="Send to Friend">
  <input name="title" type="hidden" id="title" value="<?php echo $_GET['title']; ?>">
  <input name="url" type="hidden" id="url" value="<?php if (isset($_SERVER['HTTP_REFERER'])) {
    echo $_SERVER['HTTP_REFERER'];
  }?>">
</p>
</form>
</div>
<?php } else { ?>
<h1>Oops!</h1>
<p>You seem to have landed on this page by error.</p>

this is a typical page that the user could send a link to

aboutadvoc.php

<h1 class="cent">About Advoc</h1>

$results = $connection->query("SELECT message FROM aboutadvoc");

while($row = $results->fetch_assoc())
{
   echo '<p>' . $row["message"] . '</p>';
}
//close db connection
$connection->close();

So the user is on aboutadvoc.php the navbar is above with a button called Send to Friend,
which ideally when clicked loads the send to friend page with details of the aboutadvoc.php page

So say I am on another page ... home.php and I have the same navbar above and I click the
Send to Friend button. How does sendtofriend.php know which page I am coming from?

My first thought was to add a variable to the navbar $page_details

<a href="index.php?myvarstf=myvarstf&title=MHAP+-+Forward+This+Page&pagevar=$page_details">

And say on aboutadvoc.php have

$page_details = " About Advoc";

An on home.php have

$page_details = " Home Page";

However when I try this, the browser address bar shows pagevar= as empty

http://localhost/advoc/index.php?myvarstf=myvarstf&title=MHAP+-+Forward+This+Page&pagevar=

So after all that... few...

How do I add $page_details to the navbar button and change its content depending on which page I am currently on?

<a href="index.php?myvarstf=myvarstf&title=MHAP+-+Forward+This+Page&pagevar=$page_details">

    I'll admit I did not read through everything there (what can I say, I'm lazy), but the main thing is to ensure that the variable is defined before you include the file that uses it (if, in fact, that's how you choose to do it).

    My preference would probably be to define the section with the link within a function, to which you could then pass the desired value.

    include file with menu stuff:

    <?php
    
    function menu($page_details)
    {
        $html = '
    <ul id="menu">
    <li>something</li>
    <li><a href="/refer_a_friend?page_var='.urlencode($page_details).'">tell a friend</a></li>
    <li>something else</li>
    </ul>
    ';
        return $html;
    }
    

    File that needs to use that function:

    <?php
    require_once 'path/to/include/file.php';
    ?>
    <html>etc....
    <p>some html</p>
    <?php echo menu('text for this page'); ?>
    <p>more html...</p>
    

      Seems like some server environment variables might help?

      In particular, $SERVER['SCRIPT_FILENAME'], $SERVER['PHP_SELF'] and especially $_SERVER['REQUEST_URI'] often seem to help in situations like these ....

        Hi, Thanks for the help.
        I used NogDog's advice, "ensure that the variable is defined before you include the file"
        and moved the code

                                                           // I also added $page_details here
        if (isset($_GET["myvar"])) {$name1 = $_GET["myvar"]; $page_details = "Home Page";} 
        if (isset($_GET["myvar2"])) {$name2 = $_GET["myvar2"]; $page_details = "About Advoc";}

        above the navbar

        include("plug-in/navbar.php");

        And things are now working as required!

        I will also take on board the advice about using Environment variable.

        Many thanks

          Write a Reply...