Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2013
    Thanked 0 Times in 0 Posts

    Assistance Needed... Variable to JSON array


    This is my first post on this forum, so I am going to try and ask for some assistance with piece of code I am stumped upon...


    I have a site which displays an image, on top of it, I have a Canvas in which the client will click and leave a "Dot". This dot marks the coordinates (X,Y)

    I have managed to make this happen and I can get JavaScript to even display the array being created every time the mouse clicks inside the canvas...



    Unfortunately... Once I submit the string of the array (via PHP to a CSV file for now) I am getting mixed results...

    {\"X\":\"373\" <-- This in one cell
    \"Y\":\"233\"} <-- This in the next

    I assume the issue is that the file is a CSV so the comma is taken as a separator... yet for some reason I get the backslashes added too... (Still do not understand why).

    I have tried using:

    var json_array = '<?php echo json_encode('+globalCoordinates+'); ?>';

    with no success...

    I hope what I have explained so far makes any sense.

    Here is the portion of the Code I have so far...

    ==== JavaScript ====

    var mouseX, mouseY;
    var myCanvas = document.getElementById('myCanvas');
    var context = myCanvas.getContext('2d');
    var radius = 8;
    var count = 0;

    myCanvas.addEventListener('mousemove', mouseMoved,false);
    myCanvas.addEventListener('click', mouseClicked,false);

    function mouseMoved(e){
    mouseX = e.pageX - myCanvas.offsetLeft-25;
    mouseY = e.pageY - myCanvas.offsetTop -62;
    function mouseClicked(e){
    context.arc(mouseX, mouseY, radius, 0, 2 * Math.PI, false);
    context.fillStyle = '#FF1111';
    count == count++;
    document.getElementById('count').innerHTML='Total: '+count;
    document.getElementById('pcount').value= count
    getCoordinates(mouseX +","+ mouseY);

    var globalCoordinates = new Array();
    function getCoordinates(){
    globalCoordinates.push('{"X":"'+ mouseX +'","'+'Y":"'+ mouseY + '"}');

    $(function() {

    function displayCoords(){
    var json_array = '<?php echo json_encode('+globalCoordinates+'); ?>';
    document.getElementById('coordinatesTemp').innerHTML = ('<input type="text" name="coordinates" value="'+json_array);
    //document.getElementById('coordinatesTemp').innerHTML = globalCoordinates;

  2. #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Thanked 626 Times in 605 Posts
    i think your php should be without quotes:

    var json_array = <?php echo json_encode(globalCoordinates); ?>;
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  3. Users who have thanked rnd me for this post:

    htakeuchi (09-30-2013)

  4. #3
    Senior Coder
    Join Date
    Sep 2010
    Thanked 275 Times in 275 Posts
    If you set a javascript array as the value of an input, it will appear on the php side as a comma separated string of values. That's entirely without using JSON. So if you 'pushed'

    mouseX +"|"+ mouseY into globalCoordinates you would have something like this

    137|234,322|412 etc. at the php end, which could be exploded by first the , then the | to give your results.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.

    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  5. Users who have thanked DrDOS for this post:

    htakeuchi (09-30-2013)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts