Keith D Kaiser Keith D Kaiser - 1 month ago 11
HTML Question

I need help building an anchor <a that passes variables to PHP

I'm currently using javascript but switching to jQuery is OK.

Within the HTML is a

<div id="domain">nares</div>
. I need to pick out the "nares" and put it in an anchor tag to look like this:
<a href=myCode.php?domain=nares" rel="modal:open">Listing</a>
.

So far the code looks like this:

<script type="text/javascript">
var dom = document.getElementById('domain').innerHTML;
openPage = function() {
location.href = "myCode.php?domain="+dom;
}
</script>

<a href ="javascript:openPage()" rel="modal:open">Link </a>


To me this looks like it should work but instead when the page opens I get;


TypeError: null is not an object (evaluating
'document.getElementById('domain').innerHTML')


This is probably because the div has not yet been populated. However after population the error changes to:


XMLHttpRequest cannot load javascript:openPage(). Cross origin
requests are only supported for HTTP.


Which truthfully I don't understand. In any case using jQuery or just plain javascript how do I build this tag? I really need the modal part to work, its part of the UI for this project.

Answer

You should move your dom element into the function like so:

<a id="test" onclick="openPage()" rel="modal:open">Link  </a>

<script type="text/javascript">
   function openPage() {
      var dom = document.getElementById('domain').innerHTML; 
      document.getElementById("test").href = "myCode.php?domain="+dom;
   }
</script>

Also you should get your a tag then change the href...