0

Basically I'm struggling adding PHP into

<a style="cursor:pointer;" onClick="location.href='https://www.example.com/go/'">LINK</a>

My code right now:

        <a style="cursor:pointer;" onClick="location.href='https://www.example.com/go/
                                            
<?php $randomid = rand(361, 370);
echo '' . $randomid . '&name=john'; ?>

        '">Link</a>

But the above doesn't work. When you click, nothing happens.

I have also tried this way:

<?php $randomid = rand(361, 370);
echo '<a style="cursor:pointer;" onClick="location.href='https://www.example.com/go/' . $randomid . '&name=john'">Link</a>'; ?>

But since the javascript contains '' they confict with PHP's html code ''.

How do I solve this?

9
  • 1
    Well you would have one "level" of nested string delimiters less to deal with, if you simply used the href attribute to link somewhere, like that is supposed to be done, instead of using JavaScript for the same functionality for no apparent reason. Commented Oct 1, 2021 at 9:57
  • doesn't work isn't a useful description. When you run the PHP code, what does the generated link look like? You can examine it in your browser tools to see what it looks like and spot the mistake. But CBroe is right, using JS click to make a link redirect makes no sense when you can just use the link in the normal way.. Commented Oct 1, 2021 at 9:58
  • 1
    The "doesn't work" probably results from the arbitrary whitespace you introduced there. The resulting text literal inside the single quotes will go over several lines, and that simply is a JavaScript syntax error. Commented Oct 1, 2021 at 10:00
  • Hey CBroe, I have a very good reason to use javascript link... I can't use a static a href link. I'm doing javascript link to avoid bots clicking on links. Commented Oct 1, 2021 at 10:00
  • 1
    Yes or ones which use a headless browser so that JavaScript will run when they click. A more basic crawler would only deal with the raw HTML, but I think these days search engines are more sophisticated Commented Oct 1, 2021 at 11:41

2 Answers 2

2

So ... :) like this - you either use href or use a function onclick... not supposed to be together but this will work:

$randomid = rand(361, 370);?>
<a href="https://www.example.com/go/<?php echo $randomid;?>&name=john" style="cursor:pointer;">Link</a>

From what I read in the comments you are looking for a js function... not an a href link eh?

So how about this:

<?php
$randomid = rand(361, 370);?>
<a onclick="myFunction('<?php echo $randomid;?>');" style="cursor:pointer;">Link</a>
<script type="text/javascript">
    function myFunction(randomid){
        window.location.assign("https://www.example.com/go/"+randomid+"&name=john");
    }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, the javascript code is exactly what I was looking for. Thanks again!
0

Either you can use this one it's also working.

<?php
    $randomid = rand(361, 370);
    echo '<a style="cursor:pointer;" href="https://www.example.com/go/'.$randomid.'&name=john">Link</a>';
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.