• 0
Sign in to follow this  
three

[Resolvido] variáveis JS -> PHP.

Question

Ei gente,

Arrumei num fórum um scritp que pega as coordenadas de um click do mouse num imagem:

var x = null;
var y = null;
var x2 = 0;
var y2 = 0;
var clicky = 0;
var clicks = Array();

wichX = 'x';
wichY = 'y';
function $(o){
    return document.getElementById(o);
}
function point_it(event){
    turn = clicky%2;
    
    if (!turn){
        wichX = 'x';
        wichY = 'y';
    }else{
        x = x2;
        y = y2;
        wichX = 'x2';
        wichY = 'y2';
        
    }
    
    pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("myCanvas").offsetLeft;
    pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("myCanvas").offsetTop;
    document.getElementById(wichX).value = pos_x;
    document.getElementById(wichY).value = pos_y;
    
    eval (wichX + ' = ' + pos_x);
    eval (wichY + ' = ' + pos_y);
    
        if (x2 > 0){
            line(x, y, x2, y2);
        }
            
    // aqui o array de variaveis...
    clicks[clicky] = x + "|" + y;
    clicky++;
}
...mas preciso mandar esses valores de x, y, x2 e y2 pra um código PHP....
//f15_11pt, f15_12pt, f17_11pt, f17_12pt, f18_11pt, f18_12pt, Mappe1, Mappe2
$cvsfile = "f18_12pt";

$content = ""; // Output in a .txt file
$content_user = "";                                                    
$row = 0; // Number of lines in CVS file = number of users
$clicks = 0; // Clicks in Total
$area_clicks = 0; // Clicks inside the area
$more_than_one = 0; // Click that are not the first for some user
$check = 0; // Help var
$users = 0;

$xleft = 90;        // X left limit
$yleft = 90;        // Y left limit
$xright = 300;      // X right limit
$yright = 300;      // Y right limit

//$im = imagecreatefromjpeg("f18_12pt.jpg");
//imagepng($im);                                                   

$handle = fopen ($cvsfile.".csv","r");
// fopen abre o (arquivo, ) e "r" define que é apenas leitura 

while ( ($data = fgetcsv ($handle, 1000, "\n")) !== FALSE )
{
    $num = count ($data);
    $check = $area_clicks;
    //echo $check.'/'.$user_clicks.'<br>';
    $row++;
    $content_user .= 'User: '.$row."\n";
    for ($c=0; $c < $num; $c++)
    {
        //echo $data[$c].'<br>';echo '<br>';
        if (preg_match_all("|x=([0-9]{1,4})|",$data[$c],$matchx)) 
        {
            $users++;
            //print_r($matchx); echo '<br>';       
            $nx = count($matchx[0]);
            //echo $nx.'<br>'; 
            for ($d=0; $d < $nx; $d++)
            {
                //echo 'Count of over clicks: '.$check.'<br>';
                $splitx = explode("=",$matchx[0][$d]);
                $clicks = $clicks +1;
                //echo $splitx[1].'<br>';                
                
                if ($xleft < $splitx[1] &&  $splitx[1] < $xright)
                {
                    
                    if ($check != $area_clicks)
                    {
                      $more_than_one++;
                    }
                    //print_r($splitx); echo '<br>';
                    //echo $splitx[1].'<br>';                    
                    if (preg_match_all("|y=([0-9]{1,4})|",$data[$c],$matchy)) 
                    {
                        //print_r($matchy); echo '<br>';                              
                        $splity = explode("=",$matchy[0][$d]);
                        //echo $splity[1].'<br>';
                        if ($yleft < $splity[1] &&  $splity[1] < $yright)
                        {
                            //print_r($splitx); echo '<br>';
                            //echo 'Par x,y: '.$splitx[1].' '.$splity[1].'<br>';
                            $area_clicks = $area_clicks + 1;
                            $content_user .= 'Total over click: '.$more_than_one."\n";
                        }
                    }
                }
            }
        }

    }
}

fclose ($handle);
... pra substituir os valores 90, 90, 300 e 300 no código...
$xleft = 90;        // X left limit
$yleft = 90;        // Y left limit
$xright = 300;      // X right limit
$yright = 300;      // Y right limit

não sei muito Javascript (quase nada) mas foi dessa forma que arrumei como pegar essas coordenadas.

Como mandar esses valores pra meu script PHP?

Obrigado

Three

Edited by three

Share this post


Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0

você pode jogar os valores com javascrit num input hidden e pegá-los com o php

pode enviar para outra página via QueryString

Share this post


Link to post
Share on other sites
  • 0

Obrigado pela dica,

Preciso mostrar no browser os valores de x e y quando clicados... se usar o input hidden eles não aparecem na tela.

Comecei a olhar algo sobre querystring mas é um pouco confuso pra mim. =( Como faria para passar essas variáveis usando querystring?

Gostaria que ficasse algo do tipo:

www.enderecodapagina.php?x=()&y=()&x2=()&y2=(), onde () é o valor que vem do JS.

Se pudesse me dar uma idéia do trecho de codigo que possívelmente funcione seria muito útil.

Muito obrigado mais uma vez,

Three

Share this post


Link to post
Share on other sites
  • 0

exemplo:

vl1 = 10;
vl2 = 20;
vl3 = 30;
vl4 = 40;

url = 'http://www.enderecodapagina.php?x=' + vl1 + '&y=' + vl2 + '&x2=' + vl3 + '&y2=' + vl4;
window.location.href=url;

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this