Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda