Link to home
Start Free TrialLog in
Avatar of kbios
kbios

asked on

How I include a variable in an HTML href statement?

Here is my HTML that 'calls' a php routine.

<li><a href="verifycart.php?ukey="ukey>Verify Cart</a></li>

I have a variable (ukey) that I want to pass to the PHP. I have tried many options, but I need help with the syntax of passing the value to the PHP routine.

Any help would be greatly appreciated.

Avatar of ghodder
ghodder
Flag of Australia image

If it's coming from your request URI:

<li><a href="verifycart.php?ukey=<?php echo $_GET["ukey"]; ?>">Verify Cart</a></li>

Open in new window


If it's a standard PHP variable in the script:

<li><a href="verifycart.php?ukey=<?php echo $ukey; ?>">Verify Cart</a></li>

Open in new window

If ukey is a javascript variable you need to change the name so you can do:

 <li><a href="verifycart.php?ukey=JSukey">Verify Cart</a></li>

Avatar of kbios
kbios

ASKER

The variable ukey is created via javascript in my HTML:

<script type="text/javascript" language="javascript">
    var ukey = localStorage.ukey;
</script>


 <li><a href="verifycart.php?ukey=JSukey">Verify Cart</a></li> does not work. Are you stating that the variable must be prefaced with JS?

Using .....  verifycart.php?ukey=<?php echo $ukey; ?>">  doesn't work either. I don't think I can pass the PHP variable $ukey; it is not yet created.
No I am not saying you have to prefix with JS.  I am saying ukey=ukey in the href cannot possible work thr variabel needs a different name:

  var XXXukey = localStorage.ukey;

Then  href="verifycart.php?ukey=XXXukey"  should work



Avatar of kbios

ASKER

Using a different variable name doesn't work either. With the variable name enclosed within the double quotes it it treated as part of the string. It's as if the variable needs to be concatenated to the string. I've tried several options but no luck.
What are you seeing on the server side?  Is anything going across?  Is the javascript variable getting populated?  Is localStorage enabled?  Have you tried setting the variable with XXXkey=localStorage.getItem("ukey")';

Sorry about all the questions, but you are not being very informative.  Telling me it does not work does not give any information, so I have to make guesses.

If the link won't take variable, then it might br necessary to use a hidden form and trigger the submit from the link.  However before we go that route, let's find out what is actually happening.
Avatar of kbios

ASKER

No I've not tried: XXXkey=localStorage.getItem("ukey")';    But I will.

When I use XXXukey and it's enclosed within the double quotes, it's pass ukey=XXXukey vs. ukey=value of the variable.

I just found this and am testing, I think this is working. Please look at this code and see if this is acceptable or possibly another alternative:

<script type="text/javascript" language="javascript">
    var xukey = localStorage.ukey;
    openPage = function() {
        location.href = "verifycart.php?ukey="+xukey;
    }
</script>

<li><a href="javascript:openPage()">Verify Cart</a></li>
ASKER CERTIFIED SOLUTION
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbios

ASKER

Yes.

xukey=localStorage.ukey       when I (alert) the variable xukey it contains the proper value.

I would prefer to use your method of simply passing the variable (XXXukey) but there must be some special syntax. Becasue when it is contained within the quotes it is just treated as text. If you can think of some other syntax option please let me know.

Thanks for all of your help.
Avatar of kbios

ASKER

Thanks again.
As long as location.href gets you there it is good.  The other option I was going to suggest was a hidden form, but the way you did it is less coding.  I'm just glad you got it solved.  Thanks for the A, but I'm not sure if I earned it.
Avatar of kbios

ASKER

You earned it. You stuck with me and offered suggestions and helped me along the way. I appreciate it. This is a big learning curve and I appreciate you sharing your expertise.