Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda