-1

I am using PHP to output HTML. I have a javascript function which returns a value, this function is called in anchor tag and the whole anchor tag is displayed using PHP.

Javascrip function which I want to call:

enter image description here

and here is the PHP code:

echo '<a href="abc.php?a=true&signature=\'<script>getDigitalSignatureToggleValue();</script>\'">ABC</a>';

In the above case, the complete function name along with script tag is getting printed instead of getting called. How to call the function?

gegobyte
  • 4,945
  • 10
  • 46
  • 76
  • You are not going to be able to do that. You want to onclick call a function and generate the url. – epascarello Apr 28 '20 at 19:50
  • JavaScript needs to be in the document before you can use it. Now it's just a string, so the browser won't know what to do with it. – Emiel Zuurbier Apr 28 '20 at 20:02
  • Your script tag is being interpreted as a string since it's in a href attribute. You need to move your script tag to below the element and target it using a selector by passing it an id or something to target it. Then just update the target element's href using your JS function. You shouldn't need PHP at all to achieve this. – J Dubuis Apr 28 '20 at 20:04
  • @epascarello Thanks, that's what I did to generate the url. I have posted the code I used as an answer below. – gegobyte Apr 29 '20 at 08:21

3 Answers3

1

From the other answers and the comments, it is clear that the solution to this issue is to call javascript function using onclick attribute of anchor tag. So I passed the element id to the function like this:

echo '<a id="<id-here>" onclick="generateUrl(this.id)">ABC</a>';

and in javascript function, after building the url, I am simply assigning the url to href attribute by fetching the element which can be accessed using the id which was passed to the function:

enter image description here

gegobyte
  • 4,945
  • 10
  • 46
  • 76
0

Move your JavaScript in a .js file or a <script> tag. That's its home and where it can do its thing.
Listen for the 'click' event on all the links that you want to listen to. In the event handler, which is the function that is executed after a, in this case, click select the href property of the currently clicked link.

Construct a new url with new URL() and pass the href value to it. This will build an object with all the bits and pieces that make up a URL. This new object has a property called searchParams. This is an interface of URLSearchParams which allows you to manipulate all the values after the ? symbol. In your case you want to set the 'signature' value based on the getDigitalSignatureToggleValue().

Now you've created a new URL which you can do anything with that a URL can do.

function getDigitalSignatureToggleValue() {
  let digitalSignatureStatus = document.getElementById('signature').checked;
  return digitalSignatureStatus;
}

function onLinkClick(event) {
  let href = event.target.href;
  let url = new URL(href);
  url.searchParams.set('signature', getDigitalSignatureToggleValue());
  url = url.toString();
  console.log(url);
  // Do something with the url here.
  event.preventDefault();
}

const links = document.querySelectorAll('.js-build-url');
links.forEach(link => {
  link.addEventListener('click', onLinkClick);
});
<input id="signature" type="checkbox">
<a class="js-build-url" href="abc.php?a=true">ABC</a>
Emiel Zuurbier
  • 19,095
  • 3
  • 17
  • 32
0

Script in href won’t get executed if you call with link.

You can use like.

Note: calling JavaScript directly with tag is not a good idea but below script will work.

<script>
function getDigitalSignatureToggleValue(page){
    var check = document.getElementById('signature').checked;
    window.location.href=page+".php?a=true&signature="+check;
}
</script>
<input type = "checkbox" id="signature">signature
<?php
echo '<a href="javascript:getDigitalSignatureToggleValue(\'abc\');">ABC</a>';
 ?>

It will be good if you call the javascript function with event like click.

 <?php

   echo '<a onclick="javascript:getDigitalSignatureToggleValue(\'abc\');">ABC</a>';

    ?>
Shivendra Singh
  • 2,986
  • 1
  • 11
  • 11