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

189
Vistas
how to pass a value from Js to PHP within the same page

I have a Canvas and I already drew my circles there (centers and radius from MySQL DB). Also, the circles are filled in red under some conditions (from DB).

Now I have to do this:

when the user clicks on a red circle, I should show a popup (I thought to an alert) with data taken from MySQL

I'm already able to locate, with Javascript, the coordinates of the pixel the user clicks and I'm able to get the color of that pixel.

What I would need is:

if the pixel is red I should pass that info to PHP (same page) and then I should get some data from DB.

I found lots of functions on the web, Ajax or JQuery, but I'm really unable to integrate into my PHP...

my relevant code below:

//this Js function get the coordinates and the color of the pixel where the user clicks

<script language="javascript">
    function getCursorPosition(canvas, event) {
        const rect = canvas.getBoundingClientRect();
        const x = event.clientX - rect.left;
        const y = event.clientY - rect.top;
        //console.log("x: " + x + " y: " + y);
        
        var ctx = canvas.getContext("2d");
        ctx.beginPath();
        
        var pixel = ctx.getImageData(x, y, 1, 1).data; 
        var hex = "#" + ("000000" + rgbToHex(pixel[0], pixel[1], pixel[2])).slice(-6);
        
        alert("x: " + x + " y: " + y + " hex: " + hex);
    }
</script>

...

// draw the canvas and listen for the event mousedown

<?php
    echo("<div id='text' style='text-align:center;'>");
    echo("<canvas id='myCanvas' width='400' height='600' style='border:1px solid #000000;'></canvas>");
?>
    <script>
        var canvas = document.getElementById('myCanvas'); 
        canvas.addEventListener("mousedown", function (e) { getCursorPosition(canvas, e);});
    </script>
<?php
    echo("</div>"); 
    

...

//loop getting centers and radius from DB and draws the circles on the canvas

$sqltextval = "SELECT * FROM table WHERE col='somevalue'";
$connval = mysqli_query($id,$sqltextval);
if ($rowval = mysqli_fetch_object($connval))
{
    //fill circle in red
    echo("<script type='text/javascript'>");
    echo("fill_circle($X, $Y, $radius);");
    echo("</script>;");
} 
else
{
    //circle not filled in
    echo("<script type='text/javascript'>");
    echo("draw_circle($X, $Y, $radius);");
    echo("</script>;"); 
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I suggest you try fetch

const componentToHex = c => {
  let hex = c.toString(16);
  return hex.length == 1 ? "0" + hex : hex
};
const rgbToHex = (r, g, b) => "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);


function getCursorPosition(canvas, event) {
  const rect = canvas.getBoundingClientRect();
  const x = event.clientX - rect.left;
  const y = event.clientY - rect.top;
  //console.log("x: " + x + " y: " + y);

  var ctx = canvas.getContext("2d");
  ctx.beginPath();

  var pixel = ctx.getImageData(x, y, 1, 1).data;
  var hex = "#" + ("000000" + rgbToHex(pixel[0], pixel[1], pixel[2])).slice(-6);

  return "x: " + x + " y: " + y + " hex: " + hex;
}

var canvas = document.getElementById('myCanvas');
canvas.addEventListener("mousedown", function(e) {
  const pos = getCursorPosition(canvas, e);
  console.log(pos); 
  // uncomment next line to send to server
  // fetch('server.php?' + new URLSearchParams({ pos: pos }))
});
<div id='text' style='text-align:center;'>
  <canvas id='myCanvas' width='400' height='600' style='border:1px solid #000000;'></canvas>
</div>

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