Using a php variable in a js file

Asked

Viewed 381 times

0

I have the following problem I have a variable that contains the path of an image in the database I need to send this variable with the path to a js file, but I even found several scripts with examples of how to bring this variable the problem is how I will use it in a function, follows excerpt from the code:

Crop.php file

<?php
$sQuery=mysql_query("SELECT * FROM feridas WHERE ferida_id = $id_ferida");
$row_s = mysql_fetch_object($sQuery);
$img_caminho = "../" . $row_s->ferida_path;
$img_destino = "upload/" . $id_ferida . ".png";
$_SESSION["img_ferida"] = $row_s->ferida_path;
?>
<script type="text/javascript"> var param = "<?php echo $img_destino ?>";> </script>
<script type='text/javascript' src='../include/cropmaster/crop.js'> </script>

Crop.js file:

//Funçao que traz a variavel php
jQuery(document).ready(function(){
var pagina = param;
jQuery('#btnpaginas').click(function(){
    pagina++;
    var dados = jQuery( this ).serialize();
    jQuery.ajax({
        type: "POST",
        url: "crop.php?pagina="+pagina,
        data: dados,
        success: function(data)
        {
            $(".paginacao").html(data);
        }
    });
    return false;
});    

});

//funçao para fazer um crop na imagem
$(document).ready(function() {
var condition = 1;
var points = [];//holds the mousedown points
var canvas = document.getElementById('myCanvas');
this.isOldIE = (window.G_vmlCanvasManager);
$(function() {
  //  if (document.domain == 'localhost') {

        if (this.isOldIE) {
            G_vmlCanvasManager.initElement(myCanvas);
        }
        var ctx = canvas.getContext('2d');
        var imageObj = new Image();
        function init() {
            canvas.addEventListener('mousedown', mouseDown, false);
            canvas.addEventListener('mouseup', mouseUp, false);
            canvas.addEventListener('mousemove', mouseMove, false);
        }
        // Draw  image onto the canvas
        imageObj.onload = function() {
            ctx.drawImage(imageObj, 0, 0);
        };
        imageObj.src = "dados"; 

//imageObj.src = takes variable that comes from php

  • This Javascript code is all the contents of the file Crop.js or are different files ? Because you are using three times (Document). ready, and in the archive Crop.php only have this return HTML? The return could not be in JSON format? Because it gives to understand that the file Crop.js has already been imported into the document.

  • The . js file was actually imported into PHP, but I need to play a PHP variable inside it. That contains a path of an image that comes from the database Js do not read the PHP variable, because it is different server side languages and other client side, I used Ajax but did not succeed with the variable.

  • saved in an input Hidden is the target image right? do so in php <input id="imagemdest" type="Hidden" value="<? php echo $imagem_target; ? >" /> and take with javascript imagejavascript = Document.getElementById('imagemdest'). value;

  • Okay, thank you very much I will test this alteration.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.