Hasan Abdul Rahman
Hasan Abdul Rahman

Reputation: 57

How to pass a PHP variable to a link in the same page

I am a newbie in programming and this might be very basic. (I searched in the internet but can't find the solution).

My case: In a HTML file, I am retrieving a url parameter by $_GET['s1'] and this is the code:

<?php $username=$_GET['s1']; ?>

I am successful in doing thing but what I need to do is to get the $username variable to be inside a link in the same page.

This link is actually a button in the same page. Once clicked, it will take you to another page. and its code is as follow:

<a href="http://www.mywebsite.com/AM/index.php/myusername_1 " id="btn_1_eed2a4ea6f9ab0ffcae6eced4295bd8e" class="css-button style-1"><span class="text">BOOK</span><span class="hover"></span><span class="active"></span></a>

I want to replace myusername in http://www.mywebsite.com/AM/index.php/myusername_1 with $username.

I might not be able to change the the button code as I am using a program called "optimizepress" to build this page. I can only insert the link of the button and insert a custom HTML or a shortcode anywhere in the page.

This page is to be given to my affiliates and I don't want to insert every affiliate's unique link in his duplicate of this page. I just want my affiliates' links to get generated automatically.

THANKS in advance

Upvotes: 0

Views: 2471

Answers (3)

Shomz
Shomz

Reputation: 37701

Simply echo it inside the HTML (I used a shortcode, but <?php echo $username; ?> still works):

<a href="http://www.mywebsite.com/AM/index.php/<?= $username ?>" 
   id="btn_1_eed2a4ea6f9ab0ffcae6eced4295bd8e" 
   class="css-button style-1">
     <span class="text">BOOK</span>
     <span class="hover"></span>
     <span class="active"></span>
</a>

If you might have some non-URL-safe characters, make sure to urlencode the username first.

Upvotes: 1

Hitesh
Hitesh

Reputation: 562

You can use following -

   <?php
   $username = htmlspecialchars($_GET['s1']); 

    ?>

And for your HTML Code do this -

<a href="http://www.mywebsite.com/AM/index.php/<?php echo $username; ?> " id="btn_1_eed2a4ea6f9ab0ffcae6eced4295bd8e" class="css-button style-1"><span class="text">BOOK</span><span class="hover"></span><span class="active"></span></a>

Upvotes: 1

elbblick
elbblick

Reputation: 336

Try this:

<a href="http://www.mywebsite.com/AM/index.php/<?php echo $username; ?>_1 " id="btn_1_eed2a4ea6f9ab0ffcae6eced4295bd8e" class="css-button style-1">
  <span class="text">BOOK</span>
  <span class="hover"></span>
  <span class="active"></span>
</a>

The PHP Preprocessor will replace the <?php echo $username; ?> with the content of the variable.

Upvotes: 0

Related Questions