[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

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.

0
kbios
Asked:
kbios
  • 6
  • 5
1 Solution
 
ghodderCommented:
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

0
 
COBOLdinosaurCommented:
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>

0
 
kbiosAuthor Commented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
COBOLdinosaurCommented:
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



0
 
kbiosAuthor Commented:
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.
0
 
COBOLdinosaurCommented:
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.
0
 
kbiosAuthor Commented:
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>
0
 
COBOLdinosaurCommented:
Yes setting.href like that should work.  Are you actually getting anything in the variable from localStorage?
0
 
kbiosAuthor Commented:
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.
0
 
kbiosAuthor Commented:
Thanks again.
0
 
COBOLdinosaurCommented:
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.
0
 
kbiosAuthor Commented:
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now