Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
Combining PHP into a Javascript?

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?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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>
about 4 years ago · Juan Pablo Isaza Report

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>';
 ?>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!