SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    element.value = Can get to "explode" this after construction

    Hi.
    I have an array on php, which is available to javascript:

    document.getElementById("element_1").value = "<?php echo $arrdata[3][0]; ?>";


    (noted the number 3 ? I need to put a javascript variable instead, 0,1,2 etc.)

    I need this to be dynamic, so i have this javascript variable $curr added to dynamically construct the php part

    var $mystr = '<' + "?" + "php" + " echo $arrdata[" +$curr+ '][0]; ?>';


    Tried several ways:
    document.getElementById("element_1").value = '"'+$mystr+'"';
    or
    document.getElementById("element_1").value = $mystr;

    but still $mystr is not "expanded", so my control gets:
    "<?php echo $arrdata[1][0]; ?>"
    as a value, instead of the value from the array, which i insist, is available thrugh "<?php echo $arrdata[3][0]; ?>" for example.

    How should I construct the value part to embed javascript $curr variable?

    Any help greatly appreciated.

    Carlos

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The only way as far as i know to grab data from a PHP array through JS would be to use something like the example below.

    Code JavaScript:
    var data = <?php echo json_encode($arrdata); ?>;
     
    document.getElementById("element_1").value = data[cur_id];

    I don't exactly know how your going to set the current id needed so i put cur_id in as a placeholder.

  3. #3
    SitePoint Member
    Join Date
    Jun 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Sgt!
    To simplify things, the two-dimension array was first created on java script and filled later swapping a one-dimension array from php, thus no need to give php a variable; each temporary on php come with only 8 columns, so its manageable.


    myArray[<?php echo $offset;?>][0] = "<?php echo $parts[0];?>";
    myArray[<?php echo $offset;?>][1] = "<?php echo $parts[1];?>";
    myArray[<?php echo $offset;?>][2] = "<?php echo $parts[2];?>";


    Thanks a lot !


Bookmarks

Posting Permissions

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